2

I am working on a program that requires to call another python script and truncate the execution of the current file. I tried doing the same using the os.close() function. As follows:

def call_otherfile(self):
    os.system("python file2.py") #Execute new script 
    os.close() #close Current Script 

Using the above code I am able to open the second file but am unable to close the current one.I know I am silly mistake but unable to figure out what's it.

OshoParth
  • 1,492
  • 2
  • 20
  • 44

3 Answers3

8

To do this you will need to spawn a subprocess directly. This can either be done with a more low-level fork and exec model, as is traditional in Unix, or with a higher-level API like subprocess.

import subprocess
import sys

def spawn_program_and_die(program, exit_code=0):
    """
    Start an external program and exit the script 
    with the specified return code.

    Takes the parameter program, which is a list 
    that corresponds to the argv of your command.
    """
    # Start the external program
    subprocess.Popen(program)
    # We have started the program, and can suspend this interpreter
    sys.exit(exit_code)

spawn_program_and_die(['python', 'path/to/my/script.py'])

# Or, as in OP's example
spawn_program_and_die(['python', 'file2.py'])

Also, just a note on your original code. os.close corresponds to the Unix syscall close, which tells the kernel that your program that you no longer need a file descriptor. It is not supposed to be used to exit the program.

If you don't want to define your own function, you could always just call subprocess.Popen directly like Popen(['python', 'file2.py'])

Edward Minnix
  • 2,889
  • 1
  • 13
  • 26
  • Traceback (most recent call last): File "v8function_handler.pyx", line 48, in cefpython_py27.V8FunctionHandler_Execute File "file1.py", line 62, in call_otherfile subprocess.Popen("python file2.py") File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__ errread, errwrite) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory – OshoParth Jul 03 '19 at 13:23
  • got the above error on using subprocess.Popen() however same works fine with os.system(). – OshoParth Jul 03 '19 at 13:24
  • Try it with `['python', 'file2.py']`. `os.system` takes the entire command you would run in shell. `subprocess` takes the full argv as a list. `Popen('python file2.py')` is looking for a program on your path named `python file2.py` – Edward Minnix Jul 03 '19 at 13:25
1

Use the subprocess module which is the suggested way to do that kind of stuff (execute new script, process), in particular look at Popen for starting a new process and to terminate the current program you can use sys.exit().

fedepad
  • 4,509
  • 1
  • 13
  • 27
-1

Its very simple use os.startfile and after that use exit() or sys.exit() it will work 100% #file 1 os.startfile("file2.py") exit()