使用python删除一个文件或文件夹,须要使用os模块。python
import os
os.remove(path) # path是文件的路径,若是这个路径是一个文件夹,则会抛出OSError的错误,这时需用用rmdir()来删除
os.rmdir(path) # path是文件夹路径,注意文件夹须要时空的才能被删除
os.unlink('F:\新建文本文档.txt') # unlink的功能和remove同样是删除一个文件,可是删除一个删除一个正在使用的文件会报错。
import os path = 'F:/新建文本文档.txt' # 文件路径 if os.path.exists(path): # 若是文件存在 # 删除文件,可以使用如下两种方法。 os.remove(path) #os.unlink(path) else: print('no such file:%s'%my_file) # 则返回文件不存在
import os
os.removedirs(path) # 递归地删除目录。若是子目录成功被删除,则将会成功删除父目录,子目录没成功删除,将抛异常。
import os for root, dirs, files in os.walk(top, topdown=False): for name in files: os.remove(os.path.join(root, name)) for name in dirs: os.rmdir(os.path.join(root, name))
另外一种方法spa
import shutil shutil.rmtree()