473,386 Members | 1,748 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,386 software developers and data experts.

redirecting output of process to a file using subprocess.Popen()

I am trying to redirect stderr of a process to a temporary file and
then read back the contents of the file, all in the same python
script. As a simple exercise, I launched /bin/ls but this doesn't
work:

#!/usr/bin/python
import subprocess as proc
import tempfile
name = tempfile.NamedTemporaryFile(mode='w+b')
print 'name is '+ name.name

cmd = []
cmd.append('/bin/ls')
cmd.append('-l')
cmd.append('/tmp')
p = proc.Popen(cmd, stdout=name, stderr=proc.STDOUT, close_fds=True)
while True:
ret = p.poll()
if (ret is not None):
output = name.readlines()
print 'out = ', output
break

$python sub.py
name is /tmp/tmpjz4NJY
out = []
I tried calling flush() on the file object but this didn't help
either. Tried closing and re-opening the file, but closing the file
object results in it getting deleted. Can the above be made to work by
using tempfiles?

thanks
Jul 9 '08 #1
2 10016
On Jul 9, 7:32*pm, rpar...@gmail.com wrote:
I am trying to redirect stderr of a process to a temporary file and
then read back the contents of the file, all in the same python
script. As a simple exercise, I launched /bin/ls but this doesn't
work:

#!/usr/bin/python
import subprocess as proc
import tempfile
name = tempfile.NamedTemporaryFile(mode='w+b')
print 'name is '+ name.name

cmd = []
cmd.append('/bin/ls')
cmd.append('-l')
cmd.append('/tmp')
p = proc.Popen(cmd, stdout=name, stderr=proc.STDOUT, close_fds=True)
while True:
* *ret = p.poll()
* *if (ret is not None):
* * * output = name.readlines()
* * * print 'out = ', output
* * * break

$python sub.py
name is /tmp/tmpjz4NJY
out = *[]

I tried calling flush() on the file object but this didn't help
either. Tried closing and re-opening the file, but closing the file
object results in it getting deleted. Can the above be made to work by
using tempfiles?

thanks

your script works just fine.
The problem is that you have to move to the beggining of the file to
read the actual contents of
the file.
name.seek(0)

The whole program would be:

#!/usr/bin/python
import subprocess as proc
import tempfile
name = tempfile.NamedTemporaryFile(mode='w+b')
print 'name is '+ name.name

cmd = []
cmd.append('/bin/ls')
cmd.append('-l')
cmd.append('/tmp')
p = proc.Popen(cmd, stdout=name, stderr=proc.STDOUT, close_fds=True)
while True:
ret = p.poll()
if (ret is not None):
name.seek(0)
output = name.readlines()
print 'out = ', output
break
Jul 10 '08 #2
skeept wrote:
On Jul 9, 7:32 pm, rpar...@gmail.com wrote:
>I am trying to redirect stderr of a process to a temporary file and
then read back the contents of the file, all in the same python
script. As a simple exercise, I launched /bin/ls but this doesn't
work:

#!/usr/bin/python
import subprocess as proc
import tempfile
name = tempfile.NamedTemporaryFile(mode='w+b')
print 'name is '+ name.name

cmd = []
cmd.append('/bin/ls')
cmd.append('-l')
cmd.append('/tmp')
p = proc.Popen(cmd, stdout=name, stderr=proc.STDOUT, close_fds=True)
while True:
ret = p.poll()
if (ret is not None):
output = name.readlines()
print 'out = ', output
break

$python sub.py
name is /tmp/tmpjz4NJY
out = []

I tried calling flush() on the file object but this didn't help
either. Tried closing and re-opening the file, but closing the file
object results in it getting deleted. Can the above be made to work by
using tempfiles?

thanks


your script works just fine.
The problem is that you have to move to the beggining of the file to
read the actual contents of
the file.
name.seek(0)

The whole program would be:

#!/usr/bin/python
import subprocess as proc
import tempfile
name = tempfile.NamedTemporaryFile(mode='w+b')
print 'name is '+ name.name

cmd = []
cmd.append('/bin/ls')
cmd.append('-l')
cmd.append('/tmp')
p = proc.Popen(cmd, stdout=name, stderr=proc.STDOUT, close_fds=True)
while True:
ret = p.poll()
if (ret is not None):
name.seek(0)
output = name.readlines()
print 'out = ', output
break
This is an aside, but why the loop?

....
p = ...
p.wait()
name.seek(0)
....

(p.wait() returns p.returncode, just like p.poll() does, but you aren't
using it anyway...)
--
Jul 10 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: chuck amadi | last post by:
Hi I have managed to print the output of the get_payload to screen but I need to write to a file as I only require the email body messages from the mailbox.My script using the fp.readlines()...
1
by: py | last post by:
I am using subprocess.Popen to execute an executable, how can I terminate that process when I want? for example (on windows) p = subprocess.Popen("calc.exe") ..... now say I want to kill...
3
by: Ratko | last post by:
Hi all, I have a python gui app that launches multiple applications using subprocess.Popen class and prints their output in the gui (using PIPEs, threads and wxPython). Everything works great...
1
by: The Cruisemaniac | last post by:
Hi all, I'm trying to run a windows batch file from a python script using subprocess.popen(). The issue that I'm facing is that, if i give the batch file as parameter to the popen function,...
1
by: Mrown | last post by:
Hi, I'm currently writing a python program that relies on a CLI program. What I'm currently doing is using subprocess.Popen on Python 2.5.1. Here's the line that I'm currently running: child...
3
by: Jarek Zgoda | last post by:
Hi, all, anybody has an idea on how to set ulimit (-v in my case, linux) for process started using subprocess.Popen? -- Jarek Zgoda Skype: jzgoda | GTalk: zgoda@jabber.aster.pl | voice:...
1
by: Gabriel Genellina | last post by:
En Tue, 29 Jul 2008 10:04:46 -0300, Gordon Maria <Maria.Gordon@afconsult.comescribi�: You should call os.waitpid() after killing the child process, to let the OS free the resources allocated...
4
by: standerby | last post by:
I am writing a XML file suing XElement. How can I output a file like the following? I got an exception if if I use ':' in the element name or attribute name. Thanks, <?xml version="1.0"...
1
by: Mark Shewfelt | last post by:
Hello, I am attempting to use Popen() in a Windows service. I have a small Win32 .exe that I normally run through the os.popen2() function. I've written a class to work with the input and output...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.