Athena Wisdom
Athena Wisdom

Reputation: 6871

How to Watch a File for Reads?

Using Python, how can we monitor a specific file for reads done by another process?

Specifically asking for Ubuntu, but cross-platform solutions will be ideal.

Upvotes: 0

Views: 47

Answers (1)

Digoya
Digoya

Reputation: 131

The simplest one is to compare file access date in a loop. You can use the link for examples

# get the the stat_result object
fileStatsObj = os.stat ( filePath )
# Get last access time
accessTime = time.ctime ( fileStatsObj [ stat.ST_ATIME ] )

EDIT: I want to point out this method may be bad for precise access count because of potential race conditions. The most reliable way is to force process to increment access count in a separate file with readonly mode.

Upvotes: 1

Related Questions