python os, shutil operation

Original link: https://www.cnblogs.com/VseYoung/p/9941873.html


the python file, folder (operating function) operating the need to involve os modules and shutil modules.

OS module

Get the current working directory, that directory path of the current Python script work: os.getcwd ()
returns all files and directories in the specified directory: os.listdir ()
function is used to delete a file: os.remove ()
to delete more Contents: os.removedirs: (r "c \ python")
given path if a file: os.path.isfile ()
whether the given path is a directory: os.path.isdir ()
to determine whether It is an absolute path: os.path.isabs ()
given path if really exist: os.path.exists ()

Returns a directory path and file names: os.path.split () eg os.path.split ( '/ home / swaroop / byte / code / poem.txt') Results: ( '/ home / swaroop / byte / code ',' poem.txt ')

Extension separation: os.path.splitext ()
Gets a path name: os.path.dirname ()
Gets the file name: os.path.basename ()
run shell commands: os.system ()

Read and set environment variables: os.getenv () and os.putenv ()
gives the line terminator used in the current internet: os.linesep Windows using '\ r \ n', Linux using '\ n' Mac uses' \ r '
indicates the platform you are using: os.name for Windows, it is' nt', and for Linux / Unix users, it is' posix '

Rename: os.rename (old, new)
to create multi-level directory: os.makedirs (r "c: \ python \ test")
to create a single directory: os.mkdir ( "test")
getting file attributes: os.stat ( file)
to modify file permissions and timestamps: os.chmod (file)
to terminate the current process: os.exit ()
get file size: os.path.getsize (filename)

file file operations:

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

About open mode:
w opened for writing,
A opened in append mode (from EOF start, create a new file if necessary)
r + in read-write mode open
w + Open (see w) in read-write mode
a + opened in read-write mode (see A)
RB is opened in binary read mode
wb binary opened in write mode (see W)
ab & opened in binary append mode (see A)
RB + opened in binary read-write mode (see R & lt +)
wb + opened in binary read-write mode (see w +)
ab + opened in binary read-write mode (see a +)

fp.read ([size]) #size length read in byte units
fp.readline ([size]) # line is read, if the defined size, it is possible to return only part of a line
fp.readlines ([ size]) # each line of the file as a member of a list and returns the list. In fact, its interior is invoked through the loop readline () to achieve. If the offer size parameters, size is read the contents of the total length, that is possible to read only part of the file.

fp.write (str) # The str written to a file, write () does not add a newline after str
fp.writelines (seq) seq # contents of all written to a file (multi-line one-time write into). This function is only faithfully written not add anything after each line.

fp.close () # close the file. python will turn off automatically after a file without file, but this feature is not guaranteed, it is best to develop their own habits closed. If a file has its operations after closing will produce ValueError

fp.flush () # the contents of the buffer is written to disk
fp.fileno () # Returns a long integer "file tag"
whether fp.isatty () # terminal device file is a file (unix system)
FP .tell () # returns the current position mark of the file operation, in order to beginning of the file as an origin
fp.next () # returns the next line, and the file operation flag is shifted to the next row. Put a file when used for ... in this sentence file, it is to call the next () function to implement traversal.

fp.seek (offset [, whence]) # playing the file offset of the marker to the operation. This offset is usually relative to the beginning of the file to calculate the generally positive. But whence If a parameter might not, whence the calculation can be expressed from the beginning, 1 indicates the current position as the origin is calculated as 0. 2 represents the end of the file to be calculated as the origin. Note that if a file is opened in a + or mode, every time the write operation, the file operation flag will automatically return to the end of the file.

fp.truncate ([size]) # the documents into a predetermined size, is cut to a default location of the current file operation mark. If the size of the file is larger than the size, according to the system without changing the file may be different, it may be up to the appropriate size with 0 files, it might be a bunch of random stuff to add.

shutil 操作:

Copy the file:
shutil.copyfile ( "oldfile", "newfile") can only be oldfile and newfile file
shutil.copy ( "oldfile", "newfile ") oldfile only folders, newfile can be a file, it can be destination directory

Copy the folder:
shutil.copytree ( "olddir", "newdir") olddir and newdir can only be a directory, and there is no newdir must
rename the file (directory)
os.rename ( "oldname", "newname") or file Contents are using this command
to move files (directory)
shutil.move ( "OldPos", "NewPOS")
to delete the file
os.remove ( "file")
delete the directory
os.rmdir ( "dir") can only delete empty directories
shutil .rmtree ( "dir") an empty directory with the contents of the directory can be deleted
conversion directory
os.chdir ( "path") to change path

Guess you like

Origin blog.csdn.net/wsp_1138886114/article/details/100132115