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

1

6

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

Conakry answered 12/1, 2017 at 23:20 Comment(0)
F
8

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  =  './' )
Frodina answered 13/1, 2017 at 0:1 Comment(1)
Thank you! This piece of information should actually be part of the official documentation and examples, because it seems like a quite common use case.Fifteen

© 2022 - 2024 — McMap. All rights reserved.