python 检查文件是否存在和删除文件

1.检查文件是否存在

os.path.exists("exercise_1.py")

2.删除文件

os.remove('1.txt')

3.检查文件是否存在并删除

import os
if os.path.exists("demofile.txt"):
  os.remove("demofile.txt")
else:
  print("The file does not exist")

原文地址:https://www.cnblogs.com/hkj8808/p/15013832.html