python的isdir、lexists、exists、isfile有什么区别

测试代码

import os
from os import path
if __name__ == '__main__':


    truth_pth="run/data/images"
    no_pth="run1/data/images"
    broken_pth="run/images"
    file_pth="run/data/images/1.png"
    nofile_pth = "run/data/images/2.png"

    pths = [truth_pth,no_pth,broken_pth,file_pth,nofile_pth]

    for p in pths:
        print(f"isdir({p})",path.isdir(p))
    for p in pths:
        print(f"lexists({p})",path.lexists(p))
    for p in pths:
        print(f"exists({p})",path.exists(p))
    for p in pths:
        print(f"isfile({p})", path.isfile(p))


 此时的目录

测试结果

isdir(run/data/images) True
isdir(run1/data/images) False
isdir(run/images) False
isdir(run/data/images/1.png) False
isdir(run/data/images/2.png) False
lexists(run/data/images) True
lexists(run1/data/images) False
lexists(run/images) False
lexists(run/data/images/1.png) True
lexists(run/data/images/2.png) False
exists(run/data/images) True
exists(run1/data/images) False
exists(run/images) False
exists(run/data/images/1.png) True
exists(run/data/images/2.png) False
isfile(run/data/images) False
isfile(run1/data/images) False
isfile(run/images) False
isfile(run/data/images/1.png) True
isfile(run/data/images/2.png) False

结论

lexist和exist几乎一样,只要路径存在就可以,不在乎是文件还是目录。

isdir判断是不是已经存在的目录

isfile判断是不是已经存在的文件

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值