下面给大家介绍python 批量解压压缩文件的实例代码,代码如下所述;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#/usr/bin/python#coding=utf-8import os,sys import zipfile open_path = 'e:\\data' save_path = 'e:\\data' os.chdir(open_path) #转到路径 #首先,通过zipfile模块打开指定位置zip文件 #传入文件名列表,及列表文件所在路径,及存储路径def Decompression(files,file_path,save_path): os.getcwd() #当前路径 os.chdir(file_path)#转到路径 for file_name in files: print (file_name) r = zipfile.is_zipfile(file_name) #判断是否解压文件 if r: zpfd = zipfile.ZipFile(file_name) #读取压缩文件 os.chdir(save_path) #转到存储路径 zpfd.extractall() zpfd.close() def files_save(open_path): for file_path,sub_dirs,files in os.walk(open_path): #获取所有文件名,路径 print (file_path,sub_dirs,files) Decompression(files,file_path,save_path)files_save(open_path) |
在看下一段代码python批量解压
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
#!/usr/bin/python # -*- coding: UTF-8 -*- '''Created on Jun 6, 2019 @author: carson ''' import os import re import zipfile import StringIO # 下述三行是为了解决编码问题 import sys reload (sys) sys.setdefaultencoding( 'utf8' ) file_path = r '/Users/qcq/Downloads/bills' file_out = r '/Users/qcq/Downloads/qcq.txt' # 正则表达式匹配基本话费,信息费,长途费,三个条目。 pattern = re. compile (r '\d+\.\d+' ) phone_number_line = 1 #标记文件的第一行是电话号码行 real_bill_line = 7 # 正文开始 ''' 1. 代码第一部分,首先迭代给定的文件目录,取得需要处理的zip文件,存储在一个列表里面,为后边的文件处理服务。此处主要是使用os.walk来迭代目录,然后使用os.path.join连接两个目录。 ''' file_name_list = [] for dirpath, dirnames, filenames in os.walk(file_path): for file_name in filenames: if file_name.endswith( '.zip' ): temp_path = os.path.join(dirpath, file_name) file_name_list.append(temp_path) ''' 2. 对获取到的上述文件,进行了排序使输出的顺序有序。 ''' sorted (file_name_list) ''' 3. 正文部分 ''' with open (file_out, 'w' ) as f_out: for zip_file in file_name_list: with zipfile.ZipFile(zip_file) as f: data = {} for nameOfFileUnderZip in f.namelist(): count = 1 contents = StringIO.StringIO(f.read(nameOfFileUnderZip)) sum_all = 0.0 for line in contents: if count > phone_number_line and count < real_bill_line: count + = 1 continue if phone_number_line = = count: phone_number = line.split(u ':' )[ 1 ] count + = 1 continue sum_all + = sum ( map ( float , pattern.findall(line))) data[phone_number.strip()] = sum_all f_out.write(zip_file + '\n' ) for key, value in sorted (data.items(), key = lambda d:d[ 0 ]) : f_out.write(key + ':' + str (value) + '\n' ) ############################################################################## #coding=utf-8 #甄码农python代码 #使用zipfile做目录压缩,解压缩功能 import os,os.path import zipfile def zip_dir(dirname,zipfilename): filelist = [] if os.path.isfile(dirname): filelist.append(dirname) else : for root, dirs, files in os.walk(dirname): for name in files: filelist.append(os.path.join(root, name)) zf = zipfile.ZipFile(zipfilename, "w" , zipfile.zlib.DEFLATED) for tar in filelist: arcname = tar[ len (dirname):] #print arcname zf.write(tar,arcname) zf.close() def unzip_file(zipfilename, unziptodir): if not os.path.exists(unziptodir): os.mkdir(unziptodir, 0777 ) zfobj = zipfile.ZipFile(zipfilename) for name in zfobj.namelist(): name = name.replace( '\\',' / ') if name.endswith( '/' ): os.mkdir(os.path.join(unziptodir, name)) else : ext_filename = os.path.join(unziptodir, name) ext_dir = os.path.dirname(ext_filename) if not os.path.exists(ext_dir) : os.mkdir(ext_dir, 0777 ) outfile = open (ext_filename, 'wb' ) outfile.write(zfobj.read(name)) outfile.close() if __name__ = = '__main__' : zip_dir(r 'E:/python/learning' ,r 'E:/python/learning/zip.zip' ) unzip_file(r 'E:/python/learning/zip.zip' ,r 'E:/python/learning2' ) |
总结
以上所述是小编给大家介绍的python 批量解压压缩文件的实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
原文链接:https://www.cnblogs.com/niuchen/p/6603526.html