[Biopython] subprocess.Popen problem

Brent Pedersen bpederse at gmail.com
Tue Jan 3 03:01:23 UTC 2012


On Mon, Jan 2, 2012 at 7:05 PM, Mic <mictadlo at gmail.com> wrote:
> With the following code:
> if __name__ == '__main__':
>        cmd_soap = 'soap ...'
>        proc = subprocess.Popen(cmd_soap, shell=True,
> stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>        returncode = proc.wait()
>        print "returncoded", returncode
>        stdout_value, stderr_value = proc.communicate()
>        print 'stderr ', stderr_value
>        for line in stderr_value:
>               print "!", line
>        if returncode == 1:
>                sys.exit(1)
>
> I get this:
>
> ! B
> ! e
> ! g
> ! i
> ! n
> !
> ! P
> ! r
> ! o
> ! g
> ! r
> ! a
> ! m
> !
> ! S
> ! O
> ! A
> ! P
> ! a
> ! l
> ! i
> ! g
> ! n
> ! e
> ! r
> ! /
> ! s
> ! o
> ! a
> ! p
> ! 2
> !
> ...
>
> instead of :
> Begin Program SOAPaligner/soap2
> ...
>
>
> What did I wrong?
>

stderr_value is a string. you are iterating over the letters.
you could do:

    for line in stderr_value.split("\n"):
        print "!", line

also, instead of using proc.wait/communicate(), you could do:

    for line in proc.stderr:
        print "!", line
    print proc.returncode


then you can see the output as it's generated (after buffering).



>
> On Thu, Nov 3, 2011 at 7:31 PM, Peter Cock <p.j.a.cock at googlemail.com>wrote:
>
>> On Thu, Nov 3, 2011 at 3:16 AM, Mic <mictadlo at gmail.com> wrote:
>> > Thank you, I wrote the following code and not sure whether it is what did
>> > write me.
>>
>> Depending on the tool I would check for a non-zero return code rather
>> than just treating 1 as an error.
>>
>> You are also not collecting stderr/stdout correctly. If you send them
>> to a pipe, the strings from the .communicate will be empty. Rather
>> reads from the process object's .stdout and .stderr handles. See:
>> http://docs.python.org/library/subprocess.html
>>
>> Peter
>>
> _______________________________________________
> Biopython mailing list  -  Biopython at lists.open-bio.org
> http://lists.open-bio.org/mailman/listinfo/biopython




More information about the Biopython mailing list