def del_(rootdir): filelist = [] filelist = os.listdir(rootdir) # 列出该目录下的所有文件名 for f in filelist: filepath = os.path.join(rootdir, f) # 将文件名映射成绝对路劲 if os.path.isfile(filepath): # 判断该文件是否为文件或者文件夹 os.remove(filepath) # 若为文件,则直接删除 elif os.path.isdir(filepath): shutil.rmtree(filepath, True) # 若为文件夹,则删除该文件夹及文件夹内所有文件 shutil.rmtree(rootdir, True) def zipDir(dirpath,outFullName): zip = zipfile.ZipFile(outFullName,"w",zipfile.ZIP_DEFLATED) for path,dirnames,filenames in os.walk(dirpath): # 去掉目标跟路径,只对目标文件夹下边的文件及文件夹进行压缩 fpath = path.replace(dirpath,'') for filename in filenames: zip.write(os.path.join(path,filename),os.path.join(fpath,filename)) zip.close() |