Difference between revisions of "Python:Template"
Jump to navigation
Jump to search
(Created page with "Template for Python programs. Creating logfiles, standard modules to load, opening files, user configuration. Code for mailing is on this wiki too. ToDO: * Parsin...") |
m |
||
Line 1: | Line 1: | ||
Template for Python programs. Creating logfiles, standard modules to load, opening files, user configuration. | Template for Python programs. Creating logfiles, standard modules to load, opening files, user configuration. | ||
− | [[Mail|Code for mailing | + | [[Mail|Code for mailing is on this wiki too]]. |
ToDO: | ToDO: | ||
Line 10: | Line 10: | ||
def usage(): | def usage(): | ||
print ("Usage: "+__file__) | print ("Usage: "+__file__) | ||
− | print(" | + | print("Template for python programs") |
sys.exit(1) | sys.exit(1) | ||
Revision as of 12:49, 14 September 2018
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()