User:Feixie/Python

REF

 * www.diveintopython.net
 * http://tkinter.unpythonic.net/wiki/Events
 * http://www.tcl.tk/man/tcl8.5/TkCmd/contents.htm

Glosary
import sys import os import re import random sys.path.append("/path/to/libdir")

from argparse import ArgumentParser from subprocess import Popen import subprocess import datetime import time from UserDict import UserDict import lxml from lxml import etree

Code piece
import argparse import os from datetime import datetime import gzip import subprocess import re

def walkThroughFils(path): days = params.days maxdays = params.maxdays dryrun = params.dryrun if (os.path.isfile(path)): if (params.fnpat and not re.search(params.fnpat, path)): return dt = datetime.fromtimestamp(os.stat(path).st_mtime) if maxdays >= (datetime.now - dt).days >= days: if (dryrun): print("zip file %s whose mtime is %s" % (path, str(dt))) else: isZipFile = re.search("\.gz$", path) zipcmd = ["/gzip"] zipFileName = path + ".gz" if (not params.unzip and not isZipFile and not os.path.exists(zipFileName)): print("zipping " + path) zipcmd.append(path); elif (params.unzip and isZipFile): print("unzipping " + path) zipcmd.append("-d") zipcmd.append(path);

if (len(zipcmd) > 1): ret = subprocess.call(zipcmd) if (ret != 0): print("ERROR: failed to zip/unzip file " + path)

elif (os.path.isdir(path)): items = os.listdir(path) items.sort for i in items: walkThroughFils(os.path.join(path, i))

parser = argparse.ArgumentParser(description="zip log file out of date") parser.add_argument("path", nargs="?", default="./") parser.add_argument("--days", type=int, default=2) parser.add_argument("--maxdays", type=int, default=5) parser.add_argument("--dryrun", action="store_true") parser.add_argument("--unzip", action="store_true") parser.add_argument("--fnpat")

params = parser.parse_args;

print params

walkThroughFils(params.path)