Liyu He
Liyu He

Reputation: 43

How to zip the contents of a whole directory?

I'm using python 2.7 and try to archive a directory: the object is to: archive the whole folder's content subject: C:\User\blah\blah\parentfolder

structure:C...parentfolder\ 1.docx
                            2.xlxs
                            4.pdf
                            subfolder\5.pdf
                                      6.txt

Now I know that shutil.make_archive() can zip me the folder, but it turned out to be like this

C:\\User\\blah\\blah\\zipfile_name\\parentfolder\ 1.docx
                                                  2.xlxs
                                                  4.pdf
                                                  subfolder\5.pdf
                                                            6.txt 

but I want a to skip the parentfolder layer in the zipfile_name folder, that is:(This is where the problem is different, I did learn shutil.make_archive from that question, but as I open the shutil module, I found out that make_archive has four intakes. I didn't know what makes them different and used the four input one, that's where I had my question. The previous one didn't not point out why it used just three inputs, and the other answers were using very complicated self-defined functions or so. The answer I chose in this problem compared and explained the difference, which I really appreciate. As a non-computer science majored, I think this kind of details help a lot of people.)

C:\\User\\blah\\blah\\zipfile_name\\ 1.docx
                                     2.xlxs
                                     4.pdf
                                     subfolder\5.pdf
                                               6.txt 

if I use os.path.walk and so on to write every file to zipfile_name, it seems it will be super complicated, as I will need to first 1.list everything in the folder--while:1 file?--write into zip folder while:0 directory?--make_directory , and list all items, and while loop again this process( do until no directory exists in the last directory)

Plus I don't know how to make a directory in a .zip folder. So all this just seems complicated.

So is there a way to change a folder into an archive folder while keeping the folder structure?

Upvotes: 2

Views: 3011

Answers (1)

David K
David K

Reputation: 3132

It's just a matter of how you pass the information about the parentfolder folder in the parameters of make_archive.

Consider the following two examples:

shutil.make_archive('myarchive', 'zip', '/User/blah/blah', 'parentfolder')

This will create the zip archive myarchive.zip. If you open myarchive.zip you will first see the folder parentfolder and if you open that you will see the contents of the original parentfolder from which the archive was made.

shutil.make_archive('myarchive', 'zip', '/User/blah/blah/parentfolder')

This will create the zip archive myarchive.zip. If you open myarchive.zip you will immediately see the contents of the original parentfolder from which the archive was made. The folder name parentfolder will not appear in the archive.

The parameters are documented in https://docs.python.org/2/library/shutil.html, though to be honest it is not easy for me to see where the behavior I illustrated above is documented. I found it by trial and error (Python 2.7.14 on Windows).

Upvotes: 3

Related Questions