Python 如何将一个 zip 文件复制到内存中的另一个 zip 文件?
Python How to copy files inside a zip to another zip in memory?
目的
将 zip 存档拆分为更小的 zip 存档,每个新 zip 的文件数量均匀分布。
例子
源 zip(100 个文件)
- src/100-Test.zip
目标 zip(每个 25 个文件):
- destination/1.zip
- destination/2.zip
- destination/3.zip
- destination/4.zip
描述
所以我已经能够打开 zip 文件并遍历内容以将它们拆分,但我无法写入文件。因为我没有对 zip 内容做任何事情,所以我认为我不必做任何 StringIO 之类的事情?
代码
zipFileNameSrc = '100-Test.zip'
zipFile = open(zipFileNameSrc)
unzippedFile = zipfile.ZipFile(zipFile)
imgList = [(s, unzippedFile.read(s)) for s in unzippedFile.namelist() if (".jpg" or ".JPG") in s]
#image names: imgList[i][0] and images: imgList[i][1]
#...
#...additional logic to split into sets of 25 images
#...fileTuplesList = imgList[:25]
zipNo = 1
#zipFileDest = destination + "/" + zipSrcNm + "/" + zipNo.__str__() + ".zip"
zipFileName = zipNo.__str__() + ".zip"
zipOut = zipfile.ZipFile(zipFileName, 'w')
for i in xrange(len(fileTuplesList)):
fileNameAndPath = fileTuplesList[i][0]
actualFile = fileTuplesList[i][1]
zipOut.write(fileNameAndPath, actualFile)
zipOut.close()
#move_files(zipFileName, zipFileDest)
错误
我上线了zipOut.write(fileNameAndPath, actualFile)
OSError: [Errno 2] No such file or directory: '100-Test/17.jpg'
奖金
如何将 zip 文件保存到与我的脚本所在的文件夹不同的文件夹?
ZipFile.write()
需要一个文件名作为第一个参数,并且该文件应该存在于系统中。如果是,则将该特定文件复制到 zip 存档中。
你实际上想使用 - ZipFile.writestr()
- 它期望存档名作为第一个参数,数据作为第二个参数。
此外,您可以在任何地方创建 zip 存档,只需在创建 zipFileName
时使用 os.path.join()
将目录加入 zip 文件名。做你想做的示例代码 -
import os.path
zipFileNameSrc = '100-Test.zip'
zipFile = open(zipFileNameSrc)
unzippedFile = zipfile.ZipFile(zipFile)
imgList = [(s, unzippedFile.read(s)) for s in unzippedFile.namelist() if (".jpg" or ".JPG") in s]
#image names: imgList[i][0] and images: imgList[i][1]
#...
#...additional logic to split into sets of 25 images
#...fileTuplesList = imgList[:25]
zipNo = 1
#zipFileDest = destination + "/" + zipSrcNm + "/" + zipNo.__str__() + ".zip"
zipFileName = os.path.join('<directory for zip>',zipNo.__str__() + ".zip")
zipOut = zipfile.ZipFile(zipFileName, 'w')
for i in xrange(len(fileTuplesList)):
fileNameAndPath = fileTuplesList[i][0]
actualFile = fileTuplesList[i][1]
zipOut.writestr(fileNameAndPath, actualFile)
zipOut.close()
Example/Demo 在我的系统中有效的代码 -
import zipfile
import os.path
zipFileNameSrc = 'ziptest.zip'
zipFile = open(zipFileNameSrc, 'rb')
unzippedFile = zipfile.ZipFile(zipFile)
imgList = [(s, unzippedFile.read(s)) for s in unzippedFile.namelist() if (".png" or ".PNG")]
for i in range(1,5):
zipFileName = os.path.join('<some location>','ziptest_' + str(i) + '.zip')
print('Creating ', zipFileName)
zipOut = zipfile.ZipFile(zipFileName, 'w')
for j in range(25):
ind = (i-1)*25 + j
fileNameAndPath = imgList[ind][0]
actualFile = imgList[ind][1]
zipOut.writestr(fileNameAndPath, actualFile)
zipOut.close()
您的 zipOut.write() 参数倒退了。第一个参数是你想写的文件,第二个参数是你想给文件起的名字(你也可以留空,它只会使用自己的文件名)。
fileNameAndPath = fileTuplesList[i][0]
actualFile = fileTuplesList[i][1]
zipOut.write(fileNameAndPath, actualFile)
https://docs.python.org/3.4/library/zipfile.html#zipfile.ZipFile.write
ZipFile.write(filename, arcname=None, compress_type=None)
将名为 filename 的文件写入存档,为其指定存档名称 arcname(默认情况下,这将与 filename 相同,但没有驱动器号并且删除了前导路径分隔符)。如果给定,compress_type 会覆盖为新条目的构造函数提供的压缩参数的值。存档必须以 'w' 或 'a' 模式打开——在以 'r' 模式创建的 ZipFile 上调用 write() 将引发 RuntimeError。在关闭的 ZipFile 上调用 write() 将引发 RuntimeError。
目的
将 zip 存档拆分为更小的 zip 存档,每个新 zip 的文件数量均匀分布。
例子
源 zip(100 个文件)
- src/100-Test.zip
目标 zip(每个 25 个文件):
- destination/1.zip
- destination/2.zip
- destination/3.zip
- destination/4.zip
描述
所以我已经能够打开 zip 文件并遍历内容以将它们拆分,但我无法写入文件。因为我没有对 zip 内容做任何事情,所以我认为我不必做任何 StringIO 之类的事情?
代码
zipFileNameSrc = '100-Test.zip'
zipFile = open(zipFileNameSrc)
unzippedFile = zipfile.ZipFile(zipFile)
imgList = [(s, unzippedFile.read(s)) for s in unzippedFile.namelist() if (".jpg" or ".JPG") in s]
#image names: imgList[i][0] and images: imgList[i][1]
#...
#...additional logic to split into sets of 25 images
#...fileTuplesList = imgList[:25]
zipNo = 1
#zipFileDest = destination + "/" + zipSrcNm + "/" + zipNo.__str__() + ".zip"
zipFileName = zipNo.__str__() + ".zip"
zipOut = zipfile.ZipFile(zipFileName, 'w')
for i in xrange(len(fileTuplesList)):
fileNameAndPath = fileTuplesList[i][0]
actualFile = fileTuplesList[i][1]
zipOut.write(fileNameAndPath, actualFile)
zipOut.close()
#move_files(zipFileName, zipFileDest)
错误
我上线了zipOut.write(fileNameAndPath, actualFile)
OSError: [Errno 2] No such file or directory: '100-Test/17.jpg'
奖金
如何将 zip 文件保存到与我的脚本所在的文件夹不同的文件夹?
ZipFile.write()
需要一个文件名作为第一个参数,并且该文件应该存在于系统中。如果是,则将该特定文件复制到 zip 存档中。
你实际上想使用 - ZipFile.writestr()
- 它期望存档名作为第一个参数,数据作为第二个参数。
此外,您可以在任何地方创建 zip 存档,只需在创建 zipFileName
时使用 os.path.join()
将目录加入 zip 文件名。做你想做的示例代码 -
import os.path
zipFileNameSrc = '100-Test.zip'
zipFile = open(zipFileNameSrc)
unzippedFile = zipfile.ZipFile(zipFile)
imgList = [(s, unzippedFile.read(s)) for s in unzippedFile.namelist() if (".jpg" or ".JPG") in s]
#image names: imgList[i][0] and images: imgList[i][1]
#...
#...additional logic to split into sets of 25 images
#...fileTuplesList = imgList[:25]
zipNo = 1
#zipFileDest = destination + "/" + zipSrcNm + "/" + zipNo.__str__() + ".zip"
zipFileName = os.path.join('<directory for zip>',zipNo.__str__() + ".zip")
zipOut = zipfile.ZipFile(zipFileName, 'w')
for i in xrange(len(fileTuplesList)):
fileNameAndPath = fileTuplesList[i][0]
actualFile = fileTuplesList[i][1]
zipOut.writestr(fileNameAndPath, actualFile)
zipOut.close()
Example/Demo 在我的系统中有效的代码 -
import zipfile
import os.path
zipFileNameSrc = 'ziptest.zip'
zipFile = open(zipFileNameSrc, 'rb')
unzippedFile = zipfile.ZipFile(zipFile)
imgList = [(s, unzippedFile.read(s)) for s in unzippedFile.namelist() if (".png" or ".PNG")]
for i in range(1,5):
zipFileName = os.path.join('<some location>','ziptest_' + str(i) + '.zip')
print('Creating ', zipFileName)
zipOut = zipfile.ZipFile(zipFileName, 'w')
for j in range(25):
ind = (i-1)*25 + j
fileNameAndPath = imgList[ind][0]
actualFile = imgList[ind][1]
zipOut.writestr(fileNameAndPath, actualFile)
zipOut.close()
您的 zipOut.write() 参数倒退了。第一个参数是你想写的文件,第二个参数是你想给文件起的名字(你也可以留空,它只会使用自己的文件名)。
fileNameAndPath = fileTuplesList[i][0]
actualFile = fileTuplesList[i][1]
zipOut.write(fileNameAndPath, actualFile)
https://docs.python.org/3.4/library/zipfile.html#zipfile.ZipFile.write
ZipFile.write(filename, arcname=None, compress_type=None)
将名为 filename 的文件写入存档,为其指定存档名称 arcname(默认情况下,这将与 filename 相同,但没有驱动器号并且删除了前导路径分隔符)。如果给定,compress_type 会覆盖为新条目的构造函数提供的压缩参数的值。存档必须以 'w' 或 'a' 模式打开——在以 'r' 模式创建的 ZipFile 上调用 write() 将引发 RuntimeError。在关闭的 ZipFile 上调用 write() 将引发 RuntimeError。