Os.path module is mainly used for file property acquisition, which is often used in programming, the following are several common methods of the module. More ways to see the official documents: http://docs.python.org/library/os.path.html
1.os.path.abspath (path) returns the absolute path normalized by path.
>>> Os.path.abspath ( " test.csv " ) c:\\python25\\test.csv " >> > Os.path.abspath ( ' c:\\test.csv " ) " c:\\test.csv " >>> Os.path.abspath ( Span style= "COLOR: #800000" > " . /csv\\test.csv " ) " c:\\csv\\test.csv
2.os.path.split (path)The path is split into a directory and a file name is returned by a two tuple.
>>> os.path.split ('c:\\csv\\test.csv') ('c:\\csv 'test.csv') >>> os.path.split (' c:\\csv\\') ('c:\\csv') )
3.os.path.dirname (PATH) Returns the last file name of path. If path ends with a/or \, then a null value is returned. The second element of Os.path.split (path).
>>> os.path.basename ('c:\\test.csv') 'test.csv ' >>> os.path.basename ('c:\\csv') ' CSV ' (CSV here is treated as a file name ) >>> os.path.basename ('c:\\csv\\') '
The 5.os.path.commonprefix (list) returns the longest path common to all paths in the list.
>>> Os.path.commonprefix (['/home/td','/home/td/ff' , '/home/td/fff']) '/home/td'
6.os.path.exists (path) returns true if path exists, or false if path does not exist.
>>> os.path.exists ('c:\\') True >>> os.path.exists ('c:\\csv\\test.csv') False
7.os.path.isabs (path) returns True if path is an absolute path. 8.os.path.isfile (path) returns True if path is a file that exists. Otherwise, false is returned.
>>> os.path.isfile ('c:\\boot.ini') True >>> Os.path.isfile ('c:\\csv\\test.csv') False >>> Os.path.isfile ('c:\\csv\\')
9.os.path.isdir (path) returns True if path is a directory that exists. Otherwise, false is returned.
>>> os.path.isfile ('c:\\boot.ini') True >>> Os.path.isfile ('c:\\csv\\test.csv') False >>> Os.path.isfile ('c:\\csv\\')
10.os.path.join (path1[, path2[, ...]) When multiple paths are combined and returned, the parameters before the first absolute path are ignored.
>>> Os.path.join ('c:\\','CSV','Test.csv') 'C:\\csv\\test.csv'>>> Os.path.join ('Windows\Temp','c:\\','CSV','Test.csv') 'C:\\csv\\test.csv'>>> Os.path.join ('/HOME/AA','/home/aa/bb','/home/aa/bb/c') '/home/aa/bb/c' 11.os.path.normcase (Path) on the Linux and Mac platforms, this function returns path as it is, converting all characters in the path to lowercase on the Windows platform and converting all slashes to a slash.
>>> os.path.normcase ('c:/windows\\system32\\') ' c:\\windows\\system32\\'
12.os.path.normpath (path) normalizes the path.
>>> Os.path.normpath ('c://windows\\system32\\. /temp/') 'c:\\windows\\temp'
12.os.path.splitdrive (Path) returns the (Drivername,fpath) tuple.
>>> os.path.splitdrive ('c:\\windows') (' C: ' ' \\windows '
13.os.path.splitext (path) separates the file name from the extension; the default return (fname,fextension) tuple, which can be used for sharding operations.
>>> os.path.splitext ('c:\\csv\\test.csv') ('c:\\ Csv\\test'. csv'
14.os.path.getsize (path) returns the size (in bytes) of the file for path.15.os.path.getatime (path)Returns the last access time of the file or directory to which path is pointing. 16.os.path.getmtime (Path) returns the last modified time of the file or directory to which path is pointing.
Common methods of Python:os.path modules