ImportOS#the file is in the location: d:\ level 1th \ 2nd level \ 3rd level \ 4th layer \ 5th Layer \test11.pypath1= Os.path.dirname (__file__)Print(path1)#gets the absolute path of the currently running scriptpath2= Os.path.dirname (Os.path.dirname (__file__))#Print(path2)#gets the absolute path of the currently running script (minus the last path)Path3= Os.path.dirname (Os.path.dirname (Os.path.dirname (__file__)))Print(PATH3)#gets the absolute path of the currently running script (minus the last 2 paths)Path4= Os.path.dirname (Os.path.dirname (Os.path.dirname (Os.path.dirname (__file__))))Print(PATH4)#gets the absolute path of the currently running script (minus the last 3 paths)Path5= Os.path.dirname (Os.path.dirname (Os.path.dirname (Os.path.dirname (Os.path.dirname (__file__)))))Print(PATH5)#gets the absolute path of the currently running script (minus the last 4 paths)Path6= OS.__file__ #get the directory where the OS residesPrint(PATH6)
Results:
C:\Python352\python.exe d:/1th/2nd/3rd/4th/5th/Test11.pyd:/1th/2nd/3rd/4th/5th-level D:/1th/2nd/3rd Layer/ 4th Floor D:/1th floor/2nd Layer/ 3rd layer D:/1th Layer/ 2nd layer D:/ 1th layer C:\Python352\lib\os.py Process finished with exit Code 0
Os.path.dirname () and Os.path.abspath () are generally used together, and we often see similar code in the Django project configuration file:
Import= Os.path.dirname (Os.path.dirname (Os.path.abspath (__file__)))
This assumes that the script file is test1.py and the absolute path is:/users/lowman/test1.py
Os.path.abspath (__file__) return/users/lowman/test1.py
Os.path.dirname (Os.path.abspath (__file__)) return/users/lowman
Os.path.dirname (Os.path.dirname (Os.path.abspath (__file__)) return/users
Os.path.abspath (__file__) 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
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
Use of Python Os.path