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

Y

yichun.wei

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...python+EOF+perl&rnum=2&hl=en#b1788afc3150d280
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?
 
D

Dennis Lee Bieber

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.
As a rough guess... The first problem is that Popen runs ONE
command, with that command looking at a pipe for stdin. You are passing
four command lines with an empty stdin.

Try using (on windows) "cmd.exe" as the command, and pass the rest
on the pipe... But since you have that "rm -f", I guess a Linux/UNIX
system -- don't know how to specify a command interpreter is to be run
taking commands from the pipe...

The corresponding perl script runs fine:

open(QSUB, "| $qsubcmds -") || die "kao";

I have no idea of how to read that... To me, it looks like you are
opening a file whose name is "| $qsubcmds -"... Since I'm sure that
isn't a valid file name the next question is: what does "$qsubcmds"
represent -- not that in the Python example you first used "qsubcmds" as
the first argument to the Popen, and THEN you ALSO WRITE it on stdin.
print QSUB <<End;
echo
cd $cwd
$cmds $args
rm -f $files
End
close QSUB || die "kou";

How can we manage this in Python?

Uhmm... I'd probably try to do it all within Python...

home = os.getcwd()
print
os.chdir(cwd)
os.system("%s %s" % (cmds, args))
os.system("rm -f %s" % (files))
os.chdir(home)

Granted, some work may be needed if args and files are lists...
" ".join(args)
" ".join(files)


--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
N

Nick Craig-Wood

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...
... echo two
... echo three
... echo four
... """ two
three
four

As does this using stdin
... read B
... read C
... echo $C $B $A""" ... two
... three""")
three two one
(None, None)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top