1

I have the following scripts The perl script(fasta.pl) takes an input file(abc) and gives string.

 $ ./fasta.pl abc.txt

I first tried

p1= subprocess.Popen(["./pdb_fasta.pl","abc.txt"],stdout=subprocess.PIPE);

then I confirmed that p1 is a file object

>>> type(p1.stdout)
<type 'file'>

I have another script, count.py that takes a file as input

$ ./count.py p1.stdout

now when I try to use the p1.stdout for this script, I get error. I tried two different methods first one

p2= subprocess.Popen(["./count_aa.py",p1.stdout],stdout=subprocess.PIPE).stdout.read()

and the error is

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 595, in __init__
    errread, errwrite)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 1106, in _execute_child
    raise child_exception
TypeError: execv() arg 2 must contain only strings

second method: Here the error is because the script count_aa.py needs a file as an argument and stdin is not providing.

>>> p2= subprocess.Popen(["./count_aa.py"],stdin=p1.stdout,stdout=subprocess.PIPE).stdout.read()
Traceback (most recent call last):
  File "./count_aa.py", line 4, in <module>
    fil=open(sys.argv[1],'r').read()
IndexError: list index out of range
>>> 

I was thinking on this line to achieve the desired result where I pass the output from one child process as input to another. But this does not work as explained above.

p1= subprocess.Popen(["./pdb_fasta.pl","abc.txt"],stdout=subprocess.PIPE)
p2= subprocess.Popen(["./count_aa.py"],stdin=p1.stdout,stdout=subprocess.PIPE).stdout.read()

can some explain the mistakes here and give an example where stdin could be useful or how to use in this scenario. Thank you so much!!

4

1 回答 1

1

Your last code is equivalent to following shell command.

./pdb_fasta.pl abc.txt | ./count_aa.py

To make your last code to work, change count_aa.py to get input from stdin. For example:

import sys

n = 0
for line in sys.stdin:
    n += 1
print(n)
于 2013-06-15T04:10:22.027 回答