16

I would like to do something like this:

find -name "foo*" | python main.py

and access all the files that were found by the find program. How do I access that in Python?

1
  • What about command which ran for a long time, e.g top. Commented Oct 18, 2015 at 8:55

5 Answers 5

21
import sys
for line in sys.stdin:
    print line
Sign up to request clarification or add additional context in comments.

Comments

4

Use sys.stdin.read() or raw_input()

A pipeline just changes the stdin file descriptor to point to the pipeline that the command on the left is writing to.

Comments

4

I Like using $(...) for command-line arguments that are dependent on some other program. I think this would work for your program python main.py $(find -name "foo*"). Found here

Comments

3

I think you can do:

import sys
print sys.stdin.readlines() #or what you want

Comments

1

I believe fileinput may be what you want.

Comments

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.