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

490次阅读
没有评论

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

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
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-05-06发表,共计482字。
新手QQ群:570568346,欢迎进群讨论 Python51学习