Woltan
Woltan

Reputation: 14033

How to receive arguments via shell pipe in python?

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?

Upvotes: 16

Views: 18559

Answers (5)

user2343996
user2343996

Reputation: 153

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

Upvotes: 4

Michael Kent
Michael Kent

Reputation: 1734

I believe fileinput may be what you want.

Upvotes: 1

manojlds
manojlds

Reputation: 301587

I think you can do:

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

Upvotes: 3

user2665694
user2665694

Reputation:

import sys
for line in sys.stdin:
    print line

Upvotes: 21

David Claridge
David Claridge

Reputation: 6339

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.

Upvotes: 4

Related Questions