I am not sure how to capture the output of a command
using subprocess without creating a temp file. I was
trying this:
import StringIO
import subprocess
file = StringIO.StringIO()
subprocess.call("ls", stdout = file)
Traceback (most recent call last):
File "<stdin>", line 6, in ?
File "/usr/local/lib/python2.4/subprocess.py", line 413, in call
return Popen(*args, **kwargs).wait()
File "/usr/local/lib/python2.4/subprocess.py", line 534, in __init__
(p2cread, p2cwrite,
File "/usr/local/lib/python2.4/subprocess.py", line 840, in _get_handles
c2pwrite = stdout.fileno()
AttributeError: StringIO instance has no attribute 'fileno'
So how do I get the output into a string?
I thought that the idea of StringIO was that it could be
used where a file was expected.
Thanks,
Toby
** Posted from http://www.teranews.com ** 4 13478
On Tue, Apr 15, 2008 at 10:36 PM, Tobiah <to**@tobiah.orgwrote:
I am not sure how to capture the output of a command
using subprocess without creating a temp file. I was
trying this:
import StringIO
import subprocess
file = StringIO.StringIO()
subprocess.call("ls", stdout = file)
Traceback (most recent call last):
File "<stdin>", line 6, in ?
File "/usr/local/lib/python2.4/subprocess.py", line 413, in call
return Popen(*args, **kwargs).wait()
File "/usr/local/lib/python2.4/subprocess.py", line 534, in __init__
(p2cread, p2cwrite,
File "/usr/local/lib/python2.4/subprocess.py", line 840, in _get_handles
c2pwrite = stdout.fileno()
AttributeError: StringIO instance has no attribute 'fileno'
So how do I get the output into a string?
I thought that the idea of StringIO was that it could be
used where a file was expected.
For basic file-like read and write. But it won't provide a file handle
since there is no 'real' file. Also, from 2.3.9 File Objects:
"File-like objects which do not have a real file descriptor should not
provide this method!"
You should use the PIPE subprocess argument to capture output. From
the tutorial:
6.8.3.1 Replacing /bin/sh shell backquote
output=`mycmd myarg`
==>
output = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0]
On Tue, 15 Apr 2008 13:36:11 -0700, Tobiah wrote:
I am not sure how to capture the output of a command
using subprocess without creating a temp file. I was
Sorry, I jumped into a secondary level of the
docs, and didn't see it all. I guess I can
use communicate() to get the output.
Still, about StringIO...
trying this:
import StringIO
import subprocess
file = StringIO.StringIO()
subprocess.call("ls", stdout = file)
Traceback (most recent call last):
File "<stdin>", line 6, in ?
File "/usr/local/lib/python2.4/subprocess.py", line 413, in call
return Popen(*args, **kwargs).wait()
File "/usr/local/lib/python2.4/subprocess.py", line 534, in __init__
(p2cread, p2cwrite,
File "/usr/local/lib/python2.4/subprocess.py", line 840, in _get_handles
c2pwrite = stdout.fileno()
AttributeError: StringIO instance has no attribute 'fileno'
So how do I get the output into a string?
I thought that the idea of StringIO was that it could be
used where a file was expected.
Thanks,
Toby
** Posted from http://www.teranews.com **
** Posted from http://www.teranews.com **
>
Still, about StringIO...
The module description says you can use it to read and write strings
as files, not that you can use strings *everywhere* you can use files.
In your specific case, StringIO doesn't work, because the stdout
redirection takes place at the operating system level (which uses real
file handles), rather than in a python library (for which StringIO
would probably work).
David.
On Tue, Apr 15, 2008 at 11:16:01PM +0200, David wrote:
Still, about StringIO...
The module description says you can use it to read and write strings
as files, not that you can use strings *everywhere* you can use files.
In your specific case, StringIO doesn't work, because the stdout
redirection takes place at the operating system level (which uses real
file handles), rather than in a python library (for which StringIO
would probably work).
David.
-- http://mail.python.org/mailman/listinfo/python-list
Just a note to all of those who are interested.
I have yet to get this to work properly for an app which runs indefinitely and you want to read the output at a specified interval. Right now the only way I can read is if the _close() method has been called.
Anyway, I wrote a wrapper around it so I could easily change the
implementation if I could ever find a better solution. Here's my code:
===========================
import subprocess
import os
import select
class ProcessMonitor:
def __init__(self):
self.__process = None
self.__stdin = None
self.__stdout = None
def _create(self, process):
self.__process = subprocess.Popen(process, stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True)
self.__stdin = self.__process.stdout
self.__stdout = self.__process.stdout
def _close(self):
os.kill(self.__process.pid,9)
def _listen(self):
"""
get from stdout
"""
return "".join(self.__stdout.readlines())
def _listen2(self):
"""
My attempt at trying different things.
"""
inp, out = self.__process.communicate("")
print out
--
Nick Stinemates (ni**@stinemates.org) http://nick.stinemates.org This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: McBooCzech |
last post by:
Hi all,
I am trying to use subprocess module on Linux/Python-2.4.1, but I can't
dig throught.
I need to call executable which needs two parameters to be ginven (the
serial port and the file...
|
by: Martin Jørgensen |
last post by:
Hello again,
Sorry to bother but I guess my C++ book isn't very good since it
obviously contains errors so the program code doesn't work with g++.
However I don't understand what the problem...
|
by: Alex Kachanov |
last post by:
Hi!
running py module with the following code from shell:
__
dir=os.path.join(os.path.expanduser("~/domains/domain.com/html"),'test')
subprocess.call()
__
returns:
__
find:...
|
by: Jim |
last post by:
Hello,
I need a program that will traverse a directory tree to ensure that
there
are unix-style line endings on every file in that tree that is a text
file.
To tell text files from others I...
|
by: timw.google |
last post by:
Hi
I want to write a python script that runs rsync on a given directory
and host. I build the command line string, but when I try to run
subprocess.call(cmd), or p=subprocess.Popen(cmd,...
|
by: Michael George Lerner |
last post by:
Hi,
(Python 2.5, OS X 10.4.10)
I have a program called pdb2pqr on my system. It is installed so that
"pdb2pqr" is in my path and looks like:
#\!/bin/zsh -f
/sw/share/pdb2pqr/pdb2pqr.py "$@"...
|
by: David Pratt |
last post by:
Hi. I am trying to replace a system call with a subprocess call. I have
tried subprocess.Popen and subprocess.call with but have not been
successful. The command line would be:
svnadmin dump...
|
by: freddieMaize |
last post by:
Is there a possibility of getting a XML output if i do a google search??
fREDDIE
|
by: siavashr |
last post by:
Hi,
I am new to python, and I am trying to do the following from my python code:
echo 1 > /sys/devices/omapdss/display0/enabled
I have tried to do this with the following functions:
...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |