手册目录
Python 教程
Python 机器学习
Python MySQL
如需删除文件,必须导入 OS 模块,并运行其 os.remove() 函数:
删除文件 "demofile.txt":
import os
os.remove("demofile.txt")
为避免出现错误,您可能需要在尝试删除文件之前检查该文件是否存在:
检查文件是否存在,然后删除它:
import os
if os.path.exists("demofile.txt"):
os.remove("demofile.txt")
else:
print("The file does not exist")
如需删除整个文件夹,请使用 os.rmdir() 方法:
删除文件夹 "myfolder":
import os
os.rmdir("myfolder")
提示:您只能删除空文件夹。
相关视频
科技资讯
24小时阅读榜
1
2
3
4
5
6
7
8
9
10
精品课程
共5课时 | 17.4万人学习
共49课时 | 78.1万人学习
共29课时 | 62.5万人学习
共25课时 | 39.7万人学习
共43课时 | 73.8万人学习