Python3 in sys.argv [] usage explained

 

  sys.argv [] that the white bridge is a parameter acquired from an external program, this "external" very critical, so that the code tries to explain its role has been interpreted not understand. Because the parameters we get from the outside may be more, so get is a list (list), that is to say sys.argv in fact can be viewed as a list, so in order to use [] to extract its elements. The first element is that the program itself, and then only given followed by external parameters.

Let us illustrate the results of its use by running a very simple test.py program.

1 #test.py
2 
3 import sys
4 a=sys.argv[0]
5 print(a)

 The test.py saved in the root directory of c drive.

 Found 'run' in the program -> Click -> input "cmd" -> Enter key to enter console command (see below), to enter cd c: \ (role is to change the command path c root directory) and enter test.py run the program we just wrote:

  The result is C: \ test.py, which means the code is 0 (i.e., this procedure .py) itself means.

  Then we will code 0 to 1:

  a=sys.argv[1]

  Once saved, and then run from the console window, this time we add a parameter, enter: test.py what

 The result is the parameter we enter what, to see where you are not beginning to understand it.

 That we then modify the code:

 a=sys.argv[2:]

 Once saved, and then run the program from the console window sill, this time a few more parameters, separated by a space:

 test.py a b c d e f

 The results obtained for [ 'b', 'c', 'd', 'e', ​​'f']

 

   You should Taichetaiwu it. Sys.argv [] is actually a list of items inside the parameters entered by the user, the key is to understand that this argument is input from an external program, where instead of the code itself, in order to see its effect should be save the program, to run from an external program and given parameters.

  sys.argv [] that the white bridge is a parameter acquired from an external program, this "external" very critical, so that the code tries to explain its role has been interpreted not understand. Because the parameters we get from the outside may be more, so get is a list (list), that is to say sys.argv in fact can be viewed as a list, so in order to use [] to extract its elements. The first element is that the program itself, and then only given followed by external parameters.

Let us illustrate the results of its use by running a very simple test.py program.

1 #test.py
2 
3 import sys
4 a=sys.argv[0]
5 print(a)

 The test.py saved in the root directory of c drive.

 Found 'run' in the program -> Click -> input "cmd" -> Enter key to enter console command (see below), to enter cd c: \ (role is to change the command path c root directory) and enter test.py run the program we just wrote:

  The result is C: \ test.py, which means the code is 0 (i.e., this procedure .py) itself means.

  Then we will code 0 to 1:

  a=sys.argv[1]

  Once saved, and then run from the console window, this time we add a parameter, enter: test.py what

 The result is the parameter we enter what, to see where you are not beginning to understand it.

 That we then modify the code:

 a=sys.argv[2:]

 Once saved, and then run the program from the console window sill, this time a few more parameters, separated by a space:

 test.py a b c d e f

 The results obtained for [ 'b', 'c', 'd', 'e', ​​'f']

 

   You should Taichetaiwu it. Sys.argv [] is actually a list of items inside the parameters entered by the user, the key is to understand that this argument is input from an external program, where instead of the code itself, in order to see its effect should be save the program, to run from an external program and given parameters.

Guess you like

Origin www.cnblogs.com/mashuqi/p/12127214.html