python检测给定的路径是否存在的方法
发布时间:2024-09-04 20:06:25

os.path 该模块提供了检测给定路径是否存在的函数
1、path 参数所指的文件或文件夹存在,并调用os.path.exists(path) 返回 True,否则返回 False
>>>os.path.exists('C:\\Windows')
True
>>>os.path.exists('C:\\Win')
False2、path 参数存在,是一个文件,调用 os.path.isfile(path) 返回 True,否则返回 False
>>>os.path.isfile('C:\\Windows\System32')
False
>>>os.path.isfile('C:\\WindowsSystem32\C_20280.NLS')
True3、path 参数存在,是文件夹,调用 os.path.isdir(path) 返回 True,否则返回 False
>>>os.path.isdir('C:\\WindowsSystem32\C_20280.NLS')
False
>>>os.path.isdir('C:\\Windows\System32')
True 