Python Learning Note 2 python file processing

Source: Internet
Author: User
Tags lstat python script

Import Tab #支持tab补全

Import Fileinput # supports file-based string substitution

For line in Fileinput.input (' filepath ', inplace=1): #1表示完成后回到行首

Line=line.replace (OLDSTR,NEWSTR)



Import OS

Get the current working directory, which is the directory path of the current Python script work: OS.GETCWD ()

Returns all files and directory names in the specified directory: Os.listdir ()

function to delete a file: Os.remove ()

Delete multiple directories: Os.removedirs (R ' C:\python ')

Detects whether the given path is a file or a directory: Os.path.isfile () Os.path.isdir ()

Determine if it is an absolute path: Os.path.isabs ()

Determine if the path exists: os.path.exists ()

Returns the directory name and file name of a path: Os.path.split ()

Detach extension: Os.path.splitext ()

Get path name: Os.path.dirname ()

Get file name: Os.path.basename ()

Run shell command: Os.system ()

Read and SET Environment variables: os.getenv () and os.setenv ()

Gives the line terminator used by the current platform: Os.linesep ()

Determine the current platform for Python: os.name for Windows, he is ' NT ', and for unix/linux users, he is ' POSIX '

Renamed: Os.rename (old,new)

Create a multilevel directory: Os.makedirs (R ' c:\python\test ')

Create a single directory: Os.mkdir ("Test")

Get file attributes: Os.stat (file) or Os.lstat (file)

>>> os.lstat (' D:/media ')

Nt.stat_result (st_mode=16895, st_ino=0l, st_dev=0, st_nlink=0, St_uid=0, St_gid=0, st_size=0l, St_atime=1411793385L, st_mtime=1411793385l, st_ctime=1390222253l)

>>> os.stat (' D:/media ')

Nt.stat_result (st_mode=16895, st_ino=0l, st_dev=0, st_nlink=0, St_uid=0, St_gid=0, st_size=0l, St_atime=1411793385L, st_mtime=1411793385l, st_ctime=1390222253l)

>>>

Modify file permissions and timestamps: Os.chmod (file)

Terminate current process: Os.exit ()

Get File Size: os.path.getsize (filename)



File operation:

Os.mknod (' Test.txt ') #创建空文件


F=open (' Test.txt ', ' R ') # R W A


F.read ([size]) #size为读取的长度, in bytes

F.readline ([size])

F.readlines ([size])

F.



Python Learning Note 2 python file processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.