我在做:
z = zipfile.ZipFile('myzip.zip', 'w')
z.write('/some/path/mytxt1.txt')
z.write('/some/other/path/mytxt2.txt')
z.close()
Run Code Online (Sandbox Code Playgroud)
这是在zip文件中保留文件路径。我只希望将所需文件放在zip文件中。我怎样才能做到这一点?
ZipFile.write()接受第二个参数arcname。只需将其设置os.path.basename()为第一个参数即可删除路径:
def zip_write(zip, filename):
zip.write(filename, os.path.basename(filename))
z = zipfile.ZipFile('myzip.zip', 'w')
zip_write(z, '/some/path/mytxt1.txt')
zip_write(z, '/some/other/path/mytxt2.txt')
z.close()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
681 次 |
| 最近记录: |