os.remove & os.rmdir

Padraig Brady padraig.brady at corvil.com
Tue Jan 7 09:36:16 EST 2003


Peter Hansen wrote:
> Padraig Brady wrote:
> 
>>So, should I do a simple patch?
> 
> Please don't!
> 
>>Advantages I can see are:
>>
>>    1. It follows the standard
> 
> Which standard?  This is Python, not C.

POSIX and ISO C. Having different functionality than those
is confusing I think.

>>    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.

> And if you _didn't_
> want to remove subfolders if there were any, you would just
> use os.remove or os.unlink and know that there would be an
> exception or whatever if it was not an empty directory.

that's not the way it works currently. Currently os.{remove,unlink}
will throw an exception even for empty directories.

>>Disadvantages:
>>
>>    1. Backwards compatability (minor I think)
> 
> Major, I think.  At least insofar as there is probably lots of
> code that uses os.remove/os.unlink *knowing* that it is "safe"
> and will never remove subdirectories that a user might have added.

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.

Pádraig.





More information about the Python-list mailing list