Occasional OSError: [Errno 13] Permission denied on Windows

Alec Wysoker alecw at pobox.com
Thu Jan 12 13:56:48 EST 2006


Well, I found a solution to some of these problems.  Perhaps this new
solution, combined with the sleeping to allow
virus-checker,file-indexer,etc. will fix the problem.

I opened a file with shelve, and when adding a particular entry found
it to be corrupt.  On Windows, shelve uses bsddb.hashopen.  I figured I
would just delete the file, lose whatever data was in there, and start
over.  It appears that once a bsddb hash format file is opened and
found to be corrupt, you can't delete the file until the process exits!

I tried opening the shelf with flag='n' in order to wipe the contents
of the file, but under the covers that just calls os.unlink, which
fails with the same errno 13.

Ultimately, I found I could clear out the file like this:

        db = anydbm.open(strPath, 'c')
        if hasattr(db, 'db') and hasattr(db.db, 'truncate'):
            db.db.truncate()
            db.close()
            return True

Pretty ugly but it seems to work.

-Alec


Alec Wysoker wrote:
> I tried something not exactly like this, but in the same spirit.  I
> don't generally have a list of files I want to delete - just one.  I
> try to delete it and if I get errno 13 I sleep for a little while (0.2)
> and then try again.  If the same problem then I add 1 sec to the sleep
> time and try again.  After sleeping about 10 sec total I give up.
>
> Unfortunately, one of my testers still experiences the problem even
> with this fix.  I am surprised that a virus checker or file indexer
> would hold onto a file for so long.  Ugh.
>




More information about the Python-list mailing list