LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   zipping folders using python (https://www.linuxquestions.org/questions/linux-newbie-8/zipping-folders-using-python-4175492258/)

simhumcon 01-23-2014 02:44 AM

zipping folders using python
 
hello all,

im really new to python. please help me out with this

i'm trying to create a python script that will zip a folder. In my red hat PC, in /usr/local/testlogs/, there will be be few folders. i want to zip all the folder's , NOT as one zip file, but as multiple zip files. please help me out!!

TobiSGD 01-23-2014 02:54 AM

Please show us what you have got already and where you have problems with your code.

simhumcon 01-23-2014 03:47 AM

#
import os
import zipfile

path = '/usr/local/testlogs/test2/*'

def zipdir(path, zip):
for root, dirs, files in os.walk(path):
for file in files:
zip.write(os.path.join(root, file))



for directories in glob.glob(path) :
zipf = zipfile.ZipFile('%s.zip'%directory, 'w')
zipdir(path, zipf)
zipf.close()
#

TobiSGD 01-23-2014 06:04 AM

When posting Python code it is mandatory to use code-tags (see my signature for explanation), so that the indentation is preserved. Not indented Python code is unreadable.

simhumcon 01-23-2014 06:22 AM

hi,

sorry about that. i have tried really hard to get this working and below is my latest script. im not sure why its not creating separate zip files. It is adding all the folders into 1 zip file

Code:

def zip_folder(folder_path, output_path):
    parent_folder = os.path.dirname(folder_path)
    contents = os.walk(folder_path)
    try:
        zip_file = zipfile.ZipFile(output_path, 'w', zipfile.ZIP_DEFLATED)
        for root, folders, files in contents:
            for folder_name in folders:
                absolute_path = os.path.join(root, folder_name)
                relative_path = absolute_path.replace(parent_folder + '\\',
                                                      '')
                print "Adding '%s' to archive." % absolute_path
                zip_file.write(absolute_path, relative_path)
            for file_name in files:
                absolute_path = os.path.join(root, file_name)
                relative_path = absolute_path.replace(parent_folder + '\\',
                                                      '')
                print "Adding '%s' to archive." % absolute_path
                zip_file.write(absolute_path, relative_path)
        print "'%s' created successfully." % output_path
    except IOError, message:
        print message
        sys.exit(1)
    except OSError, message:
        print message
        sys.exit(1)
    except zipfile.BadZipfile, message:
        print message
        sys.exit(1)
    finally:
        zip_file.close()

if __name__ == '__main__':
    zip_folder(r'/usr/local/testlogs/test2/',
                r'/usr/local/testlogs/test/zipfilename.zip')


simhumcon 01-23-2014 06:23 AM

another thing is...i also can figureout a way on how get the zipfiles to be named the same the folder it will zip


All times are GMT -5. The time now is 01:52 AM.