473,569 Members | 2,542 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

capturing the output of external commands



Is there a Python function in any of the
standard-distribution modules that does
what the backticks do in Perl?

I want to run an external command and I'd
like its output to be captured directly
in my Python script in the form of a string
object.

The function os.system() or any of the
os.exec functions do not work for what I
have in mind because they do not capture
and return in Python the output produced
by the commands supplied to them as
arguments.

Avi Kak
ka*@purdue.edu

Jul 18 '05 #1
4 2434
See the standard 'commands' module. If that doesn't fit your needs, you
can capture the output directly with os.popen.

On Sun, Jul 25, 2004 at 02:51:06PM +0000, Avi Kak wrote:
Is there a Python function in any of the
standard-distribution modules that does
what the backticks do in Perl?

I want to run an external command and I'd
like its output to be captured directly
in my Python script in the form of a string
object.

The function os.system() or any of the
os.exec functions do not work for what I
have in mind because they do not capture
and return in Python the output produced
by the commands supplied to them as
arguments.

Avi Kak
ka*@purdue.edu

Jul 18 '05 #2


Thanks, Phil. The function commands.getout put()
is exactly what I was looking for.

Avi


On Sun, 25 Jul 2004 11:09:47 -0400, Phil Frost <in****@bitglue .com>
wrote:
See the standard 'commands' module. If that doesn't fit your needs, you
can capture the output directly with os.popen.

On Sun, Jul 25, 2004 at 02:51:06PM +0000, Avi Kak wrote:
Is there a Python function in any of the
standard-distribution modules that does
what the backticks do in Perl?

I want to run an external command and I'd
like its output to be captured directly
in my Python script in the form of a string
object.

The function os.system() or any of the
os.exec functions do not work for what I
have in mind because they do not capture
and return in Python the output produced
by the commands supplied to them as
arguments.

Avi Kak
ka*@purdue.edu


Jul 18 '05 #3
Avi Kak <ka*@purdue.edu > wrote in message news:<0p******* *************** **********@4ax. com>...
Is there a Python function in any of the
standard-distribution modules that does
what the backticks do in Perl?

I want to run an external command and I'd
like its output to be captured directly
in my Python script in the form of a string
object.


You can fake it by redirecting the output of whatever external command
you want to run to a file, and then reading the file into a string.
It seems like kind of a hack (how does Perl's implementation actually
work for this, though?), but it will work. Package it up as a
function and you're set.
Jul 18 '05 #4
Avi Kak wrote:
Is there a Python function in any of the
standard-distribution modules that does
what the backticks do in Perl?
Yes.
I want to run an external command and I'd
like its output to be captured directly
in my Python script in the form of a string
object.


In [1]: import commands

In [2]: commands.getout put?
Type: function
Base Class: <type 'function'>
String Form: <function getoutput at 0x874a094>
Namespace: Interactive
File: /usr/lib/python2.2/commands.py
Definition: commands.getout put(cmd)
Docstring:
Return output (stdout or stderr) of executing cmd in a shell.

You can get fancy using popen, and capture separately stdout/err, but for a
simple capture getoutput should do.

Best,

f
Jul 18 '05 #5

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

Similar topics

4
3529
by: Fernando Rodriguez | last post by:
Hi, How can I save the output of an external program, called with os.system()?
2
1655
by: PWR | last post by:
Platform: Windows Python: 2.3 Python Skill: Reading, Learning, but still newbie. Mission: My mission is writing a script that is triggered from my mail server. The script will execute and download a file. The email that triggered the message has a file size which is compared to the downloaded file to make sure the bytes match. Once...
2
4348
by: Hoegje | last post by:
I am writing a C++ program, which should create a sub- process to start a telnet session to another server. Then it should login to that server (on the telnet login) and execute one or more command(s) on the remote server. The C++ program provides all the input for this process (username, password, servername, commands, ...) and should capture...
1
5367
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my application (VB.NET) will start a process, redirect its stdout and capture that process' output, displaying it in a window. I've written a component...
14
9441
by: jas | last post by:
I would like to redirect the output from os.system to a variable, but am having trouble. I tried using os.popen(..).read() ...but that doesn't give me exactly what i want. ...this is windows by the way. For example: tmp = os.popen("hostname").read() ....works as expected.
2
2388
by: Doru-Catalin Togea | last post by:
Hi! On solaris, I call a program from python with os.system("some command > outputFile.txt" ) This redirects the output of "some command"'s stdout to "outputFile.txt". However I need to redirect or somehow capture the output from "some command"'s stderr to this file too. How can I do that?
1
1230
by: Marcel | last post by:
Hi, I used the following code Jochen Kalmbach suggested to run an external program: System::Diagnostics::ProcessStartInfo *si = new System::Diagnostics::ProcessStartInfo(); si->UseShellExecute = false; si->WorkingDirectory = S"D:\\Promela\\"; si->FileName = "D:\\Promela\\PAN.exe";
5
2201
by: Luigi | last post by:
Hi to all! I'd like to execute an external program capturing the stdout/stderr messages at "real-time". I mean that I don't want to wait for the end of the process. If I write a code like this: import os import sys class Runner:
5
3317
by: amit.uttam | last post by:
Hey everyone, I've recently jumped big time into python and I'm working on a software program for testing automation. I had a question about proper logging of output. What I would like is: 1. a function syslog (to log output to log file only) 2. a function stdout (to log output to stdout only) 3. a function sslog (to log output to both...
0
7614
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8125
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...
0
6284
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...
1
5513
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
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
3653
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...
1
2114
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
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
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.