删除文件
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资源
猜你喜欢
- 2019-02-22
- Python不行了?
- 2019-05-07
- Python如何提取url中的参数
- 2015-12-16
- Python列表对象实现原理
- 2020-06-07
- python合并两个字典
- 2020-06-14
- python中如何将字符串转换为整数
- 2017-07-19
- 如何正确理解@classmethod与@staticmethod
- 2020-06-13
- python 中 xml 转换为 json
- 2014-02-10
- python 函数式编程处理函数:map(),filter(),reduce()
- 2020-06-13
- 检查对象中是否存在某个属性
- 2020-06-04
- object is not subscriptable 是什么错误