is file open?

M

Maxim Khesin

I have a script that processes a file generated by another program. I
need an unumbiguous way to know that the file has been written and
closed by the other program, at least on Windoze. I am thinking of doing

def CanOpen(fname):
try:
f = open(fname, 'a')
f.close()
return True
except IOError:
return False

should this work OK?

thanks,
max
 
F

Fredrik Lundh

Maxim said:
I have a script that processes a file generated by another program. I
need an unumbiguous way to know that the file has been written and
closed by the other program, at least on Windoze. I am thinking of doing

def CanOpen(fname):
try:
f = open(fname, 'a')
f.close()
return True
except IOError:
return False

should this work OK?

why not try it out:

many Windows programs lock the file, but it's no requirement.

As usual, the only unambigous way to make sure that another program are
done writing to the file is to have the other program use a temporary name
and rename when done. A reasonable workaround is to check the mtime,
and only read the file if the mtime is old enough.

if time.time() - os.path.getmtime(filename) >= LIMIT:
...

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,478
Latest member
ReginaldVi

Latest Threads

Top