Python删除文件及进行文件夹压缩
生活随笔
收集整理的這篇文章主要介紹了
Python删除文件及进行文件夹压缩
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
示例效果:
項目編譯發布后,刪除部分配置文件,然后做成發布文件的壓縮包。
?
# -*- coding: UTF-8 -*- import os,sys import zipfile import datetime,timedef getToday_yyyyMMdd():#return time.strftime("%Y%m%d %H:%M:%S",time.localtime(time.time()))return time.strftime("%Y%m%d",time.localtime(time.time()))def remove_noneed_files(startdir):if(os.path.exists(startdir+"\\appsettings.json")):os.remove(startdir+"\\appsettings.json")#if(os.path.exists(startdir+"\\nlog.config")):#os.remove(startdir+"\\nlog.config")if(os.path.exists(startdir+"\\nlog.Development.config")):os.remove(startdir+"\\nlog.Development.config")#if(os.path.exists(startdir+"\\web.config")):#os.remove(startdir+"\\web.config")def zip_yasuo(startdir,file_news):z = zipfile.ZipFile(file_news,'w',zipfile.ZIP_DEFLATED) for dirpath, dirnames, filenames in os.walk(startdir):fpath = dirpath.replace(startdir,'')fpath = fpath and fpath + os.sep or ''for filename in filenames:z.write(os.path.join(dirpath, filename),fpath+filename)z.close()if __name__=="__main__":print("run start")startdir = "D:\\Projects\\Deploy" file_news = 'C:\\Users\admin\\Desktop\\Deploy'+getToday_yyyyMMdd()+'.zip' remove_noneed_files(startdir)zip_yasuo(startdir,file_news)print("run finished")#os.system('pause')?
轉載于:https://www.cnblogs.com/freeliver54/p/9034903.html
總結
以上是生活随笔為你收集整理的Python删除文件及进行文件夹压缩的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hive工作记录-20180513
- 下一篇: HashMap底层原理分析(put、ge