The Python OS module provides a unified operating system interface function One, for the operation of the system 1, Os.name is currently using the platform where ' NT ' is windows, ' POSIX ' is a specific path delimiter for Linux or unix2, OS.SEP output operating system. Win under "\", Linux for "/" 3, OS.PATHSEP output divider output for splitting the file path of the string. Windows is '; ' 4, os.linesep newline character output current platform is the line terminator, win under "\ r \ n", Linux under "\ n", Mac use ' \ R '. 5, Os.environ get system environment variable PATH6, Os.system (' dir c:\\ ') Execute system Command 7, Os.popen () Execute a system command, save execution result to file handle Dirlist=os.popen (' dir d : \\tmp ') for I in Dirlist:print i II, for directory operation 1, OS.GETCWD () Gets the current working directory, which is the directory path of the current Python script work. Results: E:\python\05072, Os.chdir (' d:\\ ') modifies the current working directory 3, OS.CURDIR returns the current directory 4, os.pardir directory switch to parent directory (top level directory) Third, action on file 1, Os.listdir (' e:\\tmp ') lists all files and subdirectories in the specified directory, including hidden files or directories, and returns them as a list. 2, Os.rename (' e:\\tmp\\1.txt ', ' e:\\tmp\\1new.txt ') modify the file name 3, Os.stat (' e:\\tmp\\02.txt ') to get the life cycle of the Files 4, Os.utime ( path[, (Atime, Mtime)]) modifies the file's Time property settings file's access and modified time for a given time if the Atime and Mtime parameters are not specified, the modification will be the current time. 5. Os.access () The output file has Write permission: Print os.access (Filename,os. W_OK) Print os.access (Fn,os. R_OK) file has Read permissions for print os.access (Fn,os. X_OK) file has permission to execute Four, os.path1, os.path.exists (path) returns the current directory or whether the file existsReturns False if the path exists and returns true if it exists. 2, Os.path.isabs (path) determines whether the path is an absolute path, and if true, returns False if it is the absolute path. 3, Os.path.isfile (path) determines whether the file determines whether the path is a file, or False if it returns true. 4, Os.path.isdir (path) determines whether the directory determines whether the path is a directory, or False if the directory returns TRUE. 5, Os.path.getsize (PATH): Units are bytes to return the size of the file or directory. Gets the file size, if name is the directory that returns 0L, and if the name represents a directory or file that does not exist, the windowserror exception is reported. 6, Os.path.abspath (path) Gets the absolute path, (but this path is not necessarily the path of the real existence) Eg:print Os.path.abspath (' d:\\tmp\\test13.txt ') print Os.path.abspath (' Test13.txt ') #返回当前执行目录下的文件名的路径, when the file is not there, it will return to print OS.GETCWD () Result: d:\tmp\test13.txtd:\python\0512\ test13.txtd:\python\05127, Os.path.normpath (path) converts path to the standard path. Used to solve cross-platform problems , get the drive letter from the path, file name, extension, directory 1, Os.path.split (path) to the file path to do the partition, the last \ \ After the file directory to split the path into a directory and file name (in fact, If you use the directory completely, it will also separate the last directory as the file name, and it will not determine whether the file or directory exists, and it is returned in the tuple. Eg:print os.path.split (' d:\\tt4\\c12 ') print os.path.split (' d:\\tt4\\c12\\ ') print os.path.split (' d:\\tt4\\c12\\ T1.txt ') Results: (' d:\\tt4 ', ' C12 ') (' d:\\tt4\\c12 ', ') ' (' D:\\tt4\\c12 ', ' T1.txt ') 2, Os.path.dirname (path) Returns the name of the directory that returns the directory path of Path, which is actually OS.PATH.SPThe first element of lit (path). 3. Os.path.basename (path) returns the name of the file that returns the last file name of the path. If path ends with a/or \, a null value is returned. The second element of Os.path.split (path). 4. Os.path.splitext (path) cut the paths and extensions separately print os.path.splitext (' 01.py ') print os.path.splitext (' d:\\tmp\\001.txt ') print Os.path.splitext (' d:\\tt4\\c12 ') Result: (' "", '. Py ') (' d:\\tmp\\001 ', '. txt ') (' d:\\tt4\\c12 ', ') ' Filename,expandname = Os.path.splitext (f) Separating the path and extension can be directly assigned to two variables, in fact, a tuple 5, os.path.splitdrive (path) splitter drive (letter) and the following path. Splits the drive and file paths and returns the results in tuples, primarily for win, and the first of the Linux tuples is always empty. The return result is Ganso 6, Os.path.join (path,*paths) combines all the paths into absolute paths. Connect two or more path names, separated by "\", if the given parameters are absolute path names, the first given absolute path will be discarded.
Python file operation: OS module