3

Time for another newbie question, I fear. I'm attempting to use Python 3.2.2 (the version is important, in this case) to monitor a particular Windows path for changes. The simplest method, and the method I'm using, is:

original_state = os.listdir(path_string)

while os.listdir(path_string) == original_state:
    time.sleep(1)

change_time = datetime.datetime.now()

I'm writing this code to do some timing tests of another application. With that goal in mind, the Python script needs to (a) not adversely affect system performance, and (b) be relatively precise -- a margin of error of +/- 1 second is the absolute maximum I can justify. Unfortunately, this method doesn't meet the first criterion: When running this particular bit of code, the virtual environment is hammered, drastically slowing down the operations whose performance I'm trying to accurately measure.

I've read how to watch a File System for change, How do I watch a file for changes?, and http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html (an article recommended as a solution to that second SO question.) Unfortunately, Tim Golden's code appears to be Python 2.x code -- as near as I can tell, the pywin32 module isn't supported in Python 3.

What can I do in Python 3 to monitor this particular path without running into the same performance problems?

Community
  • 1
  • 1
Kudzu
  • 3,308
  • 1
  • 17
  • 16
  • If you're using Linux, you should look into `inotify`, but I'm not really sure when it comes to Windows. – Naftuli Kay Dec 01 '11 at 21:36
  • You didn't look hard enough. Check out the top link [here](http://sourceforge.net/projects/pywin32/files/pywin32/Build216/). – John Y Dec 01 '11 at 22:28

3 Answers3

2

According to the ActivePython 3.2 Documentation, their pywin32 now supports Python 3.x

Neil
  • 54,642
  • 8
  • 60
  • 72
  • I've been using Python 3.2.2 as downloaded from python.org. Is the ActivePython 3.2 module something I could just drop into my vanilla Python library? Or would I need to make a complete switch to ActivePython? I'd prefer not to do the latter -- ideally this code will evolve into a tool for my whole QA team. – Kudzu Dec 01 '11 at 21:57
  • @Kudzu: If you use Windows platform. You would do well to use the ActivePython distribution. It works very well. – pyfunc Dec 01 '11 at 22:13
  • @Kudzu: ActivePython is meant to be an all-inclusive distribution, not really used in conjunction with an existing installation of Python. That said, you don't need ActivePython at all. In principle, anything that is in ActivePython is available separately. There is nothing "special" about ActivePython; it's just a convenient bundle. – John Y Dec 01 '11 at 22:26
  • Now that I've got ActivePython installed, I've been able to adapt Tim Golden's code to suit my needs; and instead of using 80% of my VM's CPU, python's barely moving the needle during this test. Thanks! – Kudzu Dec 05 '11 at 19:05
1

On Linux there is iNotify and pyNotify. Similar asynchronous notification mechanism on windows is FindFirstChangeNotification function which is a part of FileSystemWatcher Class

Please look at solutions on the Tim Golden's page:

pyfunc
  • 65,343
  • 15
  • 148
  • 136
  • That Tim Golden article is actually the one I meant when I said "that article linked in the second one". Unfortunately I wasn't particularly clear about it. I've updated the question to specifically include that. – Kudzu Dec 01 '11 at 21:47
0

It is also possible to monitor a file or directory using GFileMonitor with Gio taking care of the underlying operating system details. Although, granted you likely won't be using Gtk if this is a Windows program. For posterity:

from gi.repository import Gio

gfile = Gio.file_new_for_path('/home/user/Downloads')
gfilemonitor = gfile.monitor(Gio.FileMonitorFlags.NONE, None)
gfilemonitor.connect('changed', callback_func)
andy.holmes
  • 3,383
  • 17
  • 28