Python Forum

Full Version: If - a media player exists?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Guys do you have any idea any idea on how to write a condition to check whether a media player exists (I'm only curious for the most part)? When I say media player, it is an application that can be controlled with media shortcuts from the keyboard. Example: Groove & Spotify.

Or should I just check for every single application? Also, how do I check if an application is running at all?
psutil can get info on running processes in a cross platform way: http://psutil.readthedocs.io/en/latest/

Any program can have events bound to those keys, just because they're "media" keys doesn't mean only media programs will use them. Just like text editors aren't the only programs which might use the "t" key. And then there's configurable options... vlc, for example, lets you bind whatever key you want to whatever function you want, so the media "next" button could open/close your playlist instead of... skipping to the next track.

And then, of coarse, a new program might come out which uses those keys.

So the first thing you should ask yourself, is what it is you want to handle. What do you want to do about programs which don't exist yet that might use those keys? What about non-media programs using them?
As mention bye @nilmao so is psutil nice to use.
Here a example i have given before,also use schedule.

notepad or could be any other process running get check bye iterate over all process running process_iter(),
if notepad is not running start it,if already running do nothing.
This check is now done every 10-sec.
import schedule
import time, os
import psutil
 
def check_process():
    PROCNAME = "notepad.exe"
    for proc in psutil.process_iter():
        if proc.name() == 'notepad.exe':
            return True
    else:
        return False
 
def check_run():
    if check_process():
        print('Notepad is running')
    else:
        print('Notepad in not running')
        print('Start <notepad.exe>')
        os.startfile("notepad.exe")
 
schedule.every(.10).minutes.do(check_run)
while True:
    schedule.run_pending()
    time.sleep(1)