user13562014
user13562014

Reputation: 1

Closing a python file inside another python file

I have made a code to open an inputed python file on key command in python pygame_functions

import os, pygame_functions

if spriteclicked(Sprite1):
        os.system('file.py')

Similarly how do I close an inputed python file on key command

Upvotes: 0

Views: 78

Answers (1)

Glenn Mackintosh
Glenn Mackintosh

Reputation: 2779

Your question is not very clear. By 'inputed file' do you mean that the name of the file is from user input? Or that the data in the file is from user input in some way and you want to access it?

I'm going to skip past that part and try to address what I think you are asking about. The line:

os.system('file.py')

tells the OS to run the script file.py. Because you are running it with os.system() your control is limited after you do that. You run the program and do not regain control until that program exits.

If you want to be able to run the command and then stop it when the user types a key, you need to run it in a different way. You would have to run it in a subprocess or a different thread so that you still have an active thread that is not blocked. It can monitor for the user input and then have it do something to shut it down. Exactly how you would shut it down would depend to some degree on the command you ran and how you started it.

Try looking here for some guidance on replacing the os.sytem() call.

Upvotes: 1

Related Questions