Python Forum
Command line inputs not printing to Log File
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Command line inputs not printing to Log File
#1
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
Reply
#2
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'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete line of file dionatandiego11 2 1,875 Aug-05-2020, 05:40 PM
Last Post: deanhystad
  Seven Segment Display - QUERY on Multi-line side-by-side printing output ajayachander 3 4,757 Mar-13-2020, 07:02 AM
Last Post: ajayachander
  My Loop for printing text file goes to infinity Nomex 7 4,149 Feb-10-2020, 03:13 PM
Last Post: buran
  Printing on same line from 2 functions. Dyefull 2 2,207 Dec-05-2019, 07:39 AM
Last Post: ndc85430
  finding p's in words of a multi-line text file johneven 5 4,269 Jun-24-2019, 03:41 PM
Last Post: ThomasL
  reading text file and writing to an output file precedded by line numbers kannan 7 10,242 Dec-11-2018, 02:19 PM
Last Post: ichabod801
  SSL Certificate Error when trying to do command-line registration with pypi DeliriousDelight 0 2,665 Dec-06-2018, 09:38 AM
Last Post: DeliriousDelight
  command line calculator Zatoichi 4 3,946 Jan-28-2018, 09:01 PM
Last Post: egslava
  Newly written .txt file not printing Crackity 11 5,927 Oct-20-2017, 06:43 AM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020