我是新來的蟒蛇。在這裏我的目標是將數據放入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