[英]How to add url file to zip
我有一个具有以下结构的zip文件:
my_zip.zip
|-file1.txt
|-folder1/
|-file2.txt
我想将url中的some_file
添加到folder1
。 我知道我可以做类似的事情:
>>> import zipfile
>>> z = zipfile.ZipFile("my_zip.zip", "w")
>>> z.write("some_file")
但是,有两个问题:
some_file
添加到该特定的folder1
? some_file
下载到PC上,然后使用z.write('path/to_my/local/some_file')
吗? 没有办法直接从url到zip吗? 使用ZipFile.writestr( arcname , data )
。
要写入zipfile中的文件夹,只需写文件夹名,就好像写入文件夹中的文件夹一样(So folder1/some_file
)。
import urllib.request
import zipfile
z = zipfile.ZipFile("my_zip.zip", "w")
page = urllib.request.urlopen('http://example.com/') # Change to website
z.writestr('folder1/some_file', page.read())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.