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()

Shouldn't zip_fh = 'C:\Documents and Settings\file.cfg' read zip_fh = 'C:\\Documents and Settings\\file.cfg' ?

Here's another problem:
for files in files:
Should be:

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 file in files:

The file name variable can also be assigned like this:

zip_fh = r'C:\Documents and Settings\file.cfg'

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'

You can determine the symbols defined in zipfile with dir(zipfile). 'ZipFile' should be available.

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.

def zip_fh(path):
   print "1) output file name is %s.zip" % (path)
   zipp = zipfile.ZipFile('%s.zip' % (path), 'w')
   for root, dirs, files in os.walk(path, topdown=True):
      print "2)", len(files). "files found"
      for fname in files:
         fh = os.path.join(root, fname)
         if os.path.isfile(fh):
            zipp.write(fh, os.path.basename(fh), zipfile.ZIP_DEFLATED)
         else:
            print fh, "3) is not a legitimate path and file name"
   zipp.close()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.