[introduction to Python tutorial] Python command line parameters

Python provides a getopt module for parsing command-line options and parameters.

$ python test.py arg1 arg2 arg3

The Python sys module provides access to any command line parameter through sys.argv. There are two main parameter variables-

  • sys.argv is a list of command line parameters.
  • len(sys.argv) is the number of command line arguments.

Here sys.argv [0] is the program name, that is, the name of the script. For example, in the above example code, the value of sys.argv [0] is test.py.

Example

Look at the following script command_ line_ Code for arguments.py-

#!/usr/bin/python3

import sys

print ('Number of arguments:', len(sys.argv), 'arguments.')
print ('Argument List:', str(sys.argv))

Now run the above script, which will produce the following results-

F:\>python F:\worksp\python\command_line_arguments.py
Number of arguments: 1 arguments.
Argument List: ['F:\\worksp\\python\\command_line_arguments.py']

F:\>python F:\worksp\python\command_line_arguments.py arg1 arg2 arg3 arg4
Number of arguments: 5 arguments.
Argument List: ['F:\\worksp\\python\\command_line_arguments.py', 'arg1', 'arg2', 'arg3', 'arg4']

F:\>

Note - as mentioned above, the first parameter is always the script name, which is also counted in the number of parameters.

Parsing command line parameters

Python provides a getopt module that can be used to parse command-line options and parameters. The module provides two functions and exceptions to enable command-line parameter parsing. Finally, if your time is not very tight and you want to improve quickly, the most important thing is not afraid of hardship. It is recommended that you can price @ 762459510. That's really good. Many people make rapid progress and need you to be not afraid of hardship! You can add it and have a look~

getopt.getopt method

This method parses the list of command line options and parameters. The following is a simple syntax for this method-

getopt.getopt(args, options, [long_options])

getopt.GetoptError exception

This exception is thrown when there is an unrecognized option in the parameter list, or when the option requiring a parameter is not any parameter.
The parameter of the exception is a string indicating the cause of the error. The properties msg and opt give error messages and related options. Finally, if your time is not very tight and you want to improve quickly, the most important thing is not afraid of hardship. It is recommended that you can price @ 762459510. That's really good. Many people make rapid progress and need you to be not afraid of hardship! You can add it and have a look~

Example

Suppose you want to pass two file names through the command line, and you also want to give an option to display the usage of the script. The usage of the script is as follows-

usage: file.py -i <inputfile> -o <outputfile>

The following is the command_ line_ The following script for usage.py-

#!/usr/bin/python3

import sys, getopt

def main(argv):
   inputfile = ''
   outputfile = ''
   try:
      opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
   except getopt.GetoptError:
      print ('GetoptError, usage: command_line_usage.py -i <inputfile> -o <outputfile>')
      sys.exit(2)
   for opt, arg in opts:
      if opt == '-h':
         print ('usage: command_line_usage.py -i <inputfile> -o <outputfile>')
         sys.exit()
      elif opt in ("-i", "--ifile"):
         inputfile = arg
      elif opt in ("-o", "--ofile"):
         outputfile = arg
   print ('Input file is "', inputfile)
   print ('Output file is "', outputfile)

if __name__ == "__main__":
   main(sys.argv[1:])

Now, run the script in the following ways, and the output is as follows:

F:\worksp\python>python command_line_usage.py -h
usage: command_line_usage.py -i <inputfile> -o <outputfile>

F:\worksp\python>python command_line_usage.py -i inputfile.txt -o
GetoptError, usage: command_line_usage.py -i <inputfile> -o <outputfile>

F:\worksp\python>python command_line_usage.py -i inputfile.txt -o outputfile.txt
Input file is " inputfile.txt
Output file is " outputfile.txt

F:\worksp\python>

Benefits: you can get the python tutorial video for free by replying to [01] in a private letter

Keywords: Python Django Pycharm Back-end crawler

Added by rami on Thu, 30 Sep 2021 00:38:39 +0300