469,270 Members | 1,117 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

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 66394
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Kory Wheatley | last post: by
3 posts views Thread by Sara Khalatbari | last post: by
8 posts views Thread by zhiwei wang | last post: by
1 post views Thread by Riccardo Maria Bianchi | last post: by
3 posts views Thread by Jie | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.