#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    4
    Rep Power
    0

    running bash command


    hey people

    total python noob here

    how can i run a bash command from within a python script - like perl does with the `ls` backticks?
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    There are two ways you can do this on *nix... the 'commands' module is by far the best, the disadvantage being that its only available on *nix!

    http://www.python.org/doc/2.3.3/lib/...-commands.html

    Your other option is the system() function from the os module i.e.

    Code:
    pseudocode
    import os
    os.system('command')
    http://www.python.org/doc/2.3.3/lib/os-process.html

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    os.system won't help if you want the output. commands.getoutput is the easiest way, but there are also the popen* modules to allow advanced control of all the possible file-handle combinations involved with process invocation and management.

    However, for specifically getting directory contents, use os.listdir.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    4
    Rep Power
    0
    thanks guys - you rock!

IMN logo majestic logo threadwatch logo seochat tools logo