1

Do files opened like file("foo.txt") have any info about file modification time?

Basically I want to know if the file has been modified or replaced since a certain time, but if the file is replaced between checking modification time and opening the file, then you have inaccurate information.

How can I be sure?

Thanks.

UPDATE

@rubayeet: Thanks for the answer (+1), I actually didn't think of that. But... What to do if the modification time has changed? Perhaps I reload the file again. But what if it changes that time? If the file is being touched regularly I could end up in a loop forever! What I really want is a way to just get an open file handle and a modification time to go with it, without a potential infinite loop.

PS The answer you gave was actually plenty good enough for my purposes as the file won't be changed regularly, its general interest on my part now.

UPDATE 2

Thinking the previous update through (and experimenting a little) I realize that simply knowing the file modification time at the point the file was opened is not so much use as if the file is modified while reading you can have some or all of the modified data in the stuff you read in, so you'd have to open and read/process the whole file, then check mtime again (as per @rubayeet's answer) to see if you may have stale data.

2 Answers 2

2

For simple modtimes you would use:

from os.path import getmtime

modtime = getmtime('/file/to/path')

If you want something like a callback functionality you could check the inotify bindings for python: pyinotify.

You essentialy set a watchmanager up, which notifies you in a event-loop if any changes happens in the monitored directory. You register for specific events, like opening a file (which changes the modtime if written to).

If you are interested in an exclusive access to a file, i would point to the fnctl module, which has some lowlevel and file-locking mechanism on filedescriptors.

Sign up to request clarification or add additional context in comments.

10 Comments

I know how to get the file modification time. That was not the question. And inotify is not guaranteed to tell you about changes under heavy load.
Could you clarify what you mean with inotify having difficulties under heavy load? Or point to a source for this statement? There is NO way of being sure, that there is no file-alteration between two file-attribute checks, except an exclusive lock/access to a file! You should look into fnctl.flock for this functionality.
I can't remember where I read this, I'll try and hunt it down, but the logic went as follows: inotify events are asynchronous and have a queue. Items get popped from the queue when an application is notified about them. If the system is very busy the queue can fill up so the kernel will not write any more messages to the queue.
Found a refernece: man inotify and search for IN_Q_OVERFLOW
As for there being no way to be sure, that is fine for my purpose. I'm happy for my program to be fooled by a user/process nobbling file modification times, but I'd rather it wasn't fooled by a heavy load. +1 for bringing up fnctl.flock, though it is way overkill for what I'm doing at the moment (as stated in Update 2 its mostly academic interest at this point).
|
1
import os
filepath = '/path/to/file'
modifytime1 = os.path.getmtime(filepath)
fp = open(filepath)
modifytime2 = os.path.getmtime(filepath)
if modifytime1 != modifytime2:
    print "File modified after opening"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.