![](/img/trans.png)
[英]How do I create a zip file of a file path using Python, including empty directories?
[英]How to create an empty zip file?
我正在使用zipfile
,在某些情況下,我需要為某些占位符目的創建一個空的 zip 文件。 我怎樣才能做到這一點?
我知道這個:
在 2.7.1 版更改:如果文件是使用模式 'a' 或 'w' 創建的,然后關閉而不向存檔添加任何文件,則空存檔的相應 ZIP 結構將寫入文件。
但我的服務器使用的版本較低,為 2.6。
您可以創建一個空的 zip 文件而無需將zipfile
壓縮為:
empty_zip_data = b'PK\x05\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
with open('empty.zip', 'wb') as zip:
zip.write(empty_zip_data)
empty_zip_data
是一個空 zip 文件的數據。
你可以簡單地做:
from zipfile import ZipFile
archive_name = 'test_file.zip'
with ZipFile(archive_name, 'w') as file:
pass
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.