Issue in creating Zip file using glob.glob

Posted by infosyssec on Stack Overflow See other posts from Stack Overflow or by infosyssec
Published on 2010-05-24T12:56:43Z Indexed on 2010/05/24 13:01 UTC
Read the original article Hit count: 337

Filed under:
|

Hi,

I am creating a Zip file from a folder (and subfolders). it works fine and creates a new .zip file also but I am having an issue while using glob.glob. It is reading all files from the desired folder (source folder) and writing to the new zip file but the problem is that it is, however, adding subdirectories, but not adding files form the subdirectories.

I am giving user an option to select the filename and path as well as filetype also (Zip or Tar). I don;t get any problem while creating .tar.gz file, but when use creates .zip file, this problem comes across.

Here is my code:

for name in (Source_Dir):
    for name in glob.glob("/path/to/source/dir/*" ):
        myZip.write(name, os.path.basename(name), zipfile.ZIP_DEFLATED)

myZip.close()

Also, if I use code below:

for dirpath, dirnames, filenames in os.walk(Source_Dir):
        myZip.write(os.path.join(dirpath, filename) os.path.basename(filename))        

myZip.close()

Now the 2nd code taks all files even if it inside the folder/ subfolders, creates a new .zip file and write to it without any directory strucure. It even does not take dir structure for main folder and simply write all files from main dir or subdir to that .zip file.

Can anyone please help me or suggest me. I would prefer glob.glob rather than the 2nd option to use.

Thanks in advance.

Regards, Akash

© Stack Overflow or respective owner

Related posts about glob

Related posts about zipfile