What does "sys.argv" mean?

I study the code, and one of its lines confuses me:

things = [float(arg) for arg in sys.argv[1:]] Omega_a, Omega_b, Delta_a, Delta_b, \ init_pop_a, init_pop_b, tstep, tfinal = things 

I searched online and tried to understand what sys.arg means, and here is my understanding:

So sys.argv[0] is the name of the file, and sys.argv[1:] is the rest of the parameters that should be specified by users. I'm not sure I got it right, and if so, then I don’t understand why it doesn’t look like:

 Omega_a = input() Omega_b = input() etc... 

What is the difference between these two ways of providing parameters?

Also, if I run the code (press F5 ), the Python shell will report an error to me, for example:

 Traceback (most recent call last): File "C:\Users\testcode.py", line 55, in <module> init_pop_a, init_pop_b, tstep, tfinal = things ValueError: need more than 0 values to unpack 

I was not even given a chance to give parameters ( sys.argv[1:] ) before he gave me an error. Therefore, I searched the Internet. It seems like I need to run this code in cmd, which confused me even more, why is it needed and how do I put in cmd to run it?

+6
source share
4 answers

The difference is that the sys.argv (command line) parameters are set before the program starts (when it starts):

 python testcode.py arg1 arg2 arg3 arg4 and so on ... 

This will cause your variables to be as follows:

 Omega_a = 'arg1' Omega_b = 'arg2' Delta_a = 'arg3' Delta_b = 'arg4' init_pop_a = 'and' init_pop_b = 'so' tstep = 'on' tfinal = '...' 

While input() indicated when the program is running.

Since you do not run the program with parameters, it gives you an error because there are not enough (exactly 0) parameters that need to be unpacked into variables.

+6
source

sys.argv are called "command line options." If you want to pass them, you must run the script from the command line. On Windows, the command will look like this:

 cmd> python C:\Users\testcode.py arg1 args2 

where "cmd>" is the prompt you get after using Start → Run.

+2
source

All other answers explained sys.argv just fine, but I think that was part of the fundamental terminology that was missing. I just wanted to add that ...

input() tells your program to read stdin .

It is like reading from a file and stream. The input() call is read until a new line is reached. You can also read stdin before reaching EOF ( end of file ).

sys.argv , on the other hand, is just a list that is provided to you from a system containing all the arguments that were used to invoke the command from the shell. Technically, there is some type of restriction (on a system basis) for the maximum number of arguments that can be passed on the command line , so xargs exists (for calling your command with batches of your arguments, splitting).

Stdin

echo "I am stdin" | myCommand.py

... What is the concept under the hood, how is it done AFTER your program works:

read_from_stdin = input()

arg

myCommand.py "I am an arg"

And finally, reading from stdin / input() will not automatically break your words into a list. There are additional line-reading methods you can use. But you can also read by character, a certain number of characters at a time, or the entire amount of data at the same time.

+2
source

Parameters do not match program parameters. For example, here wget used with parameters:

 $ wget "I am a parameter!" 

Here cat used with input:

 $ cat Now you type. This is the input. 

This is also the reason for your error - you cannot specify parameters as such after starting the program.

+1
source

Source: https://habr.com/ru/post/909396/


All Articles