Python:Template: Difference between revisions

From wiki
Jump to navigation Jump to search
mNo edit summary
No edit summary
Line 25: Line 25:
# END USER CONFIGURABLE ITEMS, DO NOT CHANGE ANYTHING BELOW THIS LINE
# END USER CONFIGURABLE ITEMS, DO NOT CHANGE ANYTHING BELOW THIS LINE


import os,sys,re,glob
import os,sys,re,glob,logging
from datetime import datetime
from datetime import datetime
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")


logfile2name = logfile2name.replace('timestamp',timestamp)
logfile2name = logfile2name.replace('timestamp',timestamp)
logfile,logfilename = openlog()
logfilename = openlog()
logfile2 = openlog(<apath>)[0]                                    # Get only filehandler, not the file name
openlog(<apath>)


def main():
def main():
     writelog(logfile,"This is in the logfile with generated name")
     logging.info("This is in the logfile with generated name")
     writelog(logfile2,"This is in the logfile with specified name")
     logging.info("This is in the logfile with specified name")
     openfile(filespec)
     openfile(filespec)


     return
     return


def openfile(filespec):
def openfile(filespec):
Line 47: Line 48:
                 for line in file:
                 for line in file:
                     <codeblock>
                     <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%S_%f")+" "+message+"\n")
     return
     return


Line 63: Line 53:
def openlog (logfilename = ''):
def openlog (logfilename = ''):
     if logfilename == '':
     if logfilename == '':
         logfilename = __file__.replace('/bin/','/log/')
         logfilename = re.sub('/bin/','/log/',__file__)
         logfilename += '_'+datetime.now().strftime("%y%m%d_%H%M")+'.log'
         logfilename += '_'+datetime.now().strftime("%y%m%d_%H%M")+'.log'
     try:
     logging.basicConfig(filename=logfilename,level=logging.DEBUG,format='%(asctime)s %(message)s')
        logfile.name
     logging.info(logfilename+" open.")
     except UnboundLocalError:
     return(logfilename)
        logfile = open (logfilename,"a")
     return(logfile,logfilename)


main()
main()
</syntaxhighlight>
</syntaxhighlight>

Revision as of 11:24, 22 October 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")
    version = 'R1A, 20181010, Initial release'
    print("Version: "+version)
    sys.exit(1)


# USER CONFIGURABLE ITEMS

warning = 'If you need to modify these variables anywhere else you have to declare it global there'
filespec =  '*.*'
logfile2name = '<apath>_timestamp.txt'

# END USER CONFIGURABLE ITEMS, DO NOT CHANGE ANYTHING BELOW THIS LINE

import os,sys,re,glob,logging
from datetime import datetime
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")

logfile2name = logfile2name.replace('timestamp',timestamp)
logfilename = openlog()
openlog(<apath>)

def main():
    logging.info("This is in the logfile with generated name")
    logging.info("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 openlog (logfilename = ''):
    if logfilename == '':
        logfilename = re.sub('/bin/','/log/',__file__)
        logfilename += '_'+datetime.now().strftime("%y%m%d_%H%M")+'.log'
    logging.basicConfig(filename=logfilename,level=logging.DEBUG,format='%(asctime)s %(message)s')
    logging.info(logfilename+" open.")
    return(logfilename)

main()