python判断某个文件是否存在

os模块中的os.path.exists()方法用于检验文件是否存在。

  • 判断文件是否存在
import os
os.path.exists(test_file.txt)
#True

os.path.exists(no_exist_file.txt)
#False
  • 判断文件夹是否存在
import os
os.path.exists(test_dir)
#True

os.path.exists(no_exist_dir)
#False
posted @ 2022-03-01 17:40  Tomorrow1126  阅读(97)  评论(0编辑  收藏  举报