python批量解压文件_python 批量解压压缩文件的实例代码
下面給大家介紹python 批量解壓壓縮文件的實(shí)例代碼,代碼如下所述;
#/usr/bin/python#coding=utf-8import os,sys
import zipfile open_path='e:\\data'save_path='e:\\data' os.chdir(open_path)
#轉(zhuǎn)到路徑
#首先,通過(guò)zipfile模塊打開(kāi)指定位置zip文件
#傳入文件名列表,及列表文件所在路徑,及存儲(chǔ)路徑def Decompression(files,file_path,save_path):
os.getcwd()#當(dāng)前路徑 os.chdir(file_path)#轉(zhuǎn)到路徑
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)#轉(zhuǎn)到存儲(chǔ)路徑
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
# 下述三行是為了解決編碼問(wèn)題
import sys
reload(sys)
sys.setdefaultencoding('utf8')
file_path = r'/Users/qcq/Downloads/bills'
file_out = r'/Users/qcq/Downloads/qcq.txt'
# 正則表達(dá)式匹配基本話(huà)費(fèi),信息費(fèi),長(zhǎng)途費(fèi),三個(gè)條目。
pattern = re.compile(r'\d+\.\d+')
phone_number_line = 1 #標(biāo)記文件的第一行是電話(huà)號(hào)碼行
real_bill_line = 7 # 正文開(kāi)始
'''
1. 代碼第一部分,首先迭代給定的文件目錄,取得需要處理的zip文件,存儲(chǔ)在一個(gè)列表里面,為后邊的文件處理服務(wù)。此處主要是使用os.walk來(lái)迭代目錄,然后使用os.path.join連接兩個(gè)目錄。
'''
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. 對(duì)獲取到的上述文件,進(jìn)行了排序使輸出的順序有序。
'''
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
#甄碼農(nóng)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')
總結(jié)
以上所述是小編給大家介紹的python 批量解壓壓縮文件的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
總結(jié)
以上是生活随笔為你收集整理的python批量解压文件_python 批量解压压缩文件的实例代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 内存条新宠,带宽飞跃游戏畅快
- 下一篇: 苹果7内存手机:外观酷炫,内存大容量,性