How to close program from python script - python

(Sorry for my English)
I am making simple program to manage other programs. I can open scripts using
os.startfile(self.path)
But I anyways can't close my program. I tried many ways, but still can't do it.
I even tried `
subprocess.call("TASKKILL /F /IM firefox.exe", shell=True)
` but it closes only exe files, i also need to close py files
For example, i have program named 'notion.exe' and i can open it using
os.startfile('notion.exe')
Now, i want to close it using script 'handler.py', but i didn't find os method that closes file.
Can anyone tell me which library may I use to close 'notion.exe'?

Related

python saving scripts problems

I am using Windows 7. I installed Python and everything is working fine except the fact that as I am saving python scripts on desktop or anywhere, none of them are opening.
.py files are closing in a blink!
.pyw files are not even responding.
Any help appreciated. I have already uninstalled and reinstalled the software suite but the problem still persists.
Here are the pictures of the icons.
What happens when you run a .py file is that a cmd.exe window is opened where Python runs your script. But this window will close as soon as your script finishes.
What you can do in this case:
Add raw_input() (python2) or input() to the end of your script to make it pause for input. That wat you can read any output before the window closes.
Open a cmd window and run your scripts from that.
With regard to .pyw files, they are themselves responsible for creating a window. So unless those scripts create a GUI you probably won't see anything.

Hanging script and cmd won't close

I am trying to run a python script via cmd prompt on my work PC (Windows 7, Python 2.7). The script requires filepaths from different drives on my PC. I am correctly pulling all necessary filepaths and I press Enter to run the script but the script just hangs. The only thing that shows is a blinking underscore. I try to click the X to close the prompt but nothing happens.
I am not able to Ctrl+C out of the program either. I open up Task Manager and I am not able to End Task (nothing happens) or End Process (cmd.exe doesn't even show up in this tab). I also tried Start-->Run-->taskkill /im cmd.exe but nothing happens. The rest of my team has no problem with Python 2.7. The only way to get out of the frozen cmd is to hold down the power button. I do not want to have to keep going through this process especially since this is during work. I'm hoping someone will be able to help me out:
Any idea what's wrong with the version of Python I am using?
How am I able to kill cmd.exe so that I can continue normal work functions without having to hold down the power button and waiting 5-10 minutes to reboot my PC?
The filepaths I was pulling files from were through ClearCase directories. It turned out that I simply had to reinstall ClearCase. There must have been some configuration issue that was causing the cmd to hang and thus forcing a hard reboot. It's good to point out that I am no longer experiencing this problem and that nothing was wrong with the python scripts.

How can I exit a program (not python script) from Python 3?

I am currently working on a program that will back up my Skype and iTunes data. It works fine with my iTunes data, however, when I try to backup files from Skype I get loads of errors. These are caused from the program still being open.
I thought it would be a fun little project to try and find out how to force quit a program (and then re-open).
I have successfully learned how to OPEN Skype (with a simple statement,
import os
os.startfile(path) #this opens programs. I need to close.
So I think to get my program to work, all I would need to do is close skype, run the backup process, then re-open it.
When I tried searching my question, I was only finding answers to close PYTHON scripts. I need to close something that is running in Windows.
I have found the answer.
import os
os.system("taskkill /im skype.exe") #/im means "ImageName"
taskkill is a command-line reference.
"Ends one or more tasks or processes. Processes can be killed by process ID or image name"
http://technet.microsoft.com/en-us/library/bb491009.aspx
Thank you #furas for leading me to the page with the answer. I'm surprised I missed it, I tried searching for a while.

Ending pythonw.exe process on manual exit

I can't seem to find the right google search to figure out something that should be easy.
I am using wxPython to create a GUI, and saving the Python files as .pyw so I don't have a console. Then I am importing another .py file into the main window when a user does an action. Doing so created the pythonw.exe instance that won't close. To be clear, the .pyw that opens if I only open the main console will clear from my processes if I don't open/insert the other .py file from the first file while running.
When I use the program everything is perfectly fine, but when I use the Windows "X" button to close I still get pythonw.exe in my processes, and the file I am printing errors to is locked due to something else using it(the pythonw.exe). What code do I need to use to make sure that python is exiting completely? It also seems to stay whenever I build in a file - exit as well, and it is only staying on the processes if I insert the other module(only done with user input)
Also this may be obvious but if I open the imported file manually it has the same behavior, as in the python.exe closes when I close out of the file.
If you want to exit the application you can do this like this:
import os
os._exit(0)
This works when the normal exit() or reaching the end of the main file does not work becaus of background threads running.
But so you are ending killing all running threads. You should look into the code of the imported file which causes the problem to see if it has threads.

Python script waiting for some program being launched and then starting another program. (Windows)

I would like to write a python script that will finally be converted to .exe (with pyinstaller lets say) and added to windows startup applications list. This program (once launched) should 'monitor' user and wait until user will start a specified program (say program1.exe) and then my python program should launch another specified program (program2.exe).
I know that there is something like subprocess to launch another program from python script but I was not able to make it work so far ;/ And as it comes to this part where I need to monitor if user does start specified program I have no idea haw to bite on this. I don't expect a complete solution (although it would be very nice to find such ;p) but any guides or clues would be very helpfull.
For the monitoring whether or not the user has launched the program, I would use psutil: https://pypi.python.org/pypi/psutil
and for launching another program from a python script, I would use subprocess.
To launch something with subprocess you can do something like this:
PATH_TO_MY_EXTERNAL_PROGRAM = r"C:\ProgramFiles\MyProgram\MyProgramLauncher.exe"
subprocess.call([PATH_TO_MY_EXTERNAL_PROGRAM])
If it is as simple as calling an exe though, you could just use:
PATH_TO_MY_EXTERNAL_PROGRAM = r"C:\ProgramFiles\MyProgram\MyProgramLauncher.exe"
os.system(PATH_TO_MY_EXTERNAL_PROGRAM)
Hope this helps.
-Alex

Categories