os. path: Detailed explanation of the principle and usage of python standard library
- 2021-12-11 08:15:10
- OfStack
path in os
When you look at the source code, you will see that in the
os.py
There are several lines like this in
if 'posix' in _names:
name = 'posix'
linesep = '\n'
from posix import *
# Omit some codes
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 (WindowsNT), open
ntpath.py
File, you can see that eight member variables are defined at the beginning
curdir = '.' # Current path identification
pardir = '..' #
extsep = '.' # Extension separator
sep = '\\'
pathsep = ';' # Delimiter of path in environment variable
altsep = '/' #
defpath = '.;C:\\bin' # Location of environment variables
devnull = 'nul'
In addition, there is another named
supports_unicode_filenames
Indicates whether the current system supports the file name of unicode.
It can be understood that,
path
Encapsulated in 1 and operating system-related, and different operating system code under different functions, the following default
from os.path import *
.
One-parameter function entered as a path string
输出 | |
---|---|
CODE_TAG_REPLACE_MARK_5 | 将路径修改为 CODE_TAG_REPLACE_MARK_6 形式 |
CODE_TAG_REPLACE_MARK_7 | 将路径修改为小写的 CODE_TAG_REPLACE_MARK_6 形式 |
CODE_TAG_REPLACE_MARK_9 | 返回绝对路径,其格式为 CODE_TAG_REPLACE_MARK_10 |
CODE_TAG_REPLACE_MARK_11 | 返回绝对路径,并消除其中的链接 |
CODE_TAG_REPLACE_MARK_12 | 返回相对路径,并消除其中的链接 |
CODE_TAG_REPLACE_MARK_13 | 将输入路径分割为两个部分 例如 CODE_TAG_REPLACE_MARK_10 会返回为 CODE_TAG_REPLACE_MARK_15 和 CODE_TAG_REPLACE_MARK_16 |
CODE_TAG_REPLACE_MARK_17 | 分割文件的扩展名 |
CODE_TAG_REPLACE_MARK_18 | 分割出驱动器和其他路径 |
CODE_TAG_REPLACE_MARK_19 | 最下层的目录或文件名称,即 CODE_TAG_REPLACE_MARK_20 |
CODE_TAG_REPLACE_MARK_21 | 最下层目录或文件所在目录,即 CODE_TAG_REPLACE_MARK_22 |
CODE_TAG_REPLACE_MARK_23 | 判断输入路径是否存在,存在则返回 CODE_TAG_REPLACE_MARK_24 ,否则 CODE_TAG_REPLACE_MARK_25 |
CODE_TAG_REPLACE_MARK_26 | 如果路径损坏,也返回 CODE_TAG_REPLACE_MARK_25 |
CODE_TAG_REPLACE_MARK_28 | 判断输入路径是否为绝对路径 |
CODE_TAG_REPLACE_MARK_29 | 判断输入路径是否为文件 |
CODE_TAG_REPLACE_MARK_30 | 判断输入路径是否为目录 |
CODE_TAG_REPLACE_MARK_31 | 判断输入路径是否为链接 |
CODE_TAG_REPLACE_MARK_32 | 判断输入路径是否为挂载点(在windows中就是盘符) 例如 CODE_TAG_REPLACE_MARK_33 ,返回为True |
CODE_TAG_REPLACE_MARK_34 | 通过 CODE_TAG_REPLACE_MARK_35 对路径进行扩展 |
CODE_TAG_REPLACE_MARK_36 | 将路径扩展为命令行识别的变量 |
Partial examples
>>> from os.path import *
>>> p = abspath('.')
>>> p
'E:\\Documents\\00\\1022'
>>> exists(p)
True
>>> splitdrive(p)
('E:', '\\Documents\\00\\1022')
>>> isfile(p)
False
Single parameter function related to file information
The input must be a file path, not a directory
Output
~.getsize()
Get the 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 is access
输出 | |
---|---|
CODE_TAG_REPLACE_MARK_37 | 以字节为单位获取文件大小 |
CODE_TAG_REPLACE_MARK_38 | 获取文件创建时间,c即create |
CODE_TAG_REPLACE_MARK_39 | 获取文件最后修改时间,m即modify |
CODE_TAG_REPLACE_MARK_40 | 获取文件最后访问时间,a即access |
Functions with multiple inputs
os.path.join
For splicing paths, super sweet 1 function. Many novices will be troubled by creating new folders, and often they don't know if they are writing
D:\test
Or
D:\test\
, and in
os.path.join
The two are basically equivalent.
>>> os.path.join('test','\\test1','test2\\','test3')
'\\test1\\test2\\test3'
判断是否相同 | |
---|---|
CODE_TAG_REPLACE_MARK_49 | 判断目录或文件是否相同 |
CODE_TAG_REPLACE_MARK_50 | 两个打开的文件是否指向同1个文件 |
normpath()
1
,
normpath()
2
Returns the longest path common to all path in list, that is, the common parent folder from all files and folders. The difference between the two is that the latter will add on the return path
normpath()
3
.