我正在使用zipfile,在某些情况下,我需要为占位符目的创建一个空的zip文件.我怎样才能做到这一点?
我知道这个:
在2.7.1版中更改:如果文件是使用模式"a"或"w"创建的,然后在不向归档添加任何文件的情况下关闭,则将为文件写入空归档的相应ZIP结构.
但我的服务器使用较低版本2.6.
小智 9
您可以创建一个空的zip文件,而无需zipfile:
empty_zip_data = '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)
Run Code Online (Sandbox Code Playgroud)
empty_zip_data 是一个空zip文件的数据.
你可以简单地做:
from zipfile import ZipFile
archive_name = 'test_file.zip'
with ZipFile(archive_name, 'w') as file:
pass
Run Code Online (Sandbox Code Playgroud)