背景故事:
只是今天在敲代码做测试的时候,每一次代码都会生成一个文件夹,但是自己的代码又需要反复测试,每次都需要重新去删除代码,感觉特别麻烦。就抄抄摘摘弄了个函数。
Python
def del_DirorFile(Dir_or_File):
"""
Func: Delete a file or directory (including subfiles and directories)
:param
Dir_or_File : File or directory path
"""
import os
import shutil
if os.path.isfile(Dir_or_File):
os.remove(Dir_or_File)
print(Dir_or_File + " was removed!")
elif os.path.isdir(Dir_or_File):
shutil.rmtree(Dir_or_File, True)
print("Directory: " + Dir_or_File + " was removed!")
else:
print("Delete error")
不过慎用,这个方法没有设置删除到回收站,会直接移除!