Method for Python to monitor file updates

  • 2021-06-28 09:29:25
  • OfStack

The main logic is to determine if the last modification time and creation time of the file are 1 at the second level, and this code applies to Python 2.


import time
import os

#Read fime name
FileName='D:/scapegoat/xx.csv'

#print file creation time
print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(os.stat(FileName).st_ctime))

#print file modified time
print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(os.stat(FileName).st_mtime)

Because the time taken by os.stat is the time stamp of Linux (seconds from 1970/1 to today), it is not convenient for us to read the time, so the converted time format will be printed.

Because the Linux timestamp is too precise, we only keep it at the second level.


if int(os.stat(FileName).st_ctime)==int(os.stat(FileName).st_mtime):
  print 'File has not been modified.'

Related articles: