I’m using the ZipFile package to zip file in Python. Here’s my code:
archive = zipfile.ZipFile(join(settings.ARCHIVES_DIR, 'test.zip'), "a")
for pdffile in glob.glob(join(settings.IBILLING_DIR, '*.pdf')):
archive.write(pdffile)
archive.close()
The issue I’m facing is that the ZIP file that is created, contains a directory structure. The files that are added are added with the full the path which means that the user that extracts the archive, will also end up getting a directory structure. I’d like to the add files to the ZIP but without any directory structure.
How can I do this? I didn’t find this information in the docs.
Thanks
First add
then modify the
archive.writeline to be:This specifies that each pdf should be written into the zip file with a path equivalent to only the filename portion of the path from which you are reading it (by specifying the arcname parameter for the
write()call).Note, however, that this means that if you have two PDF files with the same name in different directories, one will overwrite the other.