Drake .C
Drake .C

Reputation: 332

Unexpected file using shutil.make_archive to compress file

I am trying to use shutil.make_archive to zip files.

When I am using :

import shutil
shutil.make_archive('zip_file', 'zip', 'C:\\Users\\test')

I got a zip_file in the zip_file, and the zip_file in the zip_file seems to be a corrupted file. Anyone knows how to deal with it?

However, when I am using :

make_archive(
  'zipfile_name', 
  'zip',           # the archive format - or tar, bztar, gztar 
  root_dir=file_dir,   # root for archive - current working dir if None
  base_dir=file_dir)   # start archiving from here - cwd if None too

It gets everything correct except the folder directory wrong. For example, if file_dir = 'C:\Users\aaa\Desktop\test\bbb' The structure of the zip file will be:

 Users  
         aaa
              Desktop
                     test
                         bbb

And I wish it could be bbb/ only.

Any thoughts? I really appreciate your help.

Upvotes: 4

Views: 2564

Answers (1)

Patrick Haugh
Patrick Haugh

Reputation: 60944

I think the solution is to create the zipfile in a different directory. From there, something like

shutil.make_archive('zip_file', 'zip', 'C:\\Users\\test', "bbb")

should give you an archive of the bbb directory.

Upvotes: 2

Related Questions