473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 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 5570
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: andrew | last post by:
Ok, pretty new to this php stuff, but good with perl/python etc. What is wrong with this php document ??? I get this error: Parse error: syntax error, unexpected $end in...
4
by: Aaron Reimann | last post by:
I am trying to build a table using results from a database query. Right now (code below), the code displays each result in a new <tr>. I am wanted to display 3 results in one <tr> </tr>, and then...
54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
11
by: Nick Keighley | last post by:
I'm probably missing something rather obvious, but is there a known problem with getting a Python based socket program to communicate with a C based socket program? A simple echo server written in...
4
by: Birgit Rahm | last post by:
Hello, is there a possibility to connect to RMI Middleware from Python or alternativ to Java? -- Mit freundlichen Grüßen / best regards Birgit Rahm
22
by: stephen | last post by:
I have created an order form that users javascript to create a new html document when the customers clicks the "print page" button. Once the new document has been created it then prints the...
9
by: Eric Lindsay | last post by:
I can't figure how to best display little snippets of shell script using <pre>. I just got around to organising to bulk validate some of my web pages, and one of the problems occurs with Bash...
4
by: jelle | last post by:
Hi, I use python quite a bit to couple different programs together. Doing so has been a _lot_ easier since subprocess came around, but would really like to be able to use the succinct shell...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.