我是新来的蟒蛇。在这里我的目标是将数据放入zip文件中。以下是我写的代码,其中我将数据写入unzipped_file,然后在zipped_file.zip中写入unzipped_file,然后除去解压缩的文件。如何在python中直接添加文件到zip文件?
import os
import zipfile
##Some code above.............
for some_data in big_data:
with open('unzipped_file', 'a+') as unzipped_f:
unzipped_f.write(some_data)
##Some code in between...........
with zipfile.ZipFile('zipped_file.zip', 'w') as zipped_f:
zipped_f.write("unzipped_file")
os.remove("unzipped_file")
而不是创建一个中间unzipped_file。我可以直接在一个步骤中将数据写入zipped_file。
对不起,错误的沟通,但在这里我试图追加数据到一个unzipped_file,然后将其保存在zipped_file。我已编辑参考代码 – Eudie
@Eudie,请参阅我的编辑 – ForceBru