path in os
Viewing the source code will show that theThere are a few lines in
if 'posix' in _names: name = 'posix' linesep = '\n' from posix import * # Omit some code elif 'nt' in _names: from nt import * try: from nt import _exit __all__.append('_exit') except ImportError: pass import ntpath as path #。。。
Since we are in windows (Windows NT), open thefile, you can see that eight member variables are defined at the beginning
curdir = '.' # Current path identifier pardir = '..' # extsep = '.' # extension separator sep = '\\' pathsep = ';' # path separator in environment variables altsep = '/' # defpath = '.;C:\\bin' # Where environment variables are stored devnull = 'nul'
In addition, there is a program calledsupports_unicode_filenames
is a boolean member variable indicating whether the current system supports unicode filenames.
It can be understood thatpath
The following is a list of operating system-related features that are encapsulated in the defaultfrom import *
。
Single-parameter functions whose input is a path string
exports | |
---|---|
normpath() |
Change the path to\\a\\b formality |
normcase() |
Change the path to lowercase\\a\\b formality |
abspath() |
Returns an absolute path in the formX:\\a\\b
|
realpath() |
Returns the absolute path and eliminates the links in it |
relpath() |
Returns the relative path and eliminates the links in it |
split() |
Split the input path into two parts for example X:\\a\\b will return asX:\\a cap (a poem)b
|
splitext() |
Split file extension |
splidrive() |
Splitting out drives and other paths |
basename() |
The name of the lowest level directory or file, thesplit()[1]
|
dirname() |
The lowest directory or directory where the file is located, thesplit()[0]
|
exists() |
Determine whether the input path exists, if it does, returnTrue or elseFalse
|
lexists() |
If the path is corrupted, it also returnsFalse
|
isabs() |
Determine if the input path is an absolute path |
isfile() |
Determine if the input path is a file |
isdir() |
Determine if the input path is a directory |
islink() |
Determine if the input path is a link |
ismount() |
Determine if the input path is a mount point (in windows it's the disk drive letter) for example ('C:\\') The return value is True. |
expanduser() |
pass (a bill or inspection etc)~ Expanding on paths |
expandvars() |
Expanding paths into variables recognized by the command line |
Some examples
>>> from import * >>> p = abspath('.') >>> p 'E:\\Documents\\00\\1022' >>> exists(p) True >>> splitdrive(p) ('E:', '\\Documents\\00\\1022') >>> isfile(p) False
Single parameter functions related to file information
The input must be a file path, not a directory.
~.getsize()
Get file size in bytes~.getctime()
Get the file creation time, c is create~.getmtime()
Get the last modification time of the file, m is modify~.getatime()
Get the last access time of the file, a i.e. access
exports | |
---|---|
~.getsize() |
Get file size in bytes |
~.getctime() |
Get the file creation time, c is create |
~.getmtime() |
Get the last modification time of the file, m is modify |
~.getatime() |
Get the last access time of the file, a i.e. access |
Functions whose inputs are multiple arguments
, for splicing paths, a super sweet feature. Many newbies will be troubled for the new folder, many times they don't know whether to write the
D:\test
neverthelessD:\test\
while inin which the two are essentially equivalent.
>>> ('test','\\test1','test2\\','test3') '\\test1\\test2\\test3'
Determine if they are the same | |
---|---|
~.samefile(p1, p2) |
Determine if a directory or file is the same |
~.sameopenfile(fp1, fp2) |
Whether two open files point to the same file |
commonpath(list)
,commonprefix(list)
: Returns the longest path common to all paths in the list, i.e., the distance to the common parent folder of all files and folders. The difference between the two is that the latter will add a\
。
to this article on the python standard library principles and usage details of the article is introduced to this, more related python content please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!