This article mainly introduces Python command line parameters. Python command line parameters encounter many difficulties in a wide range of applications, for example, sys and getopt modules in python process command line parameters, the following is a detailed introduction. If you want to pass parameters to python scripts, what is the corresponding argc and argv (command line parameters in C) in python?
Required module: sys
Number of parameters: len (sys. argv)
Script Name: sys. argv [0]
Parameter 1: sys. argv [1]
Parameter 2: sys. argv [2]
- Est. py
- View sourceprint? 1 import sys
- Print "Script Name:", sys. argv [0]
- For I in range (1, len (sys. argv )):
- Print "parameter", I, sys. argv [I]
- Python test. py hello world
Script Name: test. py
Parameter 1 hello
Parameter 2 world
Use the command line option in python:
For example, we need a convert. py script. It is used to process a file and output the processed result to another file.
The script must meet the following conditions:
1. Use the-I-o option to differentiate the Python command line parameters
Whether the parameter is an input file or an output file. python convert. py-I inputfile-o outputfile
2. If you do not know which parameters are required for convert. py, use-h to print the help information.
Python convert. py-h
Original form of the getopt function:
- getopt.getopt(args, options[, long_options])
- convert.py
- view sourceprint?01 import sys, getopt
- opts, args = getopt.getopt(sys.argv[1:], "hi:o:")
- input_file=""
- output_file=""
- for op, value in opts:
- if op == "-i":
- input_file = value
- elif op == "-o":
- output_file = value
- elif op == "-h":
- usage()
- sys.exit()
Code explanation:
A) sys. argv [1:] is the list of parameters to be processed, and sys. argv [0] is the script name. Therefore, use sys. argv [1:] to filter out the Script Name.
B) "hi: o:": When an option only indicates the switch status, that is, when no additional parameter is added, the option character is written into the analysis string. When the option is followed by an additional parameter, write the option character in the analysis string and add. Therefore, "hi: o:" indicates that "h" is a switch option; "I:" and "o:" indicates that a parameter should be followed.
C) Call the getopt function. The function returns two lists: opts and args. Opts is the analyzed format information. Args is the remaining Python command line parameters that do not belong to the format information
Command line parameters. Opts is a list of two-element groups. Each element is: (option string, additional parameter ). If no parameter is attached, it is an empty string ''.
The third parameter [, long_options] of the getopt function is an optional long option parameter. All of the preceding parameters are short options (such as-I-o)
Example of long option format:
- version
- file=error.txt
Make a script support both short and long options.
- The wonderful use of the Python programming language in website development
- The incomprehensible relationship between the Python programming language and Zpoe
- How does Python IDE adapt to the current turbulent market
- Basic Environment Test for Python Programming
- How to unpack Python Sequences