Python:Template
Jump to navigation
Jump to search
Template for Python programs. Creating logfiles, standard modules to load, opening files, user configuration.
Code for mailing is on this wiki too.
ToDO:
- Parsing commandline options
def usage():
print ("Usage: "+__file__)
print("Template for python programs")
sys.exit(1)
# USER CONFIGURABLE ITEMS
filespec = '*.*'
# END USER CONFIGURABLE ITEMS, DO NOT CHANGE ANYTHING BELOW THIS LINE
import os,sys,re,glob
from datetime import datetime
timestamp = datetime.now().strftime("%y%m%d_%H%M%S")
def main():
logfile,logfilename = openlog()
logfile2,dummy = openlog(<apath>)
writelog(logfile,"This is in the logfile with generated name")
writelog(logfile2,"This is in the logfile with specified name")
openfile(filespec)
return
def openfile(filespec):
filelist = glob.glob(filespec)
for filename in filelist:
if os.path.isfile(filename):
with open (filename,"r") as file:
for line in file:
<codeblock>
return
def writelog (logfile,message):
try:
logfile.name
except AttributeError:
print("ERROR: Logfile is not open")
usage()
else:
logfile.write(datetime.now().strftime("%y%m%d_%H%M")+" "+message+"\n")
return
def openlog (logfilename = ''):
if logfilename == '':
logfilename = re.sub('/bin/','/log/',__file__)
logfilename += '_'+datetime.now().strftime("%y%m%d_%H%M")+'.log'
try:
logfile.name
except UnboundLocalError:
logfile = open (logfilename,"a")
return(logfile,logfilename)
main()