2

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.

1
  • 2
    btw, cat somefile | foo should be spelled foo < somefile. Commented Mar 20, 2013 at 13:02

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You should add a python hashbang, too: #!/usr/bin/env python

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.