os.sep: replace the operating system-specific path separator os.name: indicates the operating platform you are using. For example, for Windows, it is ' NT ' , and for Linux / Unix users, it is ' the POSIX ' . os.getcwd: get the current working directory that the current directory path python script work. os.getenv () and os.putenv: are used to read and set environment variables os.listdir (): Returns the name of 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): create a 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: given the current platform's line terminator. For example, Windows uses ' \ r \ the n- ' , Linux use ' \ the n- ' Mac uses ' \ r' 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, there is no 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 (): determining whether an absolute path os.path.normpath (path): specification path string os.path.join (path, name): connection list file or directory os.path.split (name): split file name and directory (in fact, if you use the full catalog, it also It will last a directory as the file name are separated, while it does not determine the existence of a file or directory) os.path.splitext (): separating the file name and extension os.path.basename (path): Returns the filename os.path.dirname (path): returns the file path
os.mknod ( " named text.txt in the " ): Create an empty file fp = Open ( " named text.txt in the " , w): directly open a file, if the file does not exist to create a file
w-write a append mode open (EOF from the beginning, creating a new file if necessary) r + in read-write mode open w + in read-write mode open a + opened in read-write mode rb opened in binary read mode wb write in binary 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 (see, a binary write mode + )
fp.read ([size]) # size length read in byte units fp.readline ([size]) # read line, if the defined size, it is possible to return only part of the row fp.readlines ([ size]) # put 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) # contents seq 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, in order to 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 [, The whence]) # file play operation of the marker to the offset. 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 folders, newfile can be a file, it 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 delete empty directories shutil.rmtree ( " dir " ) an empty directory with the contents of the directory can be deleted os.chdir ( " path " ) conversion directory, change the path