本文共 9450 字,大约阅读时间需要 31 分钟。
>>> import os
在 Python 中一旦涉及到路径相关的操作,os.path() 模块无疑是用得最多的了,下面就让我们一块来看看吧!
方法 | 说明 |
---|---|
os.path.abspath(path) | 返回绝对路径 |
os.path.basename(path) | 返回文件名 |
os.path.commonprefix(list) | 返回list(多个路径)中,所有path共有的最长的路径 |
os.path.dirname(path) | 返回文件所在目录路径 |
os.path.exists(path) | 路径存在则返回True,路径损坏返回False |
os.path.lexists(path) | 路径存在则返回True,路径损坏也返回True (Test whether a path exists. Returns True for broken symbolic links) |
os.path.expanduser(path) | 把path中包含的"~"和"~user"转换成用户目录 |
os.path.expandvars(path) | 根据环境变量的值替换path中包含的"$name"和"${name}" |
os.path.getatime(path) | 返回最近访问时间(浮点型秒数) |
os.path.getmtime(path) | 返回最近文件修改时间 |
os.path.getctime(path) | 返回文件 path 创建时间 |
os.path.getsize(path) | 返回文件大小,如果文件不存在就返回错误 |
os.path.isabs(path) | 判断是否为绝对路径 |
os.path.isfile(path) | 判断路径是否为文件 |
os.path.isdir(path) | 判断路径是否为目录 |
os.path.islink(path) | 判断路径是否为链接 |
os.path.ismount(path) | 判断路径是否为挂载点 |
os.path.join(path1[, path2[, ...]]) | 把目录和文件名合成一个路径 |
os.path.normcase(path) | 转换path的大小写和斜杠 |
os.path.normpath(path) | 规范path字符串形式 |
os.path.realpath(path) | 返回path的真实路径 |
os.path.relpath(path[, start]) | 从start开始计算相对路径 |
os.path.samefile(path1, path2) | 判断目录或文件是否相同 |
os.path.sameopenfile(fp1, fp2) | 判断fp1和fp2是否指向同一文件 |
os.path.samestat(stat1, stat2) | 判断stat tuple stat1和stat2是否指向同一个文件 |
os.path.split(path) | 把路径分割成 dirname 和 basename,返回一个元组 |
os.path.splitdrive(path) | 一般用在 windows 下,返回驱动器名和路径组成的元组 |
os.path.splitext(path) | 分割路径中的文件名与拓展名 |
os.path.splitunc(path) | 把路径分割为加载点与文件 |
os.path.walk(path, visit, arg) | Python3 好像没这个了,应该变成 os.walk() 了。 |
os.path.supports_unicode_filenames | 设置是否支持unicode路径名 |
>>> os.path.abspath('test.py')'/root/workspace/python3_learning/test.py'
>>> os.path.basename('/root/workspace/python3_learning/test.py')'test.py'
>>> os.path.commonprefix(['/root/test.py', '/root/workspace/python3_learning'])'/root/'
>>> os.path.dirname('/root/workspace/python3_learning/test.py')'/root/workspace/python3_learning'
>>> os.path.exists('/root/workspace/python3_learning/test.py')True>>> os.path.exists('/root/workspace/python3_learning/test3.py')False
test.py.bak 是 test.py 的符号链接。符号链接所指向的文件即使不存在了,只要符号链接仍然存在,也仍然返回 True。
>>> os.path.lexists('test.py.link')True>>> os.path.exists('test.py.link')True>>> os.remove('test.py')>>> os.path.lexists('test.py.link')True>>> os.path.exists('test.py.link')False
# root>>> os.path.expanduser('~/workspace')'/root/workspace'# looking>>> os.path.expanduser('~/workspace')'/home/looking/workspace'
>>> os.path.expandvars('${HOME}/workspace')'/root/workspace'
>>> os.path.getatime('test2.py')1611627917.5157754
>>> os.path.getmtime('test2.py')1609834946.9199047
>>> os.path.getctime('test2.py')1609834946.9209046
>>> os.path.getsize('test2.py')196
>>> os.path.isabs('/root/workspace/python3_learning/test.py')True>>> os.path.isabs('python3_learning/test.py')False
>>> os.path.isfile('test.py')True>>> os.path.isfile('non-exists')False
>>> os.path.isdir('.')True>>> os.path.isdir('test.py')False
>>> os.path.islink('test.py')False>>> os.path.islink('test.py.link')True
>>> os.path.ismount('/dev/mapper/centos-root')False>>> os.path.ismount('/')True>>> os.path.ismount('/dev')True
>>> os.path.join('/root', 'workspace', 'python3_learning/')'/root/workspace/python3_learning/'
>>> os.path.normcase('/root\/dft')'/root\\/dft'
>>> os.path.normpath('/root\/dft')'/root\\/dft'
>>> os.path.realpath('test.py')'/root/workspace/python3_learning/test.py'
>>> os.path.samefile('test.py', 'test2.py')False>>> os.path.samefile('test.py', 'test.py')True>>> os.path.samefile('test.py', 'test.py.link')True
>>> fd1 = os.open('test.txt', os.O_RDWR|os.O_CREAT)>>> fd2 = os.open('test.txt', os.O_RDWR|os.O_CREAT)>>> os.path.sameopenfile(fd1, fd2)True>>> fd3 = os.open('foo.txt', os.O_RDWR|os.O_CREAT)>>> os.path.sameopenfile(fd1, fd3)False
>>> os.path.samestat(os.stat('test.py'), os.stat('test.py'))True>>> os.path.samestat(os.stat('test.py'), os.stat('test.py.link'))True>>> os.path.samestat(os.stat('test.py'), os.stat('test2.py'))False
>>> os.path.split('/root/workspace/python3_learning/test.py')('/root/workspace/python3_learning', 'test.py')
>>> os.path.splitext('/root/workspace/python3_learning/test.py')('/root/workspace/python3_learning/test', '.py')>>> os.path.splitext('test.py')('test', '.py')>>>
>>> import pathlib
和 os、os.path 的部分功能很像。
>>> os.getcwd()'/root/workspace/python3_learning'>>> str(pathlib.Path().cwd())'/root/workspace/python3_learning'
类似于 os.path.abspath(),一般可以用来查找软链接文件对应的真实文件。不过 Path.resolve() 似乎一直返回的绝对路径,os.readlink() 如果读取到真实文件就在当前目录,就不返回绝对路径了。
>>> import pathlib>>> pathlib.Path('test.py').resolve()PosixPath('/root/workspace/python3_learning/test.py')>>> pathlib.Path('test.py.link').resolve()PosixPath('/root/workspace/python3_learning/test.py')>>> import os>>> os.readlink('test.py.link')'test.py'>>> os.readlink(os.path.abspath('test.py.link'))'test.py'>>> os.path.abspath('test.py.link')'/root/workspace/python3_learning/test.py.link'>>> os.path.abspath(os.readlink('test.py.link'))'/root/workspace/python3_learning/test.py'
[root@master python3_learning]# lltotal 76-rw-r--r--. 1 root root 422 Mar 10 09:55 test.pylrwxrwxrwx. 1 root root 7 Feb 3 17:37 test.py.link -> test.pylrwxrwxrwx. 1 root root 13 Mar 11 09:01 test.sh.link -> /root/test.sh-rwxr-xr-x. 1 root root 0 Feb 3 18:31 test.txt>>> os.path.abspath(os.readlink('test.sh.link'))'/root/test.sh'>>> str(pathlib.Path('test.sh.link').resolve())'/root/test.sh'>>> os.readlink('test.py.link')'test.py'>>> str(pathlib.Path('test.py.link').resolve())'/root/workspace/python3_learning/test.py'
>>> pathlib.Path('test.py').exists()True>>> pathlib.Path('tet.py').exists()False
>>> pathlib.Path('test.py').is_dir()False>>> pathlib.Path('test3').is_dir()True
>>> pathlib.Path('test.py').is_file()True>>> pathlib.Path('test3').is_file()False
>>> pathlib.Path('test.py').is_symlink()False>>> pathlib.Path('test.py.link').is_symlink()True
>>> pathlib.Path('test.py').is_absolute()False>>> pathlib.Path('/root/workspace/python3_learning/test.py').is_absolute()True
>>> str(pathlib.Path('/root').joinpath('hello', 'world'))'/root/hello/world'>>> pathlib.Path('/root').joinpath('hello', 'world').name'world'>>> pathlib.Path('/root').joinpath('hello', 'world').parentPosixPath('/root/hello')
下面这种路径的拼接方式是不是挺简单粗暴的!!!:)
>>> pathlib.Path('/root') / 'hello'PosixPath('/root/hello')>>> pathlib.Path('/root') / pathlib.Path('hello')PosixPath('/root/hello')>>> pathlib.Path('/root') / pathlib.Path('hello', 'world')PosixPath('/root/hello/world')
>>> pathlib.Path('~/workspace').expanduser()PosixPath('/root/workspace')
>>> pathlib.Path('/root/workspace/python3_learning/test.py').name'test.py'
>>> pathlib.Path('/root/workspace/python3_learning/test.py').stem'test'
>>> pathlib.Path('/root/workspace/python3_learning/test.py').parentPosixPath('/root/workspace/python3_learning')
>>> list(pathlib.Path('/root/workspace/python3_learning/test.py').parents)[PosixPath('/root/workspace/python3_learning'), PosixPath('/root/workspace'), PosixPath('/root'), PosixPath('/')]
>>> pathlib.Path('/root/workspace/python3_learning/test.py').suffix'.py'
>>> pathlib.Path('/root/workspace/python3_learning/test.py').parts('/', 'root', 'workspace', 'python3_learning', 'test.py')
>>> pathlib.Path('/root/workspace/python3_learning/test.py').root'/'
>>> pathlib.Path('/root/workspace/python3_learning/test.py').with_name('hello.py')PosixPath('/root/workspace/python3_learning/hello.py')
>>> pathlib.Path('/root/workspace/python3_learning/test.py').with_suffix('.rb')PosixPath('/root/workspace/python3_learning/test.rb')
>>> pathlib.Path('/root/workspace/python3_learning/test.py').match('*.py')True>>> pathlib.Path('/root/workspace/python3_learning/test.py').match('*.rb')False
>>> list(pathlib.Path('/root/workspace/python3_learning/').glob('*.py'))[PosixPath('/root/workspace/python3_learning/world.py'), PosixPath('/root/workspace/python3_learning/ip-address.py'), PosixPath('/root/workspace/python3_learning/test.py'), PosixPath('/root/workspace/python3_learning/minio_client.py'), PosixPath('/root/workspace/python3_learning/test2.py'), PosixPath('/root/workspace/python3_learning/find_symbolic_links.py')]
比 rglob 多了一个 r,r 表示递归(recursive)的意思。
>>> list(pathlib.Path('/root/workspace/python3_learning/').rglob('*.py'))[PosixPath('/root/workspace/python3_learning/world.py'), PosixPath('/root/workspace/python3_learning/ip-address.py'), PosixPath('/root/workspace/python3_learning/test.py'), PosixPath('/root/workspace/python3_learning/minio_client.py'), PosixPath('/root/workspace/python3_learning/test2.py'), PosixPath('/root/workspace/python3_learning/find_symbolic_links.py'), PosixPath('/root/workspace/python3_learning/test3/test.py')]
转载地址:http://dijqi.baihongyu.com/