webghost
webghost

Reputation: 133

shutil.make_archive issue - don't want directories included in zip file

I am trying to archive 2 files but not the full path leading up to the two files. Hopefully someone can point me in the right direction:

My directory structure is as follows. I only have two files which I want backed up.

/tmp
    my_stuff
        -hello.html
        -hello2.html

So per the documentation, shutil.make_archive can zip up entire directories. I am using the following command:

shutil.make_archive( base_name =  '/tmp/package',
                     format    =  'zip',
                     root_dir  =  '/tmp/my_stuff', 
                     base_dir  =  '/tmp/my_stuff' )

This command successfully creates the zip file however when I do "unzip package.zip from within the /tmp folder", I get the following:

➜  /tmp unzip package.zip
Archive:  package.zip
   creating: tmp/my_stuff
  inflating: tmp/my_stuff/hello.html  
  inflating: tmp/my_stuff/hello2.html

It creates a duplicate "tmp" within tmp and a new folder called my_stuff and the 2 files. All I want is for it to extract the two files (without the directories). Any advice would be appreciated.

Thanks

Upvotes: 6

Views: 4502

Answers (1)

Maurice Meyer
Maurice Meyer

Reputation: 18106

To create a flat without any directories, you need to change base_dir:

shutil.make_archive( base_name =  '/tmp/package',
                     format    =  'zip',
                     root_dir  =  '/tmp/my_stuff', 
                     base_dir  =  './' )

Upvotes: 8

Related Questions