Python Forum
Command line inputs not printing to Log File - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Command line inputs not printing to Log File (/thread-5739.html)



Command line inputs not printing to Log File - ijosefson - Oct-19-2017

Requirement: print out the command line prompts the user enters into a log file.
Problem: line 38, the prompts don't seem to write to the log file.

Code:
#! /usr/bin/env python3

import sys
import os
import argparse
import platform
import logging

if __name__ == "__main__": 
    parser = argparse.ArgumentParser(
        description='File parsing program')
    parser.add_argument('-l', '--log_file',
                        default='lab2.log',
                        metavar = ' ',
                        help='Log file',
                        action="store")
    parser.add_argument('-p', '-parse',
                        metavar = ' ',
                        help="File to parse",
                        required = True,
                        action="store")
    parser.add_argument('-s', '-string',
                        metavar = ' ',
                        help="String to search",
                        required = True,
                        action="store")
    args = parser.parse_args()
    
    try:
        logging.basicConfig(filename='lab2.log',
                            level=logging.DEBUG,
                            format=
                            '%(asctime)s,%(message)s',
                            datefmt=
                            '%m/%d/%Y %I:%M:%S %p')                 #logs date and time
        logging.debug('The logging occured in: %s ', __name__)      #logs name of code
        logging.info('The program has started!')                    #prompts start of program
        logging.info('The command line prompts were: 1) log file = %s, 2) parse = %s, and 3) string = %s ',
                     args.log_file, 
                     args.parse, 
                     args.string)                                   #logs command line prompts
    except:
        print('Could not open log file :(')                         #error message if file not created



RE: Command line inputs not printing to Log File - buran - Oct-19-2017

normal way to debug an error within try/except block is to print error msg verbatim or disable the try/except block temporary  until you are certain the block works.
In this case the problem is with line#17 and line#22 -> long flags should be '--parse' and '--string'