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?

woshitom
  • 4,811
  • 8
  • 38
  • 62
  • Does this answer your question? [How to create a zip archive of a directory in Python?](https://stackoverflow.com/questions/1855095/how-to-create-a-zip-archive-of-a-directory-in-python) – Omega Krypton Jan 29 '20 at 14:28

1 Answers1

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')
Community
  • 1
  • 1
Amadan
  • 191,408
  • 23
  • 240
  • 301