删除文件
import os
os.remove("1.txt")
如果文件不存在会报错,所有最好是先判断问价是否存在
import os
if os.path.isfile("1.txt"):
os.remove("1.txt")
或者使用pathlib模块中的方法
>>> from pathlib import Path
>>> f = Path("2.txt")
>>> if f.exists():
... f.unlink()
有问题可以扫描二维码和我交流
关注公众号「Python之禅」,回复「1024」免费获取Python资源
