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

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 66669
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***************************************@python. org>,
"Fredrik Lundh" <fr*****@pythonware.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.washington.edu
Feb 16 '06 #7
In <do************************@gnus01.u.washington.ed u>, 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
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...
3
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...
3
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...
8
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...
0
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...
10
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
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...
1
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...
3
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
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...
0
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...
0
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,...

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.