os.path 模块下 os.path.join()、os.path.exists()、os.path.split() 等基本函数用法介绍

关注公众号:【小张Python】,为你准备了 50+ 本Python 精品电子书籍 与 50G + 优质视频学习资料,后台回复关键字:1024 即可获取;如果对博文内容有什么疑问,后台添加作者【个人微信】,可与作者直接进行交流

os.path.isdir(path)
  • path(str),文件路径

判断 path 是否为文件夹目录,返回 True 或 False;

>>> os.listdir()
['map-location.xlsx', 'Map123.gif', 'recu_dir']
>>> os.path.isdir(os.getcwd() +'/Map123.gif')
False
>>> os.path.isdir(os.getcwd() +'/recu_dir')
True
os.path.join(path,name)
  • path(str),文件夹目录;
  • name(str),文件名;

通过 path + / + name 构建文件路径

>>> os.path.join('D:/ceshi','ceshi.txt')
'D:/ceshi\\ceshi.txt'
os.path.split(name)
  • name(str),文件路径;

将一个文件路径拆分为文件夹目录和文件名

>>> os.path.split('D:/ceshi/ceshi.txt')
('D:/ceshi', 'ceshi.txt')
os.path.exists(path)
  • path(str),文件路径或文件夹目录;

判断文件路径 path 是否存在,返回 True 或 False;

>>> os.path.exists(os.getcwd() +'/recu_dir')
True
>>> os.path.exists(os.getcwd() +'/recu_dir1')
False
os.path.isfile(path)
  • path(str),文件路径或文件夹目录

判断文件 path 是否为文件,返回 True 或 False;

>>> os.listdir()
['map-location.xlsx', 'Map123.gif', 'recu_dir']
>>> os.path.isfile(os.getcwd() +'/recu_dir')
False
>>> os.path.isfile(os.getcwd() +'/Map123.gif')
True
os.path.isdir(path)
  • path(str),文件路径

判断文件路径 path 知否为文件夹目录,返回 True 或 False;

>>> os.listdir()
>>> os.path.isdir(os.getcwd() +'/recu_dir')
True
os.path.isabs(path)
  • name(str),文件路径

判断文件路径 path 是否为绝对路径,返回 True, 或 False;

绝对路径 表示为完整路径 例如D:/Working/wor.txt相对路径 即表示相对某个文件下的文件路径,例如 ./word.txt

>>> os.path.isabs('D:/Working/1.png')
True
>>> os.path.isabs('a.png')
False
>>>
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员大飞1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值