ITPub博客

首页 > IT职业 > IT图书 > python检测给定的路径是否存在的方法

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

IT图书 作者:tonydeck 时间:2021-09-11 09:59:35 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

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4615/viewspace-2830671/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-13

  • 博文量
    84
  • 访问量
    961