[PYTHONMAC-SIG] os.rmdir()
Guido van Rossum
guido@CNRI.Reston.Va.US
Mon, 14 Jul 1997 22:55:14 -0400
> I'm trying to delete a directory in a Python program (in preparation for
> unstuffing a new copy of it...)
>
> I find that os.rmdir() fails with an unknown error 16.
>
> I tried writing this little function to empty out a folder before deleting
> it. It half-worked; one large portion of the tree was deleted, including
> many subdirectories, but still rmdir() refused to delete one directory
> which the Finder had no trouble trashing (so the directory was not corrupt).
Could it be that there is a hidden or locked file in there, or a file
that's in use? Which operation fails? The unlink or the rmdir?
A few nits on your code:
> def myrmdir(dirpath):
> #print 'deleting children of',dirpath
> if dirpath[-1] == os.sep: dirpath = dirpath[:-1]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Sure you need
this?
> items = os.listdir(dirpath)
> for f in items:
> if f == '.' or f == '..': continue
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unnecessary;
listdir no longer returns . or .. (and these are
unixisms anyway -- on the mac they would be : and ::
and would never be returned by listdir)
> path = dirpath + os.sep + f
Correct is:
path = os.path.join(dirpath, f)
> if os.path.isdir(path):
> myrmdir(path)
> else:
> #print 'deleting',path
> os.unlink(path)
> print 'deleting',dirpath
> os.rmdir(dirpath)
And finally...
> Any ideas? Shall I dig into the source?
Of what? It's almost certainly an OS issue, not a bug in rmdir.
--Guido van Rossum (home page: http://www.python.org/~guido/)
_______________
PYTHONMAC-SIG - SIG on Python for the Apple Macintosh
send messages to: pythonmac-sig@python.org
administrivia to: pythonmac-sig-request@python.org
_______________