Send SIGINT in Windows using Python - python

I try this code in Linux:
import os
import signal
for i in range(10000):
print i
if i==6666:
os.kill(os.getpid(),signal.SIGINT)
it works well. But it doesn't work in Windows, because the attribute 'kill' is not present in os module for Windows
How can I send SIGINT to self program in Windows?

from win32api import GenerateConsoleCtrlEvent
GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0)

Related

Python code to close all open application in windows

I am working on pyautogui for locateonscreen but I need to make sure I have a clean windows prior to running it.
I need help with a code that will kill/close all open/active windows application and then open a single exe using subprocess.
you can use the psutil library to close all open/active windows applications and then open a single exe using the subprocess module:
import psutil
import subprocess
# Kill all open/active processes
for proc in psutil.process_iter():
proc.kill()
# Open a single exe using subprocess
subprocess.Popen("C:\\path\\to\\exe.exe")
Note that the psutil.process_iter() returns a list of all running processes on the system, and proc.kill() method is used to kill each process. The subprocess.Popen method is used to open the exe file. Make sure that you provide the correct path to the exe file. Please be aware that this script will close all open applications, including any unsaved work in progress.
You should use it with caution, or for testing purpose only.
you can instead reduce all open windows. this is safer!
import win32gui
import win32con
import subprocess
def minimize_all():
def callback(hwnd, hwnds):
if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd):
hwnds.append(hwnd)
return True
hwnds = []
win32gui.EnumWindows(callback, hwnds)
for h in hwnds:
win32gui.ShowWindow(h, win32con.SW_MINIMIZE)
minimize_all()
# Open a single exe using subprocess
subprocess.Popen("C:\\path\\to\\exe.exe")
This script uses the win32gui library to enumerate all open windows, and the win32con library to minimize them.
why not minimize all windows with shortcut WIN + D ?
import pyautogui
pyautogui.hotkey('winleft','d')

How do I handle Windows Terminal Closing event in Python?

I run a .py script with Windows Terminal using 'py' command. Is there any callback function that I can add to that .py script to handle the event of closing the Terminal Windows unexpectedly? (hit the 'X' button for example)
You can try like this:
import time
def on_exit(sig, func=None):
print("exit handler")
time.sleep(10) # so you can see the message before program exits
-Windows:
if you install pywin32 package, you can :
import win32api
win32api.SetConsoleCtrlHandler(on_exit, True)
If you are on Windows, opt for win32api.
-Un*x:
Or, using python internal "signal" library, if you are using under *nix system:
import signal
signal.signal(signal.SIGTERM, on_exit)

Run python script without terminal or any app open

I have a python app that sends a notification whenever a specified YouTube channel gains a subscriber. I would like to have this run in the background instead of in terminal. Is there a way to do this? Here are my libaries if they could cause an issue:
import os
import urllib.request
import json
import time
from sys import platform
from plyer import notification
import subprocess
You can run your Python app as a service.
If you are using Windows you need some libraries such as:
win32serviceutil
win32service
win32event
servicemanager
In case of your Operating System is Windows, you can use .pyw file extension and then run the app, it will run in the background, you could only stop it using the Task Manager.
On Linux, the easiest solution is to add & at the end of the command.
I. e: python3 file.py &.
But it will not be started every time you start up the computer, you can use tools such as systemd for example, to automatically run it every time.

Is there a way to check what you are running a python program in?

I want to create a script for just Terminal and IDLE, but I don't know how. Using if 'idlelib' in sys.modules: works for seeing if it is running in IDLE, but is there a way to use the same code to find if it is in Terminal by replacing 'idlelib'?
You can try using psutil and os
import psutil
import os
if psutil.Process(os.getpid()).parent().name() in ["cmd.exe","bash"]:
print("in cmd")
Using idle it returned 'pythonw.exe' which shows this works.

How can I import a Python script with several modules included into Sikuli

I'm trying to call a Python (2.7) script from Sikuli (r930) and use a variable from the script. Below is the Python code:
import sys
import re
import os
import time
from pywinauto import application
from SendKeys import SendKeys
from cStringIO import StringIO
app=application.Application()
app.connect_(path=r'C:\Program Files\myApp\myApp.exe')
backup = sys.stdout
sys.stdout = StringIO()
app.dlg.print_control_identifiers()
out = sys.stdout.getvalue()
sys.stdout.close() # close the stream
sys.stdout = backup # restore original stdout
regex = re.compile(r'(\d{8}\s*\-\s*\d{8})')
found = re.search(regex, out)
print found.group(0) #pass this variable to Sikuli
I'm capturing stdout because this is what Pywinauto's print_control_identifiers method returns (not a string). Also, I need a hashed serial number from the GUI app that I can't get with Sikuli, hence the need to use Pywinauto. However, when I attempt to call execfile() from Sikuli, I get the error:
ImportError: no module named Pywinauto.
I read the docs, and I know that Sikuli (Jython) can include Python modules and scripts. Besides, the external .py file that I'm calling runs successfully when ran independently. Can someone tell me if I'm missing a step?
Code I'm using to call the .py file shown above from Sikuli:
aScript = 'c:\\getHash_serial.py'
execfile(aScript)
The immediate problem you're having is that Jython can't find your module. Probably, you installed it under Python, and they don't share a module path. You can fix this by setting the JYTHONPATH environment variable.
However, Pywinauto links with native code, and this is something that Jython does not support.
You may be able to get around this limitation by calling regular Python from Jython via the subprocess module.

Categories

Resources