#-*- coding:gbk -*- import os import sys import shutil import zipfile abspath = os.path.abspath(os.path.dirname(sys.argv[0])) extract_file_path = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]),'extract-files-config.txt')) def copyfiles(desdir, filename, patchname): with open(extract_file_path, 'r', encoding='utf-8') as filecfg: for line in filecfg: srcpath = filename + line.strip() if os.path.isfile(srcpath): if srcpath.find('README') == -1: #非README文件照原来路径存放 filepath = os.path.dirname(line) despath = desdir + '\' + patchname + '\' + filepath if not os.path.isdir(despath): #路径不存在则创建文件夹路径 os.makedirs(despath) shutil.copy(srcpath, despath) else: #README文件存放文件第一级目录下 despath = desdir + '\' + patchname if not os.path.isdir(despath): os.makedirs(despath) shutil.copy(srcpath, despath) def zippingfile(currentpath, patchname): zipname = currentpath + '\' + patchname + '.zip' z = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED) startdir = currentpath + '\' + patchname for dirpath, dirnames, filenames in os.walk(startdir): fpath = dirpath.replace(startdir, '') #这一句很重要,不replace的话,就从根目录开始复制,且replace中的startdir不能写成dirpath,否则所有文件在压缩包内不再具有原目录结构而是都在第一级目录下 fpath = fpath and fpath + os.sep or '' #这句话不理解 for filename in filenames: z.write(os.path.join(dirpath, filename), os.path.join(fpath, filename)) z.close() if __name__ == '__main__': filename = abspath + '\..\..\..\' desdir = 'e:3' patchname = '321' copyfiles(desdir, filename, patchname) #从需要抽取的文件配置.txt中找到文件并拷贝到指定位置,包括文件夹结构 zippingfile(desdir, patchname) #对抽取完的文件夹进行打包.zip
从指定文件配置中找到文件copy到指定目录并完成打包.zip
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)