I am doing:
z = zipfile.ZipFile('myzip.zip', 'w')
z.write('/some/path/mytxt1.txt')
z.write('/some/other/path/mytxt2.txt')
z.close()
This is preserving the file paths within the zip. I just want my desired files to sit flat in the zip file. How can I do this?
ZipFile.write() takes the second argument, arcname. Just set it to os.path.basename() of the first argument to remove the path:
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With