python config files zip

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2007
Posts: 25
Reputation: axn is an unknown quantity at this point 
Solved Threads: 0
axn axn is offline Offline
Light Poster

python config files zip

 
0
  #1
Nov 29th, 2008
need to read the config file and then zip files in path. dont get any errors but files not zipping

# config file
[clean1]
path = C:/Project/Log1
action=zip
 
[clean2]
path = C:/Project/Log2
action = zip

#zip.py
import zipfile, os, time, sys, ConfigParser, zlib
def zip_fh():
    zip = zipfile.ZipFile('%s.zip' % (path), 'w')
    for root, dirs, files in os.walk(path, topdown=True):
        for files in files:
            file_fh = os.path.join(root, file)
            zip.write(file_fh, os.path.basename(file_fh), zipfile.ZIP_DEFLATED)
    zip.close()
            
def main():
    julian = time.strftime('%j', time.localtime())
    zip_fh = 'C:\Documents and Settings\file.cfg'
    conf_config = ConfigParser.ConfigParser()
    conf_config.read(zip_fh)
    for section in conf_config.sections():
        path = conf_config.get(section, 'path')
        print path
        
        for root, dirs, files in os.walk(path, topdown=True):
            for file in files:
                fh = os.path.join(root, file)
                zip_fh()
    
    
if __name__=="__main__":
    main()
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 311
Reputation: BearofNH is on a distinguished road 
Solved Threads: 40
BearofNH's Avatar
BearofNH BearofNH is offline Offline
Posting Whiz

Re: python config files zip

 
0
  #2
Nov 29th, 2008
Shouldn't zip_fh = 'C:\Documents and Settings\file.cfg' read zip_fh = 'C:\\Documents and Settings\\file.cfg' ?
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: python config files zip

 
0
  #3
Nov 29th, 2008
Here's another problem:
for files in files:
Should be:
  1. import zipfile, os, time, sys, ConfigParser, zlib
  2. def zip_fh():
  3. zip = zipfile.ZipFile('%s.zip' % (path), 'w')
  4. for root, dirs, files in os.walk(path, topdown=True):
  5. for file in files:

The file name variable can also be assigned like this:
  1. zip_fh = r'C:\Documents and Settings\file.cfg'
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 25
Reputation: axn is an unknown quantity at this point 
Solved Threads: 0
axn axn is offline Offline
Light Poster

Re: python config files zip

 
0
  #4
Nov 29th, 2008
import os, time, sys, ConfigParser, zlib, zipfile

def zip_fh(path):
zipp = zipfile.ZipFile('%s.zip' % (path), 'w')
for root, dirs, files in os.walk(path, topdown=True):
for file in files:
fh = os.path.join(root, file)
zipp.write(fh, os.path.basename(fh), zipfile.ZIP_DEFLATED)
zipp.close()


def main():
#julian = time.strftime('%j', time.localtime())
# zip for windows
#zip_cfg = r'C:\Documents and Settings\file.cfg'
# zip for unix
zip_cfg = '/home/bin/file.cfg'
conf_config = ConfigParser.ConfigParser()
conf_config.read(zip_cfg)
for section in conf_config.sections():
path = conf_config.get(section, 'path')
action = conf_config.get(section, 'action')
mtime = conf_config.get(section, 'mtime')
print path, mtime, action

zip_fh(path)
if __name__=="__main__":
main()

# error - is it possible that the module dont exist in python2.4
File "./cfg.py", line 7, in zip_fh
zipp = zipfile.ZipFile('%s.zip' % (path), 'w')
AttributeError: 'module' object has no attribute 'ZipFile'
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: python config files zip

 
0
  #5
Nov 29th, 2008
It's almost impossible to follow your code without code tags. Please use them! Like this:

[code].....code goes here......[/code]

You can determine the symbols defined in zipfile with dir(zipfile). 'ZipFile' should be available.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,065
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 300
woooee woooee is offline Offline
Veteran Poster

Re: python config files zip

 
0
  #6
Nov 30th, 2008
I've added some print statements so you can see what is going on. First, the file name used for the zip file output is printed to make sure you are using the correct file name (if you submit "C:\\Test\\ZipDir" then the output file will be under "C:\\Test\\" and will be named "ZipDir.zip"), and second, you want to know if any files were actually found for the path arg submitted to the function, and third, if you are recombining the path and file name correctly by testing for "isfile" ("dirs" is not used in your program which may or may not be O.K.). Finally, you want zipp to point to a file in a different directory otherwise you will also be zipping the file that is output of the zip operation and that's not good.
  1. def zip_fh(path):
  2. print "1) output file name is %s.zip" % (path)
  3. zipp = zipfile.ZipFile('%s.zip' % (path), 'w')
  4. for root, dirs, files in os.walk(path, topdown=True):
  5. print "2)", len(files). "files found"
  6. for fname in files:
  7. fh = os.path.join(root, fname)
  8. if os.path.isfile(fh):
  9. zipp.write(fh, os.path.basename(fh), zipfile.ZIP_DEFLATED)
  10. else:
  11. print fh, "3) is not a legitimate path and file name"
  12. zipp.close()
Last edited by woooee; Nov 30th, 2008 at 12:46 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum


Views: 720 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC