Hoopdady
Hoopdady

Reputation: 2356

Pipe Output to Python File

How do a make a python script take input that has been piped to it. Is that a sys.argv moment. To be clear I want to figure out how to code the python side to receive input like this:

cat someFile | domeSomething.py

Can this be done? Again, to clarify, I'm not wanting to write this as passing a filename to my script and then using open(filename) I want to get piped input. Thanks for the help.

Upvotes: 2

Views: 1477

Answers (1)

Pavel Anossov
Pavel Anossov

Reputation: 62908

To get piped input, you need to read from stdin:

import sys
print sys.stdin.read()

stdin is a file, so feel free to use any of the methods of a file object.

Upvotes: 5

Related Questions