Can Python do Perl's print <<EOF; notation? - popen, subprocess works?

Nick Craig-Wood nick at craig-wood.com
Thu Aug 24 05:30:03 EDT 2006


yichun.wei at gmail.com <yichun.wei at gmail.com> wrote:
>  However, when the code in the string was actually
>          qsubcmds = """
>    echo
>    cd %(cwd)s
>    %(cmds) %(args)
>    rm -f %(files)s
>          """ % vars()
> 
>  in which %(cmd)s folks a subprocess, when this string was write to some
>  pipe, e.g.:
> 
>    QSUB = Popen(qsubcmds, shell=True, stdin=PIPE)
>    print >> QSUB.stdin, qsubcmds
>    (or Popen.communicate(qsubcmds))
> 
>  the "rm -f " was not executed in my case.

Not sure why you are sending the mini shell script to itself on stdin?
That doesn't seem to make sense.

Come up with a simple example everyone can try and post it running in
an interactive python session.  Here are my attempts to replicate your
problem.

This runs fine...

    >>> from subprocess import *
    >>> cmds="""echo one
    ... echo two
    ... echo three
    ... echo four
    ... """
    >>> 
    >>> out = Popen(cmds, shell=True, stdin=PIPE)
    >>> one
    two
    three
    four
    
    >>> 

As does this using stdin

    >>> cmds="""read A
    ... read B
    ... read C
    ... echo $C $B $A"""
    >>> out = Popen(cmds, shell=True, stdin=PIPE)
    >>> out.communicate("""one
    ... two
    ... three""")
    three two one
    (None, None)
    >>> 

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list