2016-05-29 41 views
0

我写了下面的代码,以压缩bin_file_pathpython zipfile模块:如何更改创建的zip压缩文件的结构?

zf = zipfile.ZipFile(file_to_search, mode='w') 
zf.write(bin_file_path) 
zf.close() 

如果bin_file_path是例如:\ DIR1 \ DIR2 \ bin_file,然后当我解压创建的ZIP文件,我得到了一个名为“ dir1“,在另一个名为”dir2“的目录中,只在”dir2“中,我会得到bin_file。

我想创建的zip文件直接包含bin_file并且不在sub_directories内。你有一个想法如何做到这一点?

回答

1

您可以使用

zf = zipfile.ZipFile(file_to_search, mode='w') 
zf.write(bin_file_path, custom_name) 
zf.close() 

其中custom_name可以是任何东西,包括os.path.basename(bin_file_path)

相关问题