下面給大家介紹python 批量解壓壓縮文件的實例代碼,代碼如下所述;
#/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批量解壓
#!/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 批量解壓壓縮文件的實例代碼,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

