0

I'm trying to zip a file in python, my code is:

import zipfile
f = zipfile.ZipFile('/home/tom/Desktop/test.csv.zip','w',zipfile.ZIP_DEFLATED)
f.write('/home/tom/Desktop/test.csv')
f.close()

It's working well, but inside my zip file I'm having the full path: /home/tom/Desktop/test.csv

How can I just get test.csv inside the zip file?

1

1 Answer 1

2

From docs:

ZipFile.write(filename[, arcname[, compress_type]])

Write the file named filename to the archive, giving it the archive name arcname (by default, this will be the same as filename, but without a drive letter and with leading path separators removed).

So...

f.write('/home/tom/Desktop/test.csv', 'test.csv')

Not the answer you're looking for? Browse other questions tagged or ask your own question.