File operations os / os.path / open / read

File Operations

os module

Get the current working directory os.getcwd ()

Return all files and directories in the specified directory name os.listdir ()

Function is used to delete a file os.remove ()

Create a single directory os.mkdir ( "test")

Create an empty file os.mknod ( "test.txt")

Rename os.rename (old, new)

 

os.path module

Return to a path of the directory name and file name os.path.split ()

 E.g. os.path.split ( '/ home / swaroop / byte / code / poem.txt') Results: ( '/ home / swaroop / byte / code', 'poem.txt')

Get File Size os.path.getsize (filename)

Whether given path really exist os.path.exists ()

 

open

fp = open ( "test.txt", w) directly open a file, if the file does not exist, create

Common mode on open:

w to write the way open

opened in a mode additional

r + / w + / a + are opened so as to read and write

rb opened in binary read mode

wb opened in binary write mode

rb + / wb + are opened in binary mode write

 

read

fp.read ([size]) size of the read length, a byte as a unit

Part fp.readline ([size]) to read a line, if the defined size is only possible to return line

fp.readlines ([size]) to each line of the file as a member of a list and returns the list, its interior is through the cycle call readline () to achieve

fp.close () closes the file at the end of file operations to add this command python comes though, but still want to cultivate the habit

 

 

Guess you like

Origin www.cnblogs.com/yanruizhe/p/11297651.html