How can I access the additional file streams like the comm
command in Python?
comm -23 <(sort -n Asub|uniq) <(sort -n A|uniq)
I know that I can access stdin via sys.stdin
, but how to access the other input stream?
Thanks for asking this question, as I actually didn't understand the behavior of <()
myself. It appears after some digging, however, that what it actually does it creates a temporary virtual file descriptor that it pipes the information from the subcommand into, then returns the name of that file descriptor. To see what I mean, look at this python program:
import sys
for arg in sys.argv:
print('|{}|'.format(repr(arg)))
When it is invoked like python3 thing.py <(cat a.txt) <(cat b.txt)
, you should see that the output is something like:
|'thing.py'|
|'/proc/self/fd/11'|
|'/proc/self/fd/12'|
So finally, to answer your question, what you need to do to read the data from that subprocess is to simply open that file descriptor as you would any other file. For example:
with open(sys.argv[1]) as f:
for line in f:
print(line.strip())
Which gives me an output like:
A
B
C
(Matching the contents of a.txt)
Hope that helps!