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")

 

posted on 2021-07-15 09:00  让代码改变世界ha  阅读(732)  评论(0编辑  收藏  举报

导航