0

I want to pass data to a Python file using a pipe and also specifying an input file like:

cat file.txt|python script.py -u configuration.txt

I currently have this:

for line in fileinput.input(mode='rU'):
    print(line)

I know there can be something with sys.argv but maybe using fileinput there is a clean way to do it?

Thanks.

1 Answer 1

1

From the documentation:

If a filename is '-', it is also replaced by sys.stdin. To specify an alternative list of filenames, pass it as the first argument to input().

So you can create a list containing '-' as well as the contents of sys.argv[1:] (the default), and pass that to input(). Or alternatively just put - in the list of arguments of your Python program:

cat file.txt|python script.py -u - configuration.txt

or

cat file.txt|python script.py -u configuration.txt -

depending on whether you want data provided on standard input to be processed before or after the contents of configuration.txt.

If you want to do anything more complicated than just processing the contents of standard input as if it were an input file, you probably should not be using the fileinput module.

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

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.