刪除某個目錄下的全部文件
阿新 • • 發佈:2019-01-18
for 文件 urn delet mdi pycharm mtr exist import
def delete_file_in_dir(dir):
if not os.path.exists(dir):
return 1
try:
for i in os.listdir(dir):
path_way = os.path.join(dir,i)
#如果是文件就刪除
if os.path.isfile(path_way):
os.remove(path_way)
else:
#如果是文件夾調用方法
delete_file_in_dir(path_way)
else:
#刪除目錄
os.rmdir(dir)
except:
return 1
return 0
def shutildome():
import shutil
filelist = []
rootdir = "D:\\TOOL\\PycharmProjects\\python\\cz\\815\\2018\\10\\20w"
filelist = os.listdir(rootdir)
for f in filelist:
filepath = os.path.join(rootdir, f)
if os.path.isfile(filepath):
os.remove(filepath)
print(filepath + " removed!")
elif os.path.isdir(filepath):
shutil.rmtree(filepath, True)
print("dir " + filepath + " removed!")
刪除某個目錄下的全部文件