Python Forum
How can I Open and close .py file from python scripts
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I Open and close .py file from python scripts
#1
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
Reply
#2
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
Reply
#3
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
Reply
#4
Sorry your question is not clear. Are you running a.py, b.py, c.py as different threads? Please explain it better
Reply
#5
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?
Reply
#6
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?
Reply
#7
I'm using Popen
if mybuffer == "b'Python'":
        print("Python File Opening")
        Popen('python MQTT_Close_Program.py')
Reply
#8
But to close Popen process, why you using taskkill option?. You can use Popen.kill()
Reply
#9
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?
Reply
#10
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 281 Jan-24-2024, 06:28 PM
Last Post: frohr
  file open "file not found error" shanoger 8 946 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Trying to us python.exe from our network to run scripts cubangt 3 827 Aug-17-2023, 07:53 PM
Last Post: deanhystad
  How can i combine these two functions so i only open the file once? cubangt 4 805 Aug-14-2023, 05:04 PM
Last Post: snippsat
  Save and Close Excel File avd88 0 2,844 Feb-20-2023, 07:19 PM
Last Post: avd88
  I cannot able open a file in python ? ted 5 3,051 Feb-11-2023, 02:38 AM
Last Post: ted
  testing an open file Skaperen 7 1,309 Dec-20-2022, 02:19 AM
Last Post: Skaperen
  pyPDF2 PDFMerger close pensding file japo85 2 2,341 Jul-28-2022, 09:49 AM
Last Post: japo85
  Close and Open QDialog from QDialog 2 ITSRL 4 1,197 Jul-12-2022, 12:35 AM
Last Post: ITSRL
  I get an FileNotFouerror while try to open(file,"rt"). My goal is to replace str decoded 1 1,362 May-06-2022, 01:44 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020