When I use os.popen(cmd,'w'), I find that under windows, the stdout
of the child process disappears, instead of appearing in the DOS window
the script is invoked from. eg:
C:\type foo.py
import os
import sys
file = os.popen("nslookup", 'w')
file.write("google.com\n")
file.close()
C:\python foo.py
<-- nothing is printed
C:\>
This just seems wrong. The following DOS equivalent works fine:
C:\echo google.com | nslookup
Default Server: dns.erco.x
Address: 192.168.1.14
[..expected output..]
When I run the same python program on a unix box, the output
from 'nslookup' appears in the terminal, as I'd expect.
Shouldn't popen() be consistent in its handling of the child's
stdout and stderr across platforms?
Maybe I'm missing something, being somewhat new to python, but
an old hand at unix and win32 and functions like popen(). Didn't
see anything in the docs for popen(), and I googled around quite
a bit on the web and groups for eg. 'python windows popen stdout lost'
and found nothing useful.
FWIW, I'm using the windows version of python 2.5 from activestate. 2 6187
En Sat, 12 May 2007 00:46:16 -0300, Greg Ercolano <er**@3dsite.com>
escribió:
When I use os.popen(cmd,'w'), I find that under windows, the stdout
of the child process disappears, instead of appearing in the DOS window
the script is invoked from. eg: [...]
When I run the same python program on a unix box, the output
from 'nslookup' appears in the terminal, as I'd expect.
Shouldn't popen() be consistent in its handling of the child's
stdout and stderr across platforms?
Maybe I'm missing something, being somewhat new to python, but
an old hand at unix and win32 and functions like popen(). Didn't
see anything in the docs for popen(), and I googled around quite
a bit on the web and groups for eg. 'python windows popen stdout lost'
and found nothing useful.
Using the subprocess module is the recommended approach (as you can see on
the os.popen documentation) and does what you want:
C:\TEMP>type foo2.py
import subprocess
p = subprocess.Popen("nslookup", stdin=subprocess.PIPE)
p.stdin.write("google.com\n")
p.stdin.close()
C:\TEMP>python foo2.py
C:\TEMP>Servidor predeterminado: coyote.softlabbsas.com.ar
Address: 192.168.0.116
Servidor: coyote.softlabbsas.com.ar
Address: 192.168.0.116
Respuesta no autoritativa:
Nombre: google.com
Addresses: 64.233.187.99, 64.233.167.99, 72.14.207.99
>
C:\TEMP>
For more info about subprocess usage, see http://docs.python.org/lib/module-subprocess.html
--
Gabriel Genellina
On May 11, 8:46 pm, Greg Ercolano <e...@3dsite.comwrote:
When I use os.popen(cmd,'w'), I find that under windows, the stdout
of the child process disappears, instead of appearing in the DOS window
the script is invoked from. eg:
C:\type foo.py
import os
import sys
file = os.popen("nslookup", 'w')
file.write("google.com\n")
file.close()
C:\python foo.py
<-- nothing is printed
C:\>
This just seems wrong. The following DOS equivalent works fine:
C:\echo google.com | nslookup
Default Server: dns.erco.x
Address: 192.168.1.14
[..expected output..]
When I run the same python program on a unix box, the output
from 'nslookup' appears in the terminal, as I'd expect.
Shouldn't popen() be consistent in its handling of the child's
stdout and stderr across platforms?
Maybe I'm missing something, being somewhat new to python, but
an old hand at unix and win32 and functions like popen(). Didn't
see anything in the docs for popen(), and I googled around quite
a bit on the web and groups for eg. 'python windows popen stdout lost'
and found nothing useful.
FWIW, I'm using the windows version of python 2.5 from activestate.
Glad to see you're finally coming into the light Greg! I've used Rush
in a few different studios over the past couple of years. We even had
sushi once. :)
I'm no expert like you, but I think I can point you in the right
direction. You need os.popen2 which returns a tuple of file-like
objects. The first pointing to stdin, and the second pointing to
stdout. Write to stdin, and read from stdout.
import os
import sys
stdin, stdout = os.popen2("nslookup")
stdin.write("google.com\n")
stdin.close()
print stdout.read()
stdout.close()
I don't use windows much, but I believe the os.popen functionality is
being replaced by subprocess.Popen:
from subprocess import *
import sys
p = Popen("nslookup", shell=True, bufsize=1024, stdin=PIPE,
stdout=PIPE, close_fds=True)
p.stdin.write("google.com\n")
p.stdin.close()
print p.stdout.read()
p.stdout.close()
I found these: http://pydoc.org/2.4.1/subprocess.html http://docs.python.org/lib/module-subprocess.html
~Sean DiZazzo
Curious George This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Tung Wai Yip |
last post by:
I've build the following utility. It works so far but I want to make
sure I'm doing the right thing. I am running Python 2.3 on windows
2000.
def execute(cmd):
"""
execute cmd in sub-process,...
|
by: Matthew K Jensen |
last post by:
I've been trying to find a way to detect when a command run by
os.popen (or similar) has completed (in Windows, btw). I found such a
function (i don't remember the exact name and library), but the...
|
by: iker.arizmendi |
last post by:
On UNIX one can use popen* to get a pipe for reading, a pipe for
writing, and the exit code of the child process via a call to close()
on the last pipe. Is there any way, in principle, to simulate...
|
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...
|
by: Pierre Rouleau |
last post by:
Hi all,
I have a consistent test case where os.popen3() hangs in Windows. The
system hangs when retrieving the lines from the child process stdout.
I know there were several reports related to...
|
by: bayer.justin |
last post by:
Hi,
I am trying to communicate with a subprocess via the subprocess
module. Consider the following example:
<subprocess.Popen object at 0x729f0>
Here hey is immediately print to stdout of...
|
by: dmoore |
last post by:
Hi folks,
I've seen the following issue come up in multiple posts to this
mailing list:
I have a python program that spawns a child process with popen or
popen2 or popen3 or popen2.popen2...
|
by: gregpinero |
last post by:
Let's say I have this Python file called loop.py:
import sys
print 'hi'
sys.stdout.flush()
while 1:
pass
And I want to call it from another Python process and read the value
'hi'. How...
|
by: thedsadude |
last post by:
Hello,
I'm launching a script as follows:
<code>
p = subprocess.Popen()
p.wait()
</code>
If p.py writes to sys.stdout, then it is shown on the console.
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
|
by: jack2019x |
last post by:
hello, Is there code or static lib for hook swapchain present?
I wanna hook dxgi swapchain present for dx11 and dx9.
| |