• January 3, 2021
  • July 29, 2021

Python: Output the log with logging module

It is general to export the log from the batch script which is executed periodically. The log is also useful to analyze the problem. You may insert the special log for debug to explore the reason of bug. The log is base facility to explore the reason of problem and recover from it. You can output the log from Python script with logging module. Simple Logging Most simple logging code is following. import logging logging.basicConfig(level=logging.INFO) logging.info("Most Simple Log. The Value is %d.", 10) Execute this script, the following log will be output. $ python3 demo.py INFO:root:Most Simple Log. The Value is […]

  • January 2, 2021
  • July 29, 2021

Python: Get the Command Line Argument with ArgumentParser

It is easy to get the command line argument of python shell script with ArgumentParser class. The ArgumentParser class has following functions: Get the positional required argument Get the optional argument Output the help If the script executed without required arguments, the ArgumentParser class abort the script. It is hard work to write above functions by yourself in your own script. Basic usage The basic code to use `ArgumentParser` class is following: import argparse # Allocate the parser parser = argparse.ArgumentParser() # Register arguments to the parser # Parse command line arguments # The result will be returned into the args variable. args […]