Python 技术篇-文件操控:删除本地文件、文件夹。判断文件、文件夹是否存在方法
os.remove() 就是删除文件的
os.removedirs() 就是删除文件夹的
os.path.exists() 用来判断文件或文件夹是否存在
import os
path = "D:\\hello.py"
if(os.path.exists(path)): # 判断文件是否存在
os.remove(path) # 删除文件
path = "D:\\hello"
if(os.path.exists(path)): # 判断文件夹是否存在
os.removedirs(path) # 删除文件夹
喜欢的点个赞❤吧!
各位好,我是csdn的小蓝枣,现在我的博客已经同步到博客园啦,欢迎大家关注哈!也欢迎大家关注我的公众号Py_JSQY!每天会更新有趣的科技文!哈哈!