天天看点

python 解压文件 重名_python - 从zipfile重命名解压缩的文件 - 堆栈内存溢出

为什么不直接阅读有问题的文件并自己保存而不是提取? 就像是:

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...