Difference between revisions of "Python:Template"

From wiki
Jump to navigation Jump to search
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,logging
+
import os
 +
import sys
 +
import re
 +
import glob
 +
import 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")
Line 35: Line 39:
 
def main():
 
def main():
 
     logging.info("This is in the logfile with generated name")
 
     logging.info("This is in the logfile with generated name")
    logging.info("This is in the logfile with specified name")
 
 
     openfile(filespec)
 
     openfile(filespec)
  
Line 58: Line 61:
 
     logging.info(logfilename+" open.")
 
     logging.info(logfilename+" open.")
 
     return(logfilename)
 
     return(logfilename)
 +
  
 
main()
 
main()
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 16:37, 26 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
import sys
import re
import glob
import 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")
    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()