【发布时间】:2015-09-18 00:29:21
【问题描述】:
如何压缩“飞”生成的一堆文件?
我正在用这个小规模的场景进行测试:
from time import strftime
import zipfile
# create new file
today = strftime("%Y-%m-%d %H:%M:%S")
new_file = open('testing123.txt', 'w')
text = 'this file was just added:' + str(today)
new_file.write(text)
# create zip file and write to it
newZip = zipfile.ZipFile("test.zip", "w")
newZip.write('testing123.txt')
new_file.close()
print "file created"
两件事,首先在脚本顶部创建的testing123.txt 在您解压缩创建的 zip 文件时是空白的,这应该包括循环生成的文件列表。
所以我想知道什么是动态生成一堆文件的最佳方法,然后将它们全部压缩到一个 zip 文件夹中。
【问题讨论】:
-
你应该在开始压缩之前先
new_file.close() -
你可以打开拉链并继续添加
标签: python python-2.7 zipfile