473,699 Members | 2,386 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

running a shell command from a python program

Hi,
I'm a newbie, so please be gentle :-)

How would I run a shell command in Python?

Here is what I want to do:
I want to run a shell command that outputs some stuff, save it into a
list and do stuff with the contents of that list.

I started with a BASH script actually, until I realized I really needed
better data structures :-)

Is popen the answer? Also, where online would I get access to good
sample code that I could peruse?

I'm running 2.2.3 on Linux, and going strictly by online doc so far.

Thanks!
S C

Jul 18 '05 #1
4 1822
Am Wed, 23 Feb 2005 07:00:31 -0800 schrieb Sandman:
Hi,
I'm a newbie, so please be gentle :-)

How would I run a shell command in Python? [cut] Is popen the answer? Also, where online would I get access to good
sample code that I could peruse?


Yes, popen is the answer. I recommend popen4 because it avoids
deadlocks if there is output on stdout and stderr.

Example:
stdout, stdin = popen2.popen4(" tidy -q -errors '%s'" % htmlfile)

HTH,
Thomas

--
Thomas Güttler, http://www.thomas-guettler.de/
Jul 18 '05 #2
Doh, use the search Luke:

http://www.wellho.net/forum/Programm...om-Python.html

Seems like popen is the way to go.

S

Jul 18 '05 #3
Sandman wrote:
How would I run a shell command in Python?

Here is what I want to do:
I want to run a shell command that outputs some stuff, save it into a
list and do stuff with the contents of that list.


There's a Python Cookbook example that should fit nicely with what
you're trying to do here:

<http://aspn.activestat e.com/ASPN/Cookbook/Python/Recipe/52296>
--
Leif Biberg Kristensen
Jul 18 '05 #4
In Python 2.4, use the new subprocess module for this. It subsume the
popen* methods.
Hi,
I'm a newbie, so please be gentle :-)

How would I run a shell command in Python?

Here is what I want to do:
I want to run a shell command that outputs some stuff, save it into a
list and do stuff with the contents of that list.

I started with a BASH script actually, until I realized I really needed
better data structures :-)

Is popen the answer? Also, where online would I get access to good
sample code that I could peruse?

I'm running 2.2.3 on Linux, and going strictly by online doc so far.

Thanks!
S C


Jul 18 '05 #5

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

Similar topics

2
14355
by: Jorgen Grahn | last post by:
I couldn't think of a good solution, and it's hard to Google for... I write python command-line programs under Win2k, and I use the bash shell from Cygwin. I cannot use Cygwin's python package because of a binary module which has to be compiled with Visual C 6. My scripts start with a '#!/usr/bin/env python' shebang, as God intended. Now, I assume I can make cmd.exe run foo.py by asociating *.py with the python interpreter.
5
5314
by: Shalen chhabra | last post by:
Hey, Can anyone give me a snippet for running a python program over all the files in the directory. For ex: I have ten files in a directory and I want to run a python program against all of these files, I wish to do the same using another python code instead of running each of these files one by one, which would be cumbersome giving the argv of each file every single time. This can be easily done using a shell script but I just...
9
1975
by: Erik Geiger | last post by:
Hi, sorry, my english ist not that got but I'll try. I have a running python script (capisuit incoming.py). This script shall start a linux shell script. If I start this script like os.system(/paht/to shellscipt.sh) the python scipt waits for the exit of the shell script and then goes on with the rest of the python script. How to start a shell script without waiting for the exit of that shell
2
7488
by: bbxrider | last post by:
for win2k adv server/iis5.0 trying to run an external program from my asp routine that has multiple parameters, see following set shell = server.createobject("wscript.shell") shell.Run """f:\phsData\htmldoc\htmldoc.exe"" --webpage -f phsnew.pdf phsnew.htm" program either doesn't get invoked, or has an error, but since it has no error log i can't check that,
8
1333
by: Siemel Naran | last post by:
Hi. I'm writing a command shell that reads commands from standard input. At this point I have the command in a std::string. Now I want to execute this command in the shell. From the Borland newsgroups I learned that there is a function in stdlib.h called system. int system(const char *command); First question, is the system command ANSI compliant. Because I include <cstdlib> and write std::system(command.c_str()); it looks like an...
3
1954
by: James Stroud | last post by:
Hello All, I am helping someone write a python script to run their DOS application through an SSH terminal. It seems that this program wants to access a DOS shell and send output there. If running remotely, this causes a problem because it locks up the program. The program seems (to me) to be looking for some non-existant DOS shell to send its output to. How might I emulate this shell (or whatever it is) with python? I have tried...
12
2472
by: Krypto | last post by:
I have been using python shell to test small parts of the big program. What other ways can I use the shell effectively. My mentor told me that you can virtually do anything from testing your program to anything in the shell. Any incite would be useful.
4
1867
by: Propad | last post by:
Hello, I know this issue pops up once in a while, but I haven't found a good answer to it. I need to debug a long running application under windows. The application is a combined java/python framework for testing ECUs in the automotive industry. Basically, the Java GUI (Eclipse-based) starts test-cases written in Python and provides the console where the test-logs are seen. When there is a exception somewhere in the testcases (or the...
7
6233
by: Samuel A. Falvo II | last post by:
I have a shell script script.sh that launches a Java process in the background using the &-operator, like so: #!/bin/bash java ... arguments here ... & In my Python code, I want to invoke this shell script using the Subprocess module. Here is my code: def resultFromRunning_(command):
0
8685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8613
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9172
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8908
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7745
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5869
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3054
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
3
2008
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.