问题描述
我在 Linux 服务器上有很多压缩文件,每个文件都包含多个文本文件.
I have lots of zipped files on a Linux server and each file includes multiple text files.
我想要的是提取其中一些文本文件,这些文件在压缩文件中具有相同的名称并将其保存在一个文件夹中;我正在为每个压缩文件创建一个文件夹并将文本文件解压缩到其中.我需要将父压缩文件夹名称添加到文件名的末尾,并将所有文本文件保存在一个目录中.例如,如果压缩文件夹是 March132017.zip 并且我提取了holding.txt,我的文件名将是 Holding_march13207.txt.
what I want is to extract some of those text files, which have the same name across zipped files and save it a folder; I am creating one folder for each zipped file and extract the text file to it. I need to add the parent zipped folder name to the end of file names and save all text files in one directory. For example, if the zipped folder was March132017.zip and I extracted holding.txt, my filename would be holding_march13207.txt.
我的问题是我无法更改提取文件的名称.如果您能提供建议,我将不胜感激.
My problem is that I am not able to change the extracted file's name.I would appreciate if you could advise.
import os
import sys
import zipfile
os.chdir("/feeds/lipper/emaxx")
pwkwd = "/feeds/lipper/emaxx"
for item in os.listdir(pwkwd): # loop through items in dir
if item.endswith(".zip"): # check for ".zip" extension
file_name = os.path.abspath(item) # get full path of files
fh = open(file_name, "rb")
zip_ref = zipfile.ZipFile(fh)
filelist = 'ISSUERS.TXT' , 'SECMAST.TXT' , 'FUND.TXT' , 'HOLDING.TXT'
for name in filelist :
try:
outpath = "/SCRATCH/emaxx" + "/" + os.path.splitext(item)[0]
zip_ref.extract(name, outpath)
except KeyError:
{}
fh.close()
推荐答案
为什么不直接读取有问题的文件并自行保存而不是提取?类似的东西:
Why not just read the file in question and save it yourself instead of extracting? Something like:
import os
import zipfile
source_dir = "/feeds/lipper/emaxx" # folder with zip files
target_dir = "/SCRATCH/emaxx" # folder to save the extracted files
# Are you sure your files names are capitalized in your zip files?
filelist = ['ISSUERS.TXT', 'SECMAST.TXT', 'FUND.TXT', 'HOLDING.TXT']
for item in os.listdir(source_dir): # loop through items in dir
if item.endswith(".zip"): # check for ".zip" extension
file_path = os.path.join(source_dir, item) # get zip file path
with zipfile.ZipFile(file_path) as zf: # open the zip file
for target_file in filelist: # loop through the list of files to extract
if target_file in zf.namelist(): # check if the file exists in the archive
# generate the desired output name:
target_name = os.path.splitext(target_file)[0] + "_" + os.path.splitext(file_path)[0] + ".txt"
target_path = os.path.join(target_dir, target_name) # output path
with open(target_path, "w") as f: # open the output path for writing
f.write(zf.read(target_file)) # save the contents of the file in it
# next file from the list...
# next zip file...
这篇关于重命名从 zipfile 中提取的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!