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 ('.. /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 directory of path. is actually the first element of Os.path.split (path).
>>> os.path.dirname (' c:\\csv\test.csv ')
' C:\\ '
>>> os.path.dirname (' c:\\csv ')
' C:\\ '
4.os.path.basename (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 ' (here CSV is treated as a filename)
>>> os.path.basename (' c:\\csv\\ ')
‘‘
5.os.path.commonprefix (list)
Returns the longest path common to all paths in the list.
Such as:
>>> 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\\ ')
False
9.os.path.isdir (PATH)
Returns true if path is a directory that exists. Otherwise, false is returned.
>>> os.path.isdir (' c:\\ ')
True
>>> os.path.isdir (' c:\\csv\\ ')
False
>>> os.path.isdir (' c:\\windows\\test.csv ')
False
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)
Normalize the path.
>>> Os.path.normpath (' c://windows\\system32\\. /temp/')
' C:\\Windows\\Temp '
12.os.path.splitdrive (PATH)
Return (Drivername,fpath) tuples
>>> os.path.splitdrive (' C:\\Windows ')
(' C: ', ' \\windows ')
13.os.path.splitext (PATH)
Detach file name and extension; default return (fname,fextension) tuple, can do shard operation
>>> 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.
>>> os.path.getsize (' C:\\Boot.ini ')
299L
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
Python Os.path module Common methods detailed