gpt4 book ai didi

Python - 我正在尝试解压缩一个包含多个 zip 文件的文件

转载 作者:太空宇宙 更新时间:2023-11-03 17:58:45 25 4
gpt4 key购买 nike

我的目标是获取第二层 zip 文件中的 txt 文件。问题是 txt 文件在所有 .zip 中具有相同的名称,因此它会覆盖 .txt 并且只返回 1 .txt

from ftplib import *
import os, shutil, glob, zipfile, xlsxwriter

ftps = FTP_TLS()
ftps.connect(host='8.8.8.8', port=23)
ftps.login(user='xxxxxxx', passwd='xxxxxxx')
print ftps.getwelcome()
print 'Access was granted'
ftps.prot_p()
ftps.cwd('DirectoryINeed')
data = ftps.nlst() #Returns a list of .zip diles
data.sort() #Sorts the thing out
theFile = data[-2] #Its a .zip file #Stores the .zip i need to retrieve
fileSize = ftps.size(theFile) #gets the size of the file
print fileSize, 'bytes' #prints the size

def grabFile():
filename = 'the.zip'
localfile = open(filename, 'wb')
ftps.retrbinary('RETR ' + theFile, localfile.write)
ftps.quit()
localfile.close()

def unzipping():
zip_files = glob.glob('*.zip')
for zip_file in zip_files:
with zipfile.ZipFile(zip_file, 'r')as Z:
Z.extractall('anotherdirectory')

grabFile()
unzipping()
lastUnzip()

运行后,它会获取我需要的 .zip 并将内容提取到名为 anotherdirectory 的文件夹中。它保存第二层 .zip。这就是我遇到麻烦的地方。当我尝试从每个 zip 中提取文件时。他们都有相同的名字。当每个 zip 需要一个 .txt 时,我最终得到一个 .txt。

最佳答案

我认为您每次都指定相同的输出目录和文件名。在解压函数中,

改变

Z.extractall('anotherdirectory')

Z.extractall(zip_file)

Z.extractall('anotherdirectory' + zip_file)

如果 zip_file 全部相同,请为每个输出文件夹指定一个唯一的编号名称:解压前函数:

count = 1

然后将其他代码替换为:

Z.extractall('anotherdirectory/' + str(count))
count += 1

关于Python - 我正在尝试解压缩一个包含多个 zip 文件的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28053489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com