2

I have a similar problem to a question over here with the following code:

import zipfile

report_zip = "C:\Users\user\Desktop\report.zip"
report = "C:\Users\user\Desktop\report.json"

json_zip = zipfile.ZipFile(report_zip, "w")
try:
    json_zip.write(report)
finally:
    json_zip.close()

The result of the code is as such:

report.zip
 --Users
   --user
     --Desktop
       --report.json

Whereas what I want is:

report.zip
 --report.json

How should I modify my current implementation to achieve this? Or is there a better implementation? Many thanks in advance.

Community
  • 1
  • 1
Zerhinne
  • 1,987
  • 2
  • 22
  • 43

1 Answers1

3

Pass the arcname parameter to ZipFile.write().

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Doesn't work, the directories are still inside the zipped file – Zerhinne Mar 09 '12 at 07:16
  • According to the [documentation](http://docs.python.org/library/zipfile.html#zipfile.ZipFile.write): `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).` – jcollado Mar 09 '12 at 07:19