博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python 的 os.path() 和 pathlib.path()
阅读量:4228 次
发布时间:2019-05-26

本文共 9450 字,大约阅读时间需要 31 分钟。

os.path()

>>> 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(path) 

>>> os.path.abspath('test.py')'/root/workspace/python3_learning/test.py'

os.path.basename(path)

>>> os.path.basename('/root/workspace/python3_learning/test.py')'test.py'

os.path.commonprefix(list)

>>> os.path.commonprefix(['/root/test.py', '/root/workspace/python3_learning'])'/root/'

os.path.dirname(path)

>>> os.path.dirname('/root/workspace/python3_learning/test.py')'/root/workspace/python3_learning'

os.path.exists(path)

>>> os.path.exists('/root/workspace/python3_learning/test.py')True>>> os.path.exists('/root/workspace/python3_learning/test3.py')False

os.path.lexists(path)

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

os.path.expanduser(path)

# root>>> os.path.expanduser('~/workspace')'/root/workspace'# looking>>> os.path.expanduser('~/workspace')'/home/looking/workspace'

os.path.expandvars(path)

>>> os.path.expandvars('${HOME}/workspace')'/root/workspace'

os.path.getatime(path)

>>> os.path.getatime('test2.py')1611627917.5157754

os.path.getmtime(path)

>>> os.path.getmtime('test2.py')1609834946.9199047

os.path.getctime(path)

>>> os.path.getctime('test2.py')1609834946.9209046

os.path.getsize(path) 

>>> os.path.getsize('test2.py')196

os.path.isabs(path)

>>> os.path.isabs('/root/workspace/python3_learning/test.py')True>>> os.path.isabs('python3_learning/test.py')False

os.path.isfile(path)

>>> os.path.isfile('test.py')True>>> os.path.isfile('non-exists')False

os.path.isdir(path)

>>> os.path.isdir('.')True>>> os.path.isdir('test.py')False

os.path.islink(path)

>>> os.path.islink('test.py')False>>> os.path.islink('test.py.link')True

os.path.ismount(path)

>>> os.path.ismount('/dev/mapper/centos-root')False>>> os.path.ismount('/')True>>> os.path.ismount('/dev')True

os.path.join(path1[, path2[, ...]])

>>> os.path.join('/root', 'workspace', 'python3_learning/')'/root/workspace/python3_learning/'

os.path.normcase(path)

>>> os.path.normcase('/root\/dft')'/root\\/dft'

os.path.normpath(path)

>>> os.path.normpath('/root\/dft')'/root\\/dft'

os.path.realpath(path)

>>> os.path.realpath('test.py')'/root/workspace/python3_learning/test.py'

 os.path.samefile(path1, path2)

>>> 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

os.path.sameopenfile(fp1, fp2)

>>> 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(stat1, stat2)

>>> 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(path)

>>> os.path.split('/root/workspace/python3_learning/test.py')('/root/workspace/python3_learning', 'test.py')

os.path.splitext(path)

>>> os.path.splitext('/root/workspace/python3_learning/test.py')('/root/workspace/python3_learning/test', '.py')>>> os.path.splitext('test.py')('test', '.py')>>>

pathlib.Path()

>>> import pathlib

和 os、os.path 的部分功能很像。 

Path.cwd()

>>> os.getcwd()'/root/workspace/python3_learning'>>> str(pathlib.Path().cwd())'/root/workspace/python3_learning'

Path.resolve()

类似于 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'

Path.exists()

>>> pathlib.Path('test.py').exists()True>>> pathlib.Path('tet.py').exists()False

Path.is_dir()

>>> pathlib.Path('test.py').is_dir()False>>> pathlib.Path('test3').is_dir()True

Path.is_file()

>>> pathlib.Path('test.py').is_file()True>>> pathlib.Path('test3').is_file()False

Path.is_symlink()

>>> pathlib.Path('test.py').is_symlink()False>>> pathlib.Path('test.py.link').is_symlink()True

Path.is_absolute()

>>> pathlib.Path('test.py').is_absolute()False>>> pathlib.Path('/root/workspace/python3_learning/test.py').is_absolute()True

Path.joinpath()

>>> 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')

Path.expanduser()

>>> pathlib.Path('~/workspace').expanduser()PosixPath('/root/workspace')

Path.name

>>> pathlib.Path('/root/workspace/python3_learning/test.py').name'test.py'

Path.stem 

>>> pathlib.Path('/root/workspace/python3_learning/test.py').stem'test'

Path.parent

>>> pathlib.Path('/root/workspace/python3_learning/test.py').parentPosixPath('/root/workspace/python3_learning')

Path.parents

>>> list(pathlib.Path('/root/workspace/python3_learning/test.py').parents)[PosixPath('/root/workspace/python3_learning'), PosixPath('/root/workspace'), PosixPath('/root'), PosixPath('/')]

Path.suffix

>>> pathlib.Path('/root/workspace/python3_learning/test.py').suffix'.py'

Path.parts

>>> pathlib.Path('/root/workspace/python3_learning/test.py').parts('/', 'root', 'workspace', 'python3_learning', 'test.py')

Path.root

>>> pathlib.Path('/root/workspace/python3_learning/test.py').root'/'

Path.with_name()

>>> pathlib.Path('/root/workspace/python3_learning/test.py').with_name('hello.py')PosixPath('/root/workspace/python3_learning/hello.py')

Path.with_suffix()

>>> pathlib.Path('/root/workspace/python3_learning/test.py').with_suffix('.rb')PosixPath('/root/workspace/python3_learning/test.rb')

Path.match()

>>> pathlib.Path('/root/workspace/python3_learning/test.py').match('*.py')True>>> pathlib.Path('/root/workspace/python3_learning/test.py').match('*.rb')False

Path.glob()

>>> 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')]

Path.rglob()

比 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/

你可能感兴趣的文章
UVM:7.8.2 get_reg_by_offset 函数
查看>>
UVM:8.2.2 重载的方式及种类
查看>>
UVM:8.2.3 复杂的重载
查看>>
UVM:8.3.1 重载transaction
查看>>
Log4j配置
查看>>
Struts2中过滤器和拦截器的区别
查看>>
51单片机:led灯闪烁10次后熄灭
查看>>
增加windows下Tomcat运行时的内存
查看>>
tomcat群集中session共享的几个方案
查看>>
查找google谷歌北京IP地址的方法
查看>>
入门 | 一文概览深度学习中的激活函数
查看>>
一分钟整明白Tensorflow Extended
查看>>
人工智能再次参加高考:和作家比写作文,AI能打多少分?
查看>>
云创冬日紫金山踏雪游记
查看>>
西安思源学院电子信息工程学院院长张卫钢一行到访
查看>>
邀请函|欢迎参加2019云创大数据实验平台金融类/电子商务类/数学统计类院校各省总代理招募大会!...
查看>>
云创大数据的2018年!
查看>>
QNX简介
查看>>
MQTT协议基本介绍
查看>>
进程和线程是操作系统基本概念,了解一下
查看>>