检查Python脚本中存在的文件

是否有任何方法可以检查Python脚本中的文件是否存在。

# 回答1

导入os模块并使用os.path。isfile(path)函数
# 回答2

Python提供了exists()函数,可以如下使用。

选择 | 换行 | 行号
  1. import os.path  
  2.  
  3. if(os.path.exists("/etc/passwd")):  
  4.  
  5.    print("/etc/passwd exists")

标签: python

添加新评论