0

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

1
  • 1
    You did not open a file. You requested from the system to apply a default action to a file, which accidentally for *.py is opening on your system. Thus you cannot directly close the file. It's beyond the scope of control of your program. Commented May 17, 2020 at 19:50

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.