os.remove & os.rmdir

Peter Hansen peter at engcorp.com
Tue Jan 7 16:44:22 CET 2003

Padraig Brady wrote:
> >>    2. It removes the redundancy of 2 functions doing the same thing
> >>    3. Is saves you having to write code like:
> >>        If os.path.isdir(path):
> >>            os.rmdir(path)
> >>        else:
> >>            os.unlink(path)
> >
> >
> > Why would you write that code?  If you really wanted to remove
> > a directory and all directories under it, if there are any,
> > would you not write just "os.rmdir(path)" ?
> Nope that only works for empty directories. Note I'm
> only talking about empty directories anyway.

Sorry, I got quite confused... I was picturing os.rmdir() as
having the shutil.rmtree() functionality, and you suggesting
that os.remove() should be doing the same...  no idea where I got that.

Next time I'll try to wake up a little more before posting... :-)

> As I said above that is not what I'm proposing. I just want
> the simple change to allow remove() to work on empty directories.
> Actually I don't see any backwards compatability probs at all.

Hmmm... might there not be code which uses os.remove() with the
assumption it is not going to remove anything which is an empty
directory?  Maybe this is just the reinterpretation of the direction
I was going, after correcting my confusion.  I suspect any change
to the behaviour of these functions could lead to a situation
where existing code behaves very badly.


More information about the Python-list mailing list