首页 > 其他 > 详细

删除某个目录下的全部文件

时间:2019-01-18 10:49:17      阅读:127      评论:0      收藏:0      [点我收藏+]
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!")

删除某个目录下的全部文件

原文:http://blog.51cto.com/357712148/2343998

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!