471,594 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

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

Perl has the ability to do the following:

print <<EOF;
...reams of text goes here...
EOF

Is there a Python equivalent of the above Perl code?

This thread has previous discussion on the topic:
http://groups.google.com/group/comp....788afc3150d280
we know one can use:

print """var1 = %(var1)s,
var2 = %(var2)s
... extra content..
""" % vars()

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.

The corresponding perl script runs fine:

open(QSUB, "| $qsubcmds -") || die "kao";
print QSUB <<End;
echo
cd $cwd
$cmds $args
rm -f $files
End
close QSUB || die "kou";

How can we manage this in Python?

Aug 24 '06 #1
1 5334
yi********@gmail.com <yi********@gmail.comwrote:
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 <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Aug 24 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by andrew | last post: by
4 posts views Thread by Aaron Reimann | last post: by
54 posts views Thread by Brandon J. Van Every | last post: by
11 posts views Thread by Nick Keighley | last post: by
4 posts views Thread by Birgit Rahm | last post: by
9 posts views Thread by Eric Lindsay | last post: by
232 posts views Thread by robert maas, see http://tinyurl.com/uh3t | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.