Python删除文件及进行文件夹压缩

本文介绍了一个使用Python脚本自动删除项目中不需要的配置文件,并将剩余文件压缩为ZIP格式进行发布的流程。该脚本适用于软件项目的编译后处理,通过定制化的文件筛选和打包过程,提高部署效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

示例效果:

项目编译发布后,删除部分配置文件,然后做成发布文件的压缩包。

 

# -*- coding: UTF-8 -*-
import os,sys  
import zipfile
import datetime,time

def 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值