FAQ
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

Search Discussions

  • Sick Monkey at Jan 30, 2007 at 3:18 pm
    First off, I am just learning Python, so if there is a more efficient way to
    do this, then I am all ears.... (NOTE: The code below is something that I
    was messing with to learn threads... So some functionality is not
    applicable for your needs..I just wanted to show you a demonstration)
    One way that you could get around this, is to use threads. You can lock
    your thread and when the lock has been released, you could open it and
    ensure your copy has succeeded. Just a thought....
    ~~~~~~~~~~~~~~~~~~~~~~~
    import thread
    def counter(myId, count):
    for i in range(count):
    stdoutmutex.acquire()
    #Copy Your File Here
    stdoutmutex.release()
    exitmutexes[myId].acquire()


    stdoutmutex = thread.allocate_lock()
    exitmutexes = []
    for i in range(2):
    exitmutexes.append(thread.allocate_lock())
    thread.start_new(counter, (i, 2))

    for mutex in exitmutexes:
    while not mutex.locked():
    #Open Your File Here

    print 'Exiting'


    On 1/30/07, Hugo Ferreira wrote:

    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
    --
    http://mail.python.org/mailman/listinfo/python-list
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://mail.python.org/pipermail/python-list/attachments/20070130/d532fd94/attachment.htm
  • Jean-Paul Calderone at Jan 30, 2007 at 3:59 pm

    On Tue, 30 Jan 2007 15:05:23 +0000, Hugo Ferreira wrote:
    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*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 at Jan 30, 2007 at 6:06 pm

    Hugo Ferreira wrote:
    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-
  • Hugo Ferreira at Jan 31, 2007 at 1:11 am
    Well.. Thx for the answers.

    The only way I had to make it work was to use a time.sleep(10) after the
    shutil.copyfile(). Since this is a night-run script, I can waste 10 seconds,
    but it still knocks me out "why" it happens...

    Cheers!

    Hugo Ferreira
    On 30 Jan 2007 18:06:15 +0000 (GMT), Matthew Woodcraft wrote:

    Hugo Ferreira wrote:
    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-

    --
    http://mail.python.org/mailman/listinfo/python-list
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://mail.python.org/pipermail/python-list/attachments/20070131/326f36b9/attachment.htm
  • Hugo Ferreira at Jan 31, 2007 at 2:47 pm
    I'm banging my head over this one... sometimes it just works... *argh*

    Here's a snip of my problem:

    import win32com.client
    import shutil
    import time

    engine = win32com.client.Dispatch("DAO.DBEngine.36")
    table = 'STUFFTODELETE'
    dstFilename = 'in.mdb'
    workingfile = 'work.mdb'

    shutil.copyfile(dstFilename, workingfile)

    print 'Sleeping 5 after copy...'
    time.sleep(5)

    dstDb = engine.OpenDatabase(workingfile)

    print 'Sleeping 5 after open...'
    time.sleep(5)

    dstDb.Execute('DELETE FROM ' + table)

    At at *exactly* this line, I get the error:

    C:\Python24\lib\site-packages\win32com\gen_py\00025E01-
    0000-0000-C000-000000000046x0x5x0.py 510
    com_error: (-2147352567, 'Exception occurred.', (0, 'DAO.Database', 'Not a
    valid bookmark.', 'jeterr40.chm', 5003159, -2146825129), None)

    Sometimes... Yes, the database is a %"#$"# MSAccess. This script is running
    under windows.

    Any ideas?
    On 1/31/07, Hugo Ferreira wrote:

    Well.. Thx for the answers.

    The only way I had to make it work was to use a time.sleep(10) after the
    shutil.copyfile(). Since this is a night-run script, I can waste 10
    seconds, but it still knocks me out "why" it happens...

    Cheers!

    Hugo Ferreira
    On 30 Jan 2007 18:06:15 +0000 (GMT), Matthew Woodcraft wrote:

    Hugo Ferreira wrote:
    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-

    --
    http://mail.python.org/mailman/listinfo/python-list
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://mail.python.org/pipermail/python-list/attachments/20070131/af7d1044/attachment-0001.html

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJan 30, '07 at 3:05p
activeJan 31, '07 at 2:47p
posts6
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase