[Reprinted] A concise explanation of the usage of sys.argv[] in Python

A concise explanation of the usage of sys.argv[] in Python

A concise explanation of the usage of sys.argv[] in Python

      Because I am a self-taught python, I encountered this imported module function shortly after I started, and I have been editing it on IDLE and running it, trying to find its use from the results, but the results have always been fruitless, and I also checked it online. Many, but found only one version of a more detailed explanation of the problem, most of which were reproduced and copied. All the examples given are the long list of codes in the concise python tutorial. I said that I can understand it after reading it, but I still don’t really understand it because I am confused. I only know that "sys.argv[0] indicates the file path of the code itself" Point, in fact, is still unclear. Later, after a lot of hard work, I really realized it after asking for advice from many parties. I would like to record and share it, hoping to give some inspiration to the students who are also in the process of seeking from another angle.

  To put it bluntly, sys.argv[] is a bridge to obtain parameters from outside the program. This "external" is very important, so those who try to explain its function from the code have not understood it. Because we can get multiple parameters from the outside, what we get is a list (list), that is to say, sys.argv can actually be regarded as a list, so we can use [] to extract the elements. Its first element is the program itself, followed by parameters given externally.

Below we illustrate its usage through the running results of a very simple test.py program.

 #test.py
 import sys
 a=sys.argv[0]
 print(a)

Save test.py in the root directory of the c drive.

Find 'Run' in the program -> click -> enter "cmd" -> press Enter to enter the console command window (as shown below), first enter cd c:\ (the function is to change the command path to the root directory of the c drive) , and then enter test.py to run the program we just wrote:

 

The result is C:\test.py, which is what 0 refers to the code (that is, this .py program) itself.

Then we change 0 to 1 in the code:

a=sys.argv[1]

After saving, run it from the console window again, this time we add a parameter, enter: test.py what

 

 The result is the parameter what we input. Do you start to understand when you see this.

Then let's modify the code again:

a=sys.argv[2:]

After saving, run the program from the console windowsill, this time add a few more parameters, separated by spaces:

test.py a b c d e f

 

The result is ['b', 'c', 'd', 'e', ​​'f']

 

You should have realized it. Sys.argv[ ] is actually a list, and the items in it are the parameters input by the user. The key is to understand that the parameters are input from the outside of the program, not where in the code itself. If you want to see its effect, you should set The program is saved, run the program from the outside and give the parameters.


Reposted from: https://www.cnblogs.com/aland-1415/p/6613449.html

Guess you like

Origin blog.csdn.net/TommyXu8023/article/details/109200685