473,586 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.communica te(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 5585
yi********@gmai l.com <yi********@gma il.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.communica te(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="""ech o one
... echo two
... echo three
... echo four
... """
>>>
out = Popen(cmds, shell=True, stdin=PIPE)
one
two
three
four
>>>
As does this using stdin
>>cmds="""rea d A
... read B
... read C
... echo $C $B $A"""
>>out = Popen(cmds, shell=True, stdin=PIPE)
out.communica te("""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
1728
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 E:\xampp\workspace\test1\fragments.php on line 7 from this doc ...
4
2177
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 create a new <tr> with another 3, etc. Here is human code: print a <tr> if only 1, 2 or 3 results have been returned, show the results and...
54
6539
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 FRICKIN' COOL!!! ***MAN*** that would save me a buttload of work and make my life sooooo much easier!" As opposed to minor differences of this feature...
11
10073
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 Python (the example from Python in a Nutshell actually) will happily talk to a Python based client. If a C based client is substitued the connection...
4
9602
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
130082
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 document and closes it with the following code: <body onload="window.print(); window.close();"> This works correctly (or at least the way I expect...
9
5537
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 shell pieces like this: <pre><code> #!/bin/sh ftp -i -n ftp.server.com&lt; &lt;EOF user username password epsv4 cd /
4
2612
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 syntax; >, <, | That really shouldn't be too hard to wrap in a class, but so far I didn't succeed to do so this well, since I'm facing some trouble...
232
13195
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 set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an...
0
8338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6614
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.