0

My understanding is that the redirection operator, <, should allow me to take text from a file and give it as input to another file as if I had written out the contents of that file. Here is what I am trying to do:

python code.py < input.txt

I expect this to act as though I had typed the contents of input.txt after python code.py, but instead it acts as if I passed no input. If I use cat, I get the contents of the file:

> cat input.txt
['2015-1-1','2015-5-1','2015-9-1','2015-10-1','2015-12-1','2016-1-1','2016-2-1','2016-4-1','2016-5-1'] [65,50,30,45,55,39,45,30,20]

And if I just copy and paste the contents of the file, I get the correct behavior.

I know this must be a really simple misunderstanding on my part, but I can't figure it out.

Kewl
  • 3,327
  • 5
  • 26
  • 45

1 Answers1

2

It's called Redirection, not piping, but you are correct that the < operator will push the file to the command. You can see this in action by using Sort instead of echo.

sort < input.txt

This will display the text file as a list, sorted alphabetically. Echo does not work with text files, so sending a text file to Echo simply runs "Echo".

If you just want to send a file to the command window, you can use Type instead, and not use the redirector.

type input.txt
TBowman
  • 615
  • 4
  • 15
  • Thanks! Is there a rule for what commands can use the redirection operator and which can't? Is there any way of doing something similar to redirection when redirection doesn't work? I've clarified my question to talk about the specific command I want (running a python script), the behavior of echo just seemed the same so I figured it was a simpler example. – Kewl Jun 25 '17 at 13:52
  • I would suggest you have a look at this question/answer: https://stackoverflow.com/questions/7033987/python-get-files-from-command-line – TBowman Aug 08 '17 at 19:10