品易云推流 关闭
文章详情页
文章 > Python基础教程 > python检测给定的路径是否存在的方法

python检测给定的路径是否存在的方法

头像

宋雪维

2021-02-22 16:51:282884浏览 · 0收藏 · 0评论

os.path 模块提供了函数用于检测给定的路径是否存在

1、path 参数所指的文件或文件夹存在,调用os.path.exists(path) 返回 True,否则返回 False

>>> os.path.exists('C:\\Windows')
True

>>> os.path.exists('C:\\Win')
False

2、path 参数存在并且是一个文件,调用 os.path.isfile(path) 返回 True,否则返回 False

>>> os.path.isfile('C:\\Windows\\System32')
False

>>> os.path.isfile('C:\\Windows\\System32\\C_20280.NLS')
True

3、path 参数存在并且是一个文件夹,调用 os.path.isdir(path) 返回 True,否则返回 False

>>> os.path.isdir('C:\\Windows\\System32\\C_20280.NLS')
False

>>> os.path.isdir('C:\\Windows\\System32')
True
关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图