Python method for obtaining command line parameters (recommended), python command line parameters

Source: Internet
Author: User
Tags element groups

Python method for obtaining command line parameters (recommended), python command line parameters

This article describes how the sys and getopt modules in python process command line parameters.

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]

Test. py

Import sysprint "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 identify 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.pypython test.py -i inputfile -o outputfileimport sys, getoptopts, 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:" indicate 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. 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 ''.

In the above example, ops is: [('h', ''), ('-I', 'inputfile'), ('-O', 'outputfile')]

Args is the remaining command line parameter that does not belong to the format information.

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

Allow a script to support both short and long options.

getopt.getopt(sys.argv[1:], "hi:o:", ["version", "file="])

Summary

The above section describes how to obtain command line parameters in Python. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.