0

I was wondering how would you pass and then retrieve a list of numbers to a script using this syntax:

python3 program.py < 1 2 3 4

I know I can pass arguments using the following syntax:

python3 program.py 1 2 3 4

And then retrieve the arguments using sys.argv:

print(sys.argv)  # a list of arguments

I have seen around, but for some reason I was not able to do it.

Note that I am wondering if it's possible or not, I am not asking ways to read from the standard input that are different from my first example above.

nbro
  • 15,395
  • 32
  • 113
  • 196
  • Well then, I'll leave it here. Just note that `sys.stdin` is used to read from the standard input an when redirected it read's from the mentioned input – Bhargav Rao Mar 20 '15 at 20:37
  • If you had a simple utility that did nothing but echo its command line arguments to stdout, you could do something like;`util 1 2 3 4 > python3 program.py` – martineau Mar 20 '15 at 20:37
  • @martineau do you mean `util 1 2 3 4 | python3 program.py`? – Zero Piraeus Mar 20 '15 at 20:47
  • @Zero: Yes, that's what I should have written. – martineau Mar 20 '15 at 20:56
  • Taking a cue from @martineau `echo 1 2 3 4 | python test.py` would send a string `"1 2 3 4"` to the python program in which you can split based on space and map each to a set of numbers. – Bhargav Rao Mar 20 '15 at 21:02
  • Some unix shells allow something called a "here document" or "inline file" using `<<` — see [_How can I write a here doc to a file in Bash script?_](http://stackoverflow.com/questions/2953081/how-can-i-write-a-here-doc-to-a-file-in-bash-script) – martineau Mar 21 '15 at 13:21

2 Answers2

2

You've misunderstood the syntax of your proposed command.

Here's a simple python program that prints out the contents of standard input and command-line arguments:

# program.py

import sys

print("stdin:", sys.stdin.read())
print("args:", sys.argv)

... and here's what happens when you call it with your proposed syntax:

$ python3 program.py < 1 2 3 4
stdin: This is the content of file '1'.

args: ['program.py', '2', '3', '4']

In other words, your syntax redirects the content of a file '1' to standard input, and supplies the arguments '2', '3' and '4' to the program.

So, no, what you want to do isn't possible - not because of Python, but because of the way the shell works.

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
1

Yes, you can pass a list of numbers using redirection. There is here-string syntax in bash:

$ python3 -c 'print(sum(map(int, input().split())))' <<<'1 2 3'
6

Though it is more straightforward to pass it as an argument list:

$ python -c 'import sys; print(sum(map(int, sys.argv[1:])))' 1 2 3
6

< 1 tries to redirect stdin from the file named 1. It is probably not what you want.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • Terrific idea of using `>>>` ! I broke my head and finally came up with a poor version [here](http://stackoverflow.com/questions/29175282/is-it-possible-to-pass-a-list-of-numbers-as-argument-using/29175443#comment46569254_29175282). I guess this is it (what the OP wants) – Bhargav Rao Mar 20 '15 at 21:24