Discussion:
Synchronous shutil.copyfile()
(too old to reply)
Hugo Ferreira
2007-01-30 15:05:23 UTC
Permalink
Hi there,

I have a problem. I'm using calling shutil.copyfile() followed by
open(). The thing is that most of the times open() is called before
the actual file is copied. I don't have this problem when doing a
step-by-step debug, since I give enough time for the OS to copy the
file, but at run-time, it throws an exception.

Is there anyway to force a sync copy of the file (make python wait for
the completion)?

Thanks in advance!

Hugo Ferreira
Jean-Paul Calderone
2007-01-30 15:59:30 UTC
Permalink
Post by Hugo Ferreira
Hi there,
I have a problem. I'm using calling shutil.copyfile() followed by
open(). The thing is that most of the times open() is called before
the actual file is copied. I don't have this problem when doing a
step-by-step debug, since I give enough time for the OS to copy the
file, but at run-time, it throws an exception.
Is there anyway to force a sync copy of the file (make python wait for
the completion)?
shutil.copyfile() _is_ synchronous. Check out the source:

def copyfileobj(fsrc, fdst, length=16*1024):
"""copy data from file-like object fsrc to file-like object fdst"""
while 1:
buf = fsrc.read(length)
if not buf:
break
fdst.write(buf)

def copyfile(src, dst):
"""Copy data from src to dst"""
if _samefile(src, dst):
raise Error, "`%s` and `%s` are the same file" % (src, dst)

fsrc = None
fdst = None
try:
fsrc = open(src, 'rb')
fdst = open(dst, 'wb')
copyfileobj(fsrc, fdst)
finally:
if fdst:
fdst.close()
if fsrc:
fsrc.close()

The problem you are experiencing must have a different cause.

Jean-Paul
Matthew Woodcraft
2007-01-30 18:06:15 UTC
Permalink
Post by Hugo Ferreira
I have a problem. I'm using calling shutil.copyfile() followed by
open(). The thing is that most of the times open() is called before
the actual file is copied. I don't have this problem when doing a
step-by-step debug, since I give enough time for the OS to copy the
file, but at run-time, it throws an exception.
Is there anyway to force a sync copy of the file (make python wait for
the completion)?
shutil.copyfile() closes both files before it returns, so I suspect
this is an OS-level bug.

The most likely culprits are buggy network filesystems and buggy
on-access virus scanners.

-M-

Loading...