LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-23-2014, 03:44 AM   #1
simhumcon
Member
 
Registered: Dec 2013
Posts: 35

Rep: Reputation: Disabled
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!!
 
Old 01-23-2014, 03:54 AM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,131
Blog Entries: 2

Rep: Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833
Please show us what you have got already and where you have problems with your code.
 
Old 01-23-2014, 04:47 AM   #3
simhumcon
Member
 
Registered: Dec 2013
Posts: 35

Original Poster
Rep: Reputation: Disabled
#
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()
#
 
Old 01-23-2014, 07:04 AM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,131
Blog Entries: 2

Rep: Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833
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.
 
Old 01-23-2014, 07:22 AM   #5
simhumcon
Member
 
Registered: Dec 2013
Posts: 35

Original Poster
Rep: Reputation: Disabled
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')
 
Old 01-23-2014, 07:23 AM   #6
simhumcon
Member
 
Registered: Dec 2013
Posts: 35

Original Poster
Rep: Reputation: Disabled
another thing is...i also can figureout a way on how get the zipfiles to be named the same the folder it will zip
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script to compare folders,Sub-Folders and Sub-Sub-Folders unix_72427 Programming 8 08-08-2012 03:51 PM
zipping a directory tractng Linux - Newbie 3 04-16-2008 05:54 PM
Zipping a folder Gins Linux - General 9 06-19-2007 06:17 PM
Zipping of files rajesh_b Programming 5 10-27-2004 07:02 AM
zipping/compressing folders and files DiZASTiX Linux - Newbie 1 05-26-2003 09:48 PM


All times are GMT -5. The time now is 10:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration