python检测给定的路径是否存在的方法
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
以上就是python检测给定的路径是否存在的方法,希望能对大家有所帮助。更多Python学习教程请关注IT培训机构:开发教育。
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!