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