Python Forum

Full Version: How can I Open and close .py file from python scripts
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Here is my code:
    if mybuffer == "b'Python'":
        print("Python File Opening")
        thislist = [r'"py_exe.py"', r'"MQTT_pub.py"'] #add path here
        for x in thislist:
            print(x)
            Popen("python " + x)
    if mybuffer == "b'Close_Py'":
        thislist = [r'"python.exe"']  # add path here
        for x in thislist:
            print(x)
            os.system("TASKKILL /F /IM " + x)
            print("Python was closed")
this code is can run but.My problem is when I close thie python file it's will close all of python file that running in process
include the main python that run this code. My question is how can I close the specific python file?

Huh
what exactly are you trying to do?
you run a python script like:
python script.py
if you want to run other scripts from inside of one script, you use import
I want to close the specific python file for example:
In my process was running python file A.py, B.py, C.py

I want to know how can I close A.py, B.py, C.py in sequent. ?

Thank you for your reply Smile
Sorry your question is not clear. Are you running a.py, b.py, c.py as different threads? Please explain it better
I working on windows and when I open python file in every file name for example I name python file test1.py , test2.py and test3.py when I run them in the same time. And I saw them in the process It's just show only python.exe like this
[Image: iOy0nN]

And In my work is coding the python scrip that receive the mqtt command to close the python file test1.py , test2.py and test3.py . and I face the problem that when I running my python scrip and receive the mqtt msg to close the python file
my python scrip is also close because when I running my scrip in the process show python.exe too. Here is my code:
    if mybuffer == "b'Close_Py'":
        print("Program is Closing")
        os.system("TASKKILL /F /IM python.exe")
        print("Python was closed")
. I just prefer to close only test1.py , test2.py and test3.py not include my python scrip
how to do that?
If you are task killing Python process itself, it is expected to close all the Python thread. How you are opening all the python files? Using subprocess?
I'm using Popen
if mybuffer == "b'Python'":
        print("Python File Opening")
        Popen('python MQTT_Close_Program.py')
But to close Popen process, why you using taskkill option?. You can use Popen.kill()
so if I use popen.kill() It can close the specific python file name right?
I mean It will close only test1.py,test2.py and test3.py Right?
(Dec-13-2019, 01:11 AM)SayHiii Wrote: [ -> ]so if I use popen.kill() It can close the specific python file name right?
I mean It will close only test1.py,test2.py and test3.py Right?

I don't understand close meaning from your view. Using Popen, you would've created a new thread to execute test1.py/test2.py/test3.py or whatever and that thread will get stopped using Popen.kill() method