terminate python program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gopython
    New Member
    • Mar 2007
    • 18

    terminate python program

    The python program usually terminate itself by the end of the file. Is there anyway to control how the python program should terminate? For example:

    Code:
    import os
    
    os.system ('notepad.exe')
    # And here terminate the python program after the notepad is launch
    Thank you
  • dshimer
    Recognized Expert New Member
    • Dec 2006
    • 136

    #2
    Maybe it's more a matter of using a command that doesn't wait for termination.
    Code:
    import os
    os.execlp('notepad','')
    which will start up notepad then go on to exit.
    You could even put it in a try/except block in case there was a problem.
    For example by introducing an error (mispelled the program name)
    Code:
    import os
    try:os.execlp('notpad','')
    except:print 'There was a problem'
    I still get a clean exit.

    Comment

    • ghostdog74
      Recognized Expert Contributor
      • Apr 2006
      • 511

      #3
      Originally posted by gopython
      The python program usually terminate itself by the end of the file. Is there anyway to control how the python program should terminate? For example:

      Code:
      import os
      
      os.system ('notepad.exe')
      # And here terminate the python program after the notepad is launch
      Thank you
      Code:
      >>> import subprocess
      >>> pid = subprocess.Popen(["notepad.exe", " "]).pid
      Please see the docs for subprocess

      Comment

      • gopython
        New Member
        • Mar 2007
        • 18

        #4
        Thanks for the pointer. I thought I try out the sys.exit (0). So that when the python program run the notepad.exe then the python program will just kill itself. But that does not do anything for me. The os.execlp works out fine. I really appreciate it.
        Last edited by gopython; Mar 23 '07, 05:31 PM. Reason: Need to add more comments

        Comment

        Working...