| | |
python config files zip
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2007
Posts: 25
Reputation:
Solved Threads: 0
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()•
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
Here's another problem:
for files in files:
Should be:
The file name variable can also be assigned like this:
for files in files:
Should be:
Python Syntax (Toggle Plain Text)
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:
Python Syntax (Toggle Plain Text)
zip_fh = r'C:\Documents and Settings\file.cfg'
•
•
Join Date: Sep 2007
Posts: 25
Reputation:
Solved Threads: 0
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'
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'
•
•
Join Date: Dec 2006
Posts: 1,065
Reputation:
Solved Threads: 300
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.
Python Syntax (Toggle Plain Text)
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()
Last edited by woooee; Nov 30th, 2008 at 12:46 am.
![]() |
Similar Threads
- Python Executable (Py2Exe) (Python)
Other Threads in the Python Forum
- Previous Thread: Using the wxClipboard
- Next Thread: Ask user for binary number
Views: 720 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Python
anti approximation array avogadro basic beginner builtin cipher clear code color converter countpasswordentry cturtle curved def dictionary drive dynamic examples excel file float format frange ftp function gui heads homework import input java lapse library line lines linux list lists loop mouse multiple mysqldb mysqlquery newb number numbers output parsing path port prime program programming projects py2exe pygame pymailer pyqt python random raw_input recursion recursive redirect script scrolledtext server singleton sqlite ssh stamp string strings subprocess sum syntax table terminal text textarea thread threading time tkinter tlapse tuple tutorial twoup ubuntu unicode urllib urllib2 variable web-scrape wikipedia windows word wxpython






