Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os, time, sys
- # Segundos.
- update_interval = 1
- # Tiempo de modificacion antiguo.
- _after_timestamp = 0
- # Tiempo de modificacion nuevo.
- _before_timestamp = 0
- # Variable que indica volver a abrir el archivo para leearlo.
- _open_file_and_read = True
- # Argumento: El archivo a leer.
- the_file = sys.argv[1]
- file_opened = None
- # Funcion simple que permite verificar el tiempo de modificacion de un archivo
- check_mod_timestamp = lambda file_: int(os.stat(file_).st_mtime)
- # verificar el tiempo de modificacion del archivo
- _before_timestamp = check_mod_timestamp(the_file)
- # Primero leemos el archivo, despues esperamos 1 segundo, posteriormente verificamos que
- # el archivo no se ha modificado, si lo hizo entonces imprimimos la siguiente linea:
- # === {file} has been modified :: {time} ===
- # === file.txt has been modified :: Mon Feb 17 22:22:46 2020 ===
- #
- # Despues de hacer eso se muestra el texto del "nuevo" archivo.
- def start():
- # las variables declaradas fuera de una funcion son globales (aunque no se modifican globalmente dentro de una funcion)
- # pero al declarar una variable con "_" ( _variable ), es como si declararamos una variable "privada" en python.
- # https://stackoverflow.com/questions/1641219/does-python-have-private-variables-in-classes
- global _open_file_and_read, _before_timestamp, _after_timestamp
- global file_opened
- while True:
- if(_open_file_and_read):
- _open_file_and_read = False
- file_opened = open(the_file, 'r')
- for line in file_opened.readlines():
- print(line.rstrip() )
- time.sleep(update_interval)
- _after_timestamp = check_mod_timestamp(the_file)
- if( not (_after_timestamp == _before_timestamp) ):
- _open_file_and_read = True
- _before_timestamp = _after_timestamp
- print(" === {file} has been modified :: {time} ===".format(file=the_file, time=time.ctime(_after_timestamp) ) )
- if(__name__ == '__main__'):
- try:
- start()
- except(KeyboardInterrupt):
- file_opened.close()
- print('')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement