Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 481
Woltan Avatar asked May 24 '11 06:05

Woltan


People also ask

How do you capture an argument in Python?

To access command-line arguments from within a Python program, first import the sys package. You can then refer to the full set of command-line arguments, including the function name itself, by referring to a list named argv. In either case, argv refers to a list of command-line arguments, all stored as strings.

Does Python have command line arguments?

Python sys module stores the command line arguments into a list, we can access it using sys. argv . This is very useful and simple way to read command line arguments as String. Let's look at a simple example to read and print command line arguments using python sys module.

Does Python support argv?

argv is a list in Python that contains all the command-line arguments passed to the script. It is essential in Python while working with Command Line arguments.


2 Answers

import sys
for line in sys.stdin:
    print line
like image 188
Andreas Jung Avatar answered Sep 30 '22 18:09

Andreas Jung


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.

like image 41
David Claridge Avatar answered Sep 30 '22 17:09

David Claridge