[Tutor] Checking a file size before moving it

Magnus Lycka magnus@thinkware.se
Fri Jan 31 08:29:01 2003


At 21:15 2003-01-31 +1000, Alfred Milgrom wrote:
>Another suggestion might be to copy the files rather than moving them, and 
>at the end of the operation check whether the size of the files is the 
>same. If yes, you can remove the file from Directory A.

I think both this and the sleep version might cause lost
data if the computer system is slow.

Why don't you just try to open the file? At least in Win2000
you get an IOError if you try to open a file that it being
written. I assume that something like opening in append more
would yield an error in any OS, if open in read while writing
is permitted.

something like:

for fn in glob.glob('*'):
     try:
         f = file(fn)
         f.close()
         however_you_do_the_copying
     except IOError:
         print fn, 'is busy?'

See also
http://www.python.org/doc/current/lib/os-fd-ops.html


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se