[Tutor] [HELP]how to test properties of a file

Alan Gauld alan.gauld at freenet.co.uk
Thu Apr 7 00:01:49 CEST 2005


> Here's where my question.
> The file is already opened by Excel. os.access indicates it's
> writable, but it's actully not.

It is writable, but it is locked by the other program.
There is a difference! :-)

Basically you have two issues, the first is whether the OS will
ever allow anyone or anything to write to the file - its permissions,
the second is whether you can safely do so at this point in time - its
status. If another program has the file open for writing then anything
you tried to write to the file is likely to be overwritten by the
other
program, so you'd lose it! thus the OS refuses to allow more
than one program at a time to open the file in write mode.

[ You can make a guess at whether this is the case by reading the
last access time from the os.stat() result and comparing it to the
current time, but it can only be a guess. However it might enable
a friendlier error message if the program is interactive. ]

So after discovering that the file can be written to, you still
need to use the try/except technique when opening the file to
see if its locked, if it is you can decide to wait and try
again - in case its just a temporary glitch - or to log an
error and exit or move to the next file.

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list