how do i run another script from my python script

Discussion in 'Python' started by nephish@xit.net, Oct 27, 2005.

  1. Guest

    hey there all,
    i have been looking for a way to run a php command line script from my
    python script.

    here is what i want to do:

    if x = 4:
    execute php4 testin.php
    else:
    execute php4 testout.php

    and i also need the script to wait untill the php4 script is done
    (which i think is the default ) before continuing to run.

    i know this is possible, but how?
    thanks
     
    , Oct 27, 2005
    #1
    1. Advertising

  2. Steve Holden Guest

    wrote:
    > hey there all,
    > i have been looking for a way to run a php command line script from my
    > python script.
    >
    > here is what i want to do:
    >
    > if x = 4:
    > execute php4 testin.php
    > else:
    > execute php4 testout.php
    >
    > and i also need the script to wait untill the php4 script is done
    > (which i think is the default ) before continuing to run.
    >
    > i know this is possible, but how?
    > thanks
    >

    import os
    if x = 4:
    script = "testin.php"
    else:
    script = "testout.php"
    os.system(script)

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC www.holdenweb.com
    PyCon TX 2006 www.python.org/pycon/
     
    Steve Holden, Oct 27, 2005
    #2
    1. Advertising

  3. Guest

    man, thats just too easy.
    thanks much !
     
    , Oct 27, 2005
    #3
  4. Steve already answeared to your question, regaring PHP script

    if this would be python script, you could run it by import'ing it

    #a.py
    print "in a"
    ------------

    #b.py
    import a # prints "in a"
    print "in b"
    ------------

    and of course other solutions
    import os
    if os.fork()==0:
    os.execv("/bin/cmd_here", ["-blabla"])
    else:
    # parent here

    or maybe using threading

    >>> class FileWatcher(th.Thread):

    .... def __init__(self, filename):
    .... th.Thread.__init__(self)
    .... self.filename = filename
    .... def run(self):
    .... import time
    .... from os.path import exists
    .... while not exists(self.filename):
    .... time.sleep(0.5)
    .... print "not there"
    ....
    >>> f = FileWatcher("/pool/xyz")
    >>> f.start()
    >>> not there

    not there
    not there
    not there
    not there
    not there
    not there
    not there
    not there
    not there
    not there
    not there

    >>>



    hth, Daniel
     
    =?ISO-8859-1?Q?Daniel_Sch=FCle?=, Oct 27, 2005
    #4
  5. Mike Meyer Guest

    Daniel Schüle <-karlsruhe.de> writes:
    > Steve already answeared to your question, regaring PHP script
    > if this would be python script, you could run it by import'ing it


    That's not very pythonic. Better is to provide a function in the
    script to run it (say run), then run that in the script iff it's
    the script is being executed:

    if __name__ == '__main__':
    run()

    Then to use it from another script, you'd do something like:

    import myscript
    myscript.run()

    > or maybe using threading


    Launching new threads as part of the import process is
    *dangerous*. There are some nasty bugs lurking there that cause things
    like your import never finishing. If you need to start a thread in a
    module, the approach outlined above avoids those bugs.

    <mike
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
     
    Mike Meyer, Oct 27, 2005
    #5
  6. Guest

    well i know i dont want to do another thread, my program has six
    running all the time.
    whew. that can get nuts.
    i mean, this will come to me easier later i am sure. But right now.....
    simple is best.
    thanks for all the suggestions, gents.
    right now i am just doing a varient of Steves suggestion, and so far,
    its working.

    thanks again,
    sk
     
    , Oct 28, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    9
    Views:
    998
    Paddy O'Loughlin
    Feb 27, 2009
  2. Dan Lee

    How to run another python script?

    Dan Lee, Feb 14, 2011, in forum: Python
    Replies:
    2
    Views:
    261
    Jean-Michel Pichavant
    Feb 14, 2011
  3. souleymane yo
    Replies:
    1
    Views:
    171
    Thomas Jollans
    Jul 23, 2011
  4. Robert Cohen
    Replies:
    3
    Views:
    291
    Andrew Durstewitz
    Jul 15, 2003
  5. 3lionz Wexler
    Replies:
    2
    Views:
    711
    Suraj Kurapati
    Aug 24, 2010
Loading...

Share This Page