-1

So I'm working on something right now and I'm trying to be able to run it in the command line. I was able to run my python file given this command "python ./pythonprogram.py arg1"

However,

"python ./pythonprogram.py < arg1"

should apparently work as well. E.g. this article says that the "symbol" indicates that arg1 is suppose to be inputted into the program on the left. Am i missing something? Is Python an exception and can arguments only be passed via a space? Thank you!

bea
  • 19
  • 3
  • 1
    `< arg` does not pass a command line argument. It sends the content of the file `arg1` to the program's stdin. This is really more of a SuperUser question than a SO question. – Aran-Fey Nov 08 '18 at 09:19

1 Answers1

2

They do different things.

This:

python ./pythonprogram.py arg1

means "run my program and pass the string arg1 as an argument"

This:

python ./pythonprogram.py < arg1

means "read the file arg1 and pass its content to stdin for my program".

See also How do you read from stdin in Python?

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • So for the former, I know that I'll have to import sys and put sys.argv to pass arg1 it...how do I do the latter, assuming that arg1 turns out to be a text file? When i enter "python ./pythonprogram.py < arg1" on my commandline I get an error. Thank you by the way! – bea Nov 08 '18 at 13:03
  • @bea [How do you read from stdin in Python?](https://stackoverflow.com/questions/1450393/how-do-you-read-from-stdin-in-python) – khelwood Nov 08 '18 at 13:21