1

How come this command python test.py <(cat file1.txt) does not work accordingly. I could've sworn I had this working previously. Basically, I would like to get the output of that cat command as an input to the python script.

This command

cat file1.txt | python test.py

works okay, which outputs:

reading:  file11
reading:  file12

Which are based on the following scripts/files below.

The reason I want this to work is because I really want to feed in 2 input files like

python test.py <(cat file1.txt) <(cat file2.txt)

And would like some python output like:

reading:  file11 file21
reading:  file12 file22

I know this is a very simple example, and I can just read in or open() both files inside the python script and iterate accordingly. This is a simplified version of my current screnario, the cat command is technically another executable doing other things, so its not as easy as just reading/opening the file to read.

Sample script/files:

test.py:

import sys

for line in sys.stdin:
   print("reading: ", line.strip())

sys.stdin.close()

file1.txt:

file11
file12

file2.txt:

file21
file22
5
  • You can just do cat file1.txt file2.txt | python test.py Commented Dec 12, 2021 at 23:40
  • FYI cat function can take more parameters Commented Dec 12, 2021 at 23:40
  • Right, cat can take more files, but like I said. The cat is really just a sample, it is really another script/executable doing other things so I would like <() <() format to work Commented Dec 12, 2021 at 23:49
  • A process has only one stdin, so the <() <() format can't work. Commented Dec 12, 2021 at 23:54
  • Maybe you should show an actual example that demonstrates why cat file1.txt file2.txt | python test.py isn't sufficient. Commented Dec 12, 2021 at 23:56

2 Answers 2

1

changing test.py to:

import sys

input1 = open(sys.argv[1], "r")
input2 = open(sys.argv[2], "r")

for line1, line2 in zip(input1, input2):
   print("reading: ", line1.strip(), line2.strip())

input1.close()
input2.close()

will enable python test.py <(cat file1.txt) <(cat file2.txt) to work

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

Comments

0

Actually it depends on shell you are using. I guess you use bash which unfortunately can't have it working as only last redirection from specific descriptor is taken. You could create temporary file, redirect output of scripts to it and then feed your main script with tmp file.

Or if you don't mind you can switch e.g to zsh, which has such feature enabled by default.

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.