421,743 Members | 1,379 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 421,743 IT Pros & Developers. It's quick & easy.

How to run shell commands within python

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
use subprocess module

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

Feb 16 '06 #6

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.