python学习笔记,23——python压缩bin包

说明(2017-12-25 10:43:20):

1. CZ写的压缩bin包代码,记下来以后好抄。

 1 # coding:utf-8
 2 '''
 3 Created on 2014年8月14日
 4 
 5 @author: johnsoncheng
 6 '''
 7 
 8 import zipfile, os
 9 
10 IGNORE_POSTFIX_LIST = {".mp3", ".wav", ".jpg", ".JPG", ".wma", ".png", ".mp4", ".avi", ".wmv"}
11 
12 
13 def zipFolder():
14     # srcFolder = r"E:\XZJYRootFolder\root\Math_Video"
15     # tarFile = r"E:\XZJYRootFolder\root\Data\Math_Video.zip"
16     rootFolder = r"D:\软件发布\压缩根目录"
17     #rootFolder = r"D:\XZJYRootFolder\root\Data1\res"
18     # 遍历所有文件夹
19     folderList = os.listdir(rootFolder)
20     for folder in folderList:
21         folderPath = os.path.join(rootFolder, folder)
22         if os.path.isdir(folderPath):
23             # 取得二级目录
24             folderList1 = os.listdir(folderPath)
25             for folder1 in folderList1:
26                 folderPath1 = os.path.join(folderPath, folder1)
27                 if os.path.isdir(folderPath1):
28                     zipPath = os.path.join(folderPath, folder1 + ".bin")
29                     with zipfile.ZipFile(zipPath, 'w', allowZip64=True) as myzip:
30                         zipSubFolder(myzip, folderPath1, folderPath)
31     pass
32 
33 def zipSubFolder(zipObj, folderPath, rootFolderPath):
34     global IGNORE_POSTFIX_LIST
35     print("compressing " + folderPath)
36     # 取得当前文件夹差异路径
37     folderRelativePath = folderPath[len(rootFolderPath): ]
38     # 插入当前文件夹
39     zipObj.write(folderPath, folderRelativePath)
40     # 遍历所有子实体
41     entryList = os.listdir(folderPath)
42     # 处理所有子文件夹
43     for entry in entryList:
44         entryPath = os.path.join(folderPath, entry)
45         if os.path.isdir(entryPath):
46             zipSubFolder(zipObj, entryPath, rootFolderPath)
47     # 处理子文件
48     for entry in entryList:
49         entryPath = os.path.join(folderPath, entry)
50         if os.path.isfile(entryPath):
51             fileRelativePath = folderPath[len(rootFolderPath): ] + "/" + entry
52             tempRoot, tempPostFix = os.path.splitext(fileRelativePath)
53             if tempPostFix in IGNORE_POSTFIX_LIST:
54                 zipObj.write(entryPath, fileRelativePath)
55             else:
56                 zipObj.write(entryPath, fileRelativePath, zipfile.ZIP_DEFLATED)
57     pass
58 
59 
60 if __name__ == '__main__':
61     print("************** start *************")
62     zipFolder()
63     print("************** end *************")
64     pass