Python Os.path module Common methods detailed
1. Os.path.abspath (PATH)
Returns the absolute path normalized by path.
>>> Import OS
>>> os.path.abspath (' Pjc.txt ')
'/home/pjc/pjc.txt '
>>> os.path.abspath (' c:\\test.csv ') #Windows主机指定完美的路径
' C:\\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 ('/home/pjc/pjc.txt ')
('/HOME/PJC ', ' pjc.txt ')
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.dirname ('/home/pjc/pjc.txt ')
'/HOME/PJC '
>>> os.path.dirname ('/home/pjc/')
'/HOME/PJC '
>>> os.path.dirname ('/HOME/PJC ')
' Home '
4.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
5.os.path.isabs (PATH)
Returns true if path is an absolute path.
6.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
7.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
>>> os.path.isdir ('/HOME/PJC ')
True
This article is from the "willing to share with you" blog, please be sure to keep this source http://pengjc.blog.51cto.com/9255463/1854261
Python Os.path module Common methods detailed