the python os module, open Parameter Description

#os learning module
Import os
"" "
os.sep: replace the operating system-specific path separator
os.name:. indicates the operating platform you are using, such as for Windows, it is 'nt', and for Linux / Unix users, it is 'POSIX'.
the os.getcwd: get the current working directory, i.e. the current directory path python script work.
the os.getenv () and os.putenv: it is used to read and set the environment variable
os.listdir (): returns All files and directories in the specified directory
os.remove (file): delete a file
os.stat (file): get the file attributes
os.chmod (file): modify file permissions and timestamps
os.mkdir (name): Creating directory
os.rmdir (name): delete the directory
os.removedirs (r "c: \ python "): delete multiple directories
os.system (): run shell commands
os.exit (): terminate the current process
os.linesep: to the current platform of line terminator. For example, Windows uses' \ r \ n ', Linux use' \ n 'Mac uses' \ r'

os.getcwd () # get the current working directory
os.path.abspath ( '. ') # get the current working directory
os.path.abspath (' .. ') to get the current working directory of the parent directory #
os.path.abspath (os.curdir) # get the current working directory

os.path #
os.path.split (): Returns the path of a directory name and file name
path to check if the given os.path.isfile () and os.path.isdir () is a directory or file
os.path. existe (): given path really exists
os.listdir (dirname): lists the directories and files in the dirname
os.getcwd (): get the current working directory
os.curdir: returns the current directory ( '.')
os.chdir (dirname): change the working directory to the dirname
os.path.isdir (name): judge name is not a directory, not a directory returns false
os.path.isfile (name): determine whether the name of this file exists, does not exist return false
os.path.exists (name): determining whether there is a file or directory name
os.path.getsize (name): get the file size, or if the name is a directory to return 0L
os.path.abspath (name): get the absolute path
os.path.isabs (): determine whether the absolute path
os.path.normpath (path): specification path string
os.path.split (name): split file name and directory (in fact, if you use the full catalog it will also last a directory as the file name are separated, while it does not determine the file or directory exists)
O s.path.splitext (): file name and extension separation
os.path.join (path, name): the connected directory and filename or directory
os.path.basename (path): Returns the file name
os.path.dirname (path): Returns the file path
"" "
" ""
#open parameters:
w-write
a append mode open (from EOF start, create a new file if necessary)
r + in read-write mode open
w + opened in read-write mode
a + opened in read-write mode
rb opened in binary read mode
wb in binary write mode open (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 (refer to 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 the row

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 can also be manipulated to produce ValueError after closing

fp.flush () # the contents of the buffer is written to disk

fp.fileno () # returns a long integer "file tag"

fp.isatty () # if the file is a terminal device file (unix system)

fp.tell () # returns the current position mark of the file operation to the beginning of the file as an origin

fp.next () # returns the next line, and the operation flag is shifted to a file the next line. 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.

Directory operations

os.mkdir ( "file") to create the directory

shutil.copyfile ( "oldfile", "newfile ") to copy files: oldfile and can only be newfile file

shutil.copy ( "oldfile", "newfile ") oldfile only is the folder, newfile can be a file, can also be a target directory

shutil.copytree ( "olddir", "newdir ") and copy the folder .olddir newdir can only be a directory, and newdir must not exist

os.rename ( "oldname "," newname ") rename the file (directory). files or directories are using this command

shutil.move (" oldpos "," newpos ") to move the file (directory)

os.rmdir (" dir ") can only be deleted empty directory

shutil.rmtree ( "dir") an empty directory with the contents of the directory can be deleted

os.chdir ( "path") conversion directory, change the path
"" "

#eg:
IF __name__ __ ==" __ main__ ":
  Print (os .getcwd ()) #D: \ 000 \ python \ pytext21 \ foundation that runs the file directory
  print (os.path.abspath ( '.')) # ditto, as D: \ 000 \ python \ pytext21 \ basic
  print (os.path.abspath ( '..') ) #D: \ 000 \ python \ pytext21 go back one level
  filepathname=os.getcwd()+"\测试文件夹"
  if not os.path.exists(filepathname):
  os.mkdir(filepathname)









Guess you like

Origin www.cnblogs.com/yiyea/p/11442625.html