"Python" python gets command line parameters

Source: Internet
Author: User

Sometimes it is necessary to use the same Python program at different times to deal with different files at this time, it is assumed that the Python program to change the input, output file name, it is too troublesome. It's much easier to get command-line parameters through Python. The following is a small program I wrote, I hope to help you.


For example, one of the following programs is test.py by accepting the command line two parameters. and print out both of these parameters.

Import sys                              #需导入sys模块 print sys.argv[1], sys.argv[2]          #打印出从命令行接受的两个參数


Linux execution: python test.py hello python (note: Hello is the first parameter.) Python is the second number of references)

Execution Result: Hello Python

Each of the two parameters entered is Hello and Python, and then the program accepts the two parameters and prints them out. It's simple.

It is very convenient for a program that often needs to change the read and output files.




"Python" python gets command line parameters

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.