Lo env�o en un attach para quien pueda serle �til.
Saludos,
Nico.
Nicol�s Ech�niz wrote:
Hola listeros
Necesito monitorear un directorio para disparar un proceso cada vez que se modifica un archivo.
El proceso que disparo es en Python, por lo que pensaba programar el "monitor" tambi�n en Python.
Qu� opciones se les ocurren para monitorear el directorio de manera eficiente?
Saludos,
Nicol�s Ech�niz
#! /usr/bin/env python # # Author: Nicolas Echaniz <[EMAIL PROTECTED]> # #
"""
Monitor modification times for a list of files.
Sample usage:
files_list = ["/path/foo", "/path/bar", "/path2/foo"]
fm = file_monitor(files_list)
while 1:
time.sleep(1)
modified_files = fm.modified()
if modified_files:
for file in modified_files:
# do stuff
"""
import os, sys
def get_mtimes(files_list):
"""get_mtimes(files_list) -> dictionary
files_list must be a list of filenames (full paths).
Returns a dictionary with filename as key
and last modification time as value for each file on the list"""
mtimes = {}
for file in files_list:
try:
file_mtime = os.stat(file).st_mtime
except:
print "File not found:", file
sys.exit()
mtimes[file] = file_mtime
return mtimes
class file_monitor:
"""Usefull to monitor a list of files for changes when used inside a loop.
The constructor __init__() expects a list of filenames (full paths).
file_monitor.modified() returns a list of files that where modified
since the last call to this funcion.
"""
def __init__(self, files_list):
self.files_list = files_list
self.mtimes = get_mtimes(files_list)
def modified(self):
new_mtimes = get_mtimes(self.files_list)
modified_files = [ file for file in self.files_list\
if self.mtimes[file] != new_mtimes[file] ]
if modified_files:
self.mtimes = new_mtimes
return modified_files
-- Para desuscribirte ten�s que visitar la p�gina https://listas.linux.org.ar/mailman/listinfo/lugar-gral/ Si ten�s alg�n inconveniente o consulta escrib� a mailto:[EMAIL PROTECTED] Apoya al ASLE enviando tu firma http://www.linux.org.ar/asle
