0

I'm wondering how I would go about having a function refresh itself every minute, and check if a certain file it open. I don't exactly know how to go about this, but heres an example of what I'm looking for:

def timedcheck():
   if thisgame.exe is open:
      print("The Program is Open!")
   else:
      print("The Program is closed!")
      *waits 1 minute*
      timedcheck()

I would also like the script to refresh the function "def timedcheck():" every minute, so it keeps checking if thisgame.exe is open.

I searched through the site already, all suggestions recommended using "import win32ui", which gives me an error when I do.

tshepang
  • 12,111
  • 21
  • 91
  • 136

3 Answers3

3

To repeat this check every minute:

def timedcheck():
   while True:
       if is_open("thisgame.exe"):
          print("The Program is Open!")
       else:
          print("The Program is closed!")
       sleep(60)

Since it's a .exe file, I assume that by "check if this file is open" you mean "check if thisgame.exe" is running. psutil should be helpful - I haven't tested the below code, so it may need some tweaking, but shows the general principle.

def is_open(proc_name):
    import psutil
    for process in psutil.process_iter():
        if proc_name in process.name:
            return True
    return False
rkday
  • 1,106
  • 8
  • 12
  • ImportError: No module named psutil... Im getting real tired of these constant import errors, running Python27 btw. – Jordan ChillMcgee Ludgate Feb 03 '13 at 03:07
  • He linked the psutil page. You have to download it and add it as a Python module yourself; it's not a default module. – Anorov Feb 03 '13 at 06:31
  • 1
    there is no `name.contains()` method. `psutil.get_process_list()` is deprecated. You could [use `process_iter()` instead](http://stackoverflow.com/a/14674690/4279). – jfs Feb 03 '13 at 16:39
  • Thanks - can't think what language I was thinking of with `contains()`. – rkday Feb 03 '13 at 20:07
  • you could use `any()`: `is_open = lambda name: any(name in p.name for p in psutil.process_iter())` – jfs Feb 04 '13 at 07:59
0

You can use sleep from the time module with an input of 60 for 1 minute delay between checks. You can open the file temporarily and close it if not needed. An IOError will occur if the file is already opened. Catch the error with an exception and the program will wait for another minute before trying again.

import time
def timedcheck():
   try:
      f = open('thisgame.exe')
      f.close()
      print("The Program is Closed!")
   except IOError:
      print("The Program is Already Open!")
   time.sleep(60) #*program waits 1 minute*
   timedcheck()
Octipi
  • 835
  • 7
  • 12
  • That covers how to get the program to wait, thanks. But how would I get the script to check if a certain program is open. – Jordan ChillMcgee Ludgate Feb 03 '13 at 02:40
  • A nice addition to this would be using the winsound module to sound an alert over your speakers. There is a [stack overflow here](http://stackoverflow.com/a/6537563/1961486) if you are interested. – Octipi Feb 03 '13 at 03:01
0

Here's a variation on @rkd91's answer:

import time

thisgame_isrunning = make_is_running("thisgame.exe")

def check():
   if thisgame_isrunning():
      print("The Program is Open!")
   else:
      print("The Program is closed!")

while True:
    check() # ignore time it takes to run the check itself
    time.sleep(60) # may wake up sooner/later than in a minute

where make_is_running():

import psutil # 3rd party module that needs to be installed

def make_is_running(program):
    p = [None] # cache running process
    def is_running():
        if p[0] is None or not p[0].is_running():
            # find program in the process list
            p[0] = next((p for p in psutil.process_iter()
                         if p.name == program), None)
        return p[0] is not None
    return is_running

To install psutil on Windows for Python 2.7, you could run psutil-0.6.1.win32-py2.7.exe.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670