#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    17
    Rep Power
    0

    Making Python execute a command line in UNIX


    How can I have a script, let's say 'go.py' execute another script called 'run.py'?

    I just want 'go.py' to perfom the same function as when you type a command in UNIX like below:

    python run.py

    thanks for the help!
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Just a quick suggestion, you could use distutils. from distutils.core import run.py. You can learn all about this at python.org's tutorial. Also, with from distutils.core import run.py, yes, run.py would have to be in the same Directory as go.py.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    or if you want to just run it like unix does you could do

    Code:
    from os import system
     
     system("/path/to/file.py")
    Inside the "" you can put anything you want, it will go to shell and execute how shell would execute is, so if you put say "echo 'hehe'" it would put hehe to the screen like shell would.
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Does system() work like Shell() works in VB6? If you know VB6. For example, if i did system(notepad) would it open notepad.exe through system32 (on windows)?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    yes it does, everything that you would enter into cmd it will run exactly the same way with the system function in python.
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    If your running Python 2.4 then you should be using the subprocess module rather than os.system() etc. Heres a link to the modules documentation.

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

    This module intends to replace several other, older modules and functions, such as:


    os.system
    os.spawn*
    os.popen*
    popen2.*
    commands.*
    Hope this helps,

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

  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    17
    Rep Power
    0
    thanks guys,

    these are very heplful

IMN logo majestic logo threadwatch logo seochat tools logo