473,769 Members | 8,305 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to run shell commands within python

How can I execute shell commands from within python. Specifically, I
am looking for something like the shell "cat". But this also made me
wonder how to execute shell commands anyway, just if i needed to do
that in the future.

Feb 16 '06 #1
7 66688
fileexit wrote:
How can I execute shell commands from within python. Specifically, I
am looking for something like the shell "cat". But this also made me
wonder how to execute shell commands anyway, just if i needed to do
that in the future.

You can use os.system() for that.
Feb 16 '06 #2
thanks... i was to hasty to post this question, i found a good answer
here:
http://groups.google.com/group/comp....dab847125f81b6

Feb 16 '06 #3
On 15 Feb 2006 23:21:09 -0800, fileexit <ma*****@gmail. com> wrote:
How can I execute shell commands from within python. Specifically, I
am looking for something like the shell "cat". But this also made me
wonder how to execute shell commands anyway, just if i needed to do
that in the future.


You can use os.system() or os.popen(), etc.
--
I like python!
My Blog: http://www.donews.net/limodou
NewEdit Maillist: http://groups.google.com/group/NewEdit
Feb 16 '06 #4
"fileexit" wrote:
thanks... i was to hasty to post this question, i found a good answer
here:
http://groups.google.com/group/comp....dab847125f81b6


that's an old thread, and Python has grown a few more ways to
deal with shell commands since then. if os.system isn't good en-
ough for you, subprocess is the preferred alternative:

http://www.python.org/doc/lib/module-subprocess.html

(also note that most trivial shell commands are better done in
python. most uses of cat, for example, can be trivially emulated
with one or two lines of python...)

</F>

Feb 16 '06 #5
use subprocess module

from subprocess import call
call(['cmd', 'arg1', 'arg2'], stdin='...', stdout='...')
eg:
call(['ls', '-l'])

Feb 16 '06 #6
In article <ma************ *************** ************@py thon.org>,
"Fredrik Lundh" <fr*****@python ware.com> wrote:
(also note that most trivial shell commands are better done in
python. most uses of cat, for example, can be trivially emulated
with one or two lines of python...)


Though the knowledge required to do this may be more trivial
for some of us than others! "cat" probably doesn't have much
going for it that a naive implementation would miss - some
versions will recreate "holes", but most applications will never
miss this. You can replace "mv" with os.rename() if you don't
care that it will fail when the destination is on a different
filesystem. Etc.

Donn Cave, do**@u.washingt on.edu
Feb 16 '06 #7
In <do************ ************@gn us01.u.washingt on.edu>, Donn Cave wrote:
You can replace "mv" with os.rename() if you don't
care that it will fail when the destination is on a different
filesystem. Etc.


If you care than use `shutil.move()` instead.

Ciao,
Marc 'BlackJack' Rintsch
Feb 17 '06 #8

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

Similar topics

0
6029
by: Will Seay | last post by:
At the end of this message I've pasted a script we're trying to modify slightly. I don't believe it is VBscript or javascript but these are the closest groups I could find with my limited programming knowledge. Basically, we are trying to add a few lines to this script that will execute a few shell commands (see comments at the very end of the code). We think this may be ActionScript2 but aren't sure. If you can offer any help, or know...
3
2265
by: Kory Wheatley | last post by:
Hi all, I've just started to learn Python and I have a question. What is the syntax to embed Unix commands, or call other programs within a Python script? for example I would like to use a system chmod unix command in the python script:
3
2111
by: Sara Khalatbari | last post by:
There are a lot of commands that I need to use in my code & I don't know how to do it Is there a way to use shell commands in Python code? __________________________________ Do you Yahoo!? Yahoo! Mail - Helps protect you from nasty viruses.
8
35043
by: zhiwei wang | last post by:
I remember that there is a function that could invoke shell command such as "rm" "cp", directly in .c file. But I could not recall its name, and I googled with nothing meaningful. I vaguely remember it is like: foo("cp file1 file2"); it is that simple, but cant remember. Could anyone here give me a clue?
0
1470
by: Bruceneedshelp | last post by:
My application starts logging and executes shell commands on startup. The application runs fine, except when it executes at boot time. In other words when I make it a "startup" application on my Windows XP box, it does everything except logging and shell commands. If I execute the application after Windows XP boots up, then it runs just fine. It seems as if the app is not getting its inherited functionality at boot time. Any help is...
10
2278
by: A.M | last post by:
Hi, I am having difficulty with shell scripting in Python. I use the following command to run a DOS command and put the return value in a Python variable:
1
4013
by: Riccardo Maria Bianchi | last post by:
Hello! :) I'm trying to run shell commands both with os.system() and subprocess.Popen() class. But I can't run aliases or function defined in my .bashrc file, like in the login interactive shell. Can you help me? Maybe have I to add some commands to load the .bashrc?
1
3557
by: raocheng | last post by:
Please see the following code. Suppose I have many shell commands to be executed. And I don't want to fork a sub shell for each command(eg: status,output = commands.getstatusoutput(cmd)) because it is too expensive. I want to use only one sub shell to execute all these commands and want to get each command's output. How can I accomplish this task ? Thanks in advance. =========================================== #!/usr/bin/env python
3
10388
by: Jie | last post by:
Hi all, i'm having trouble executing os.system('source .bashrc') command within python, it always says that source not found and stuff. Any clue? Thanks, Jie
0
9589
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
9423
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,...
1
9998
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
9865
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
8876
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...
1
7413
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
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();...
0
5310
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.