8.2 OS. path -- common path name Operation (1)
OS. path. isabs (path)
If the path parameter is an absolute path, True is returned; otherwise, False is returned.
Example:
# Python3.4
Fromos. pathimport *
Pathstr = 'f: \ temp \ py \ cal_1.py'
R = isabs (pathstr)
Print (r)
The output is as follows:
True
OS. path. isfile (path)
If the path parameter is returned by a file, True is returned; otherwise, False is returned.
Example:
# Python3.4
Fromos. pathimport *
Pathstr = 'f: \ temp \ py \ cal_1.py'
R = isfile (pathstr)
Print (r)
The output is as follows:
True
OS. path. isdir (path)
If the path parameter is a directory, True is returned; otherwise, False is returned.
Example:
# Python3.4
Fromos. pathimport *
Pathstr = 'f: \ temp \ py \ cal_1.py'
R = isdir (pathstr)
Print (r)
The output is as follows:
False
OS. path. islink (path)
If the path parameter is a symbolic entry connection, True is returned; otherwise, False is returned.
Example:
# Python3.4
Fromos. pathimport *
Pathstr = 'f: \ temp \ py \ cal_1.py'
R = islink (pathstr)
Print (r)
The output is as follows:
False
OS. path. ismount (path)
Determine whether the path parameter path is a mount point. If yes, return True.
Example:
# Python3.4
Fromos. pathimport *
Pathstr = 'f: \ temp \ py \ cal_1.py'
R = ismount (pathstr)
Print (r)
The output is as follows:
False
OS. path. join (path1 [, path2 [,...])
Reasonably connect directories and file composition paths.
Example:
# Python3.4
Fromos. pathimport *
Pathstr = 'f: \ temp \ py \ cal_1.py'
R = join ('f: \ temp ',' \ py \ cal_1.py ')
Print (r)
The output is as follows:
F: \ py \ cal_1.py
OS. path. normcase (path)
Format the case and slash of the path.
Example:
# Python3.4
Fromos. pathimport *
Pathstr = 'f: \ Temp \ py \ cal_1.py'
R = normcase (pathstr)
Print (r)
The output is as follows:
F: \ temp \ py \ cal_1.py
OS. path. normpath (path)
Remove the relative path from the path.
Example:
# Python3.4
Fromos. pathimport *
Pathstr = 'f: \ Temp \ .. \ py \ cal_1.py'
R = normpath (pathstr)
Print (r)
The output is as follows:
F: \ py \ cal_1.py
OS. path. realpath (path)
Returns the actual path. If it is a relative path, it is removed. If it is a shortcut, the actual path is returned.
Example:
# Python3.4
Fromos. pathimport *
Pathstr = 'f: \ Temp \ .. \ py \ cal_1.py'
R = realpath (pathstr)
Print (r)
The output is as follows:
F: \ py \ cal_1.py
OS. path. relpath (path, start = OS. curdir)
Convert the path to a relative path based on the directory start.
Example:
# Python3.4
Fromos. pathimport *
Pathstr = 'f: \ Temp \ .. \ py \ cal_1.py'
R = relpath (pathstr)
Print (r)
The output is as follows:
.. \... \ .. \ Py \ cal_1.py
OS. path. samefile (path1, path2)
Determine whether the two paths are the same. If the values are the same, True is returned. Otherwise, False is returned.
Example:
# Python3.4
Fromos. pathimport *
Pathstr = 'f: \ Temp \ py \ cal_1.py'
R = relpath (pathstr)
Print (r)
R = samefile (pathstr, r)
Print (r)
The output is as follows:
.. \ Py \ cal_1.py
True
OS. path. sameopenfile (fp1, fp2)
Checks whether fp1 and fp2 are the same files. If they are the same, True is returned. Otherwise, False is returned.
Example:
# Python3.4
Fromos. pathimport *
Pathstr = 'f: \ Temp \ py \ cal_1.py'
R = relpath (pathstr)
Print (r)
Fp1 = open (pathstr)
Fp2 = open (r)
R = sameopenfile (fp1.fileno (), fp2.fileno ())
Print (r)
Fp1.close ()
Fp2.close ()
The output is as follows:
.. \ Py \ cal_1.py
True
OS. path. samestat (stat1, stat2)
The file status is used to determine whether two files point to the same file. If yes, True is returned; otherwise, False is returned.
Example:
# Python3.4
Fromos. pathimport *
Importos
Pathstr = 'f: \ Temp \ py \ cal_1.py'
R = relpath (pathstr)
Print (r)
Fp1 = OS. stat (pathstr)
Fp2 = OS. stat (r)
R = samestat (fp1, fp2)
Print (r)
The output is as follows:
.. \ Py \ cal_1.py
True
OS. path. split (path)
Divide the path into two parts: path and file name.
Example:
# Python3.4
Fromos. pathimport *
Importos
Pathstr = 'f: \ Temp \ py \ cal_1.py'
R = split (pathstr)
Print (r)
The output is as follows:
('F: \ Temp \ py', 'cal _ 1. py ')
OS. path. splitdrive (path)
Break down the path into the driver and the path relative to the driver.
Example:
# Python3.4
Fromos. pathimport *
Importos
Pathstr = 'f: \ Temp \ py \ cal_1.py'
R = split (pathstr)
Print (r)
R = splitdrive (pathstr)
Print (r)
The output is as follows:
('F: \ Temp \ py', 'cal _ 1. py ')
('F: ',' \ Temp \ py \ cal_1.py ')
OS. path. splitext (path)
Break down the path into the file name and suffix.
Example:
# Python3.4
Fromos. pathimport *
Importos
Pathstr = 'f: \ Temp \ py \ cal_1.py'
R = split (pathstr)
Print (r)
R = splitdrive (pathstr)
Print (r)
R = splitext (pathstr)
Print (r)
The output is as follows:
('F: \ Temp \ py', 'cal _ 1. py ')
('F: ',' \ Temp \ py \ cal_1.py ')
('F: \ Temp \ py \ cal_1 ','. py ')
OS. path. splitunc (path)
The path is decomposed by unc.
Example:
# Python3.4
Fromos. pathimport *
Importos
Pathstr = 'f: \ Temp \ py \ cal_1.py'
R = split (pathstr)
Print (r)
R = splitdrive (pathstr)
Print (r)
R = splitext (pathstr)
Print (r)
R = splitunc (pathstr)
Print (r)
R = splitunc (R' \ 192.168.0.1 \ abc \ test. py ')
Print (r)
The output is as follows:
('F: \ Temp \ py', 'cal _ 1. py ')
('F: ',' \ Temp \ py \ cal_1.py ')
('F: \ Temp \ py \ cal_1 ','. py ')
('', 'F: \ Temp \ py \ cal_1.py ')
('\\\\ 192.168.0.1 \ abc',' \ test. py ')
OS. path. supports_unicode_filenames
If the Unicode string can be used as the file name, True is returned; otherwise, False is returned.
Example:
# Python3.4
Fromos. pathimport *
Importos
Pathstr = 'f: \ Temp \ py \ cal_1.py'
R = split (pathstr)
Print (r)
Print (supports_unicode_filenames)
The output is as follows:
('F: \ Temp \ py', 'cal _ 1. py ')
True