python script reading config file and then zipping files after X days from directories. dir contains dynamic files. i have specified the extension and the "startswith" in the config. problem i have is it zips up everything. (if i can get this work will add a delete too). i also want it to perform these actions only when it is in the specified server environment. eg if it is in vod just do vod stuff and skip vod2 as defined in the config file.

## config file
[log1]
env = vod2
path = /projects/log/files
zip_time = 7
del_time = 14
ftype = dmplog

[log2]
env = vod
path = /projects/log/files
zip_time = 7
del_time = 14
ftype = dmptrc


# main section

def main():
   global zip_cfg
   # current time
   now = time.time()
   true_env = '%s' % (os.environ['LOGNAME'])
   env = true_env
   zip_cfg = '/home/etc/file_cleanup.cfg'
   conf_config = ConfigParser.ConfigParser()
   conf_config.read(zip_cfg)
   # check environment and then loop thru config file
   if env == true_env:
      print env
      for section in conf_config.sections():
         env = conf_config.get(section, 'env')
         path = conf_config.get(section, 'path')
         del_time = conf_config.get(section, 'del_time')
         zip_time = conf_config.get(section, 'zip_time')
         ftype = conf_config.get(section, 'ftype')
         zip_time2 = int(zip_time)
         #print zip_time2
         print ""
         
         # check if file older than X days then zip
         dirList = os.listdir(path)
         for fnames in dirList:
            if fnames.endswith(ftype) or fnames.startswith(ftype):
               true_file = os.path.join(path, fnames)
               print true_file
               # diff b/w current time and last time file was modified
               ftime = now - zip_time2 * 86400
               print ftime
               if os.stat(true_file).st_mtime < ftime:
                  if os.path.isfile(true_file):
                     #print os.stat(true_file).st_mtime
                     zippy(path)
                     #os.remove(path)

Recommended Answers

All 3 Replies

Do you have any code for the function zippy? I'm trying to walk through the code, but I don't have all the pieces.

def zippy(path):
   for root, dirs, files in os.walk(path, topdown=True):
      for file in files:
         file_path = os.path.join(root, file)
         fh = commands.getoutput('/usr/bin/bzip2 % s' % (file_path))

This is the errant code. If it finds any file that fulfills the requirements, the whole path is zipped (and is re_zipped multiple times if more than one file is found). Perhaps you want to append the file name to a list, and zip those files only. You could also copy the files to a temporary directory, zip that directory, and then delete everything in it. That may cause problems when un-zipping as it will unzip to the temporary directory and not the original directory.

if os.stat(true_file).st_mtime < ftime:
                  if os.path.isfile(true_file):
                     #print os.stat(true_file).st_mtime

                     ##--- zips everything in the path
                     zippy(path)
                     #os.remove(path)
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.