Skip to content
Advertisement

zip file and avoid directory structure

I have a Python script that zips a file (new.txt):

JavaScript

When I do this I get the zip file. But when I try to unzip the file I get the text file inside of a series of directories corresponding to the path of the file i.e I see a folder called root in the result directory and more directories within it, i.e. I have

JavaScript

and when I unzip new.zip I have a directory structure that looks like

JavaScript

Is there a way I can zip such that when I unzip I only get new.txt?

In other words I have /root/files/result/new.zip and when I unzip new.zip, it should look like

JavaScript

Advertisement

Answer

The zipfile.write() method takes an optional arcname argument that specifies what the name of the file should be inside the zipfile

I think you need to do a modification for the destination, otherwise it will duplicate the directory. Use :arcname to avoid it. try like this:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement