os.remove & os.rmdir

Peter Hansen peter at engcorp.com
Tue Jan 7 15:07:06 CET 2003


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.

>     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)" ?  And if you _didn't_
want to remove subfolders if there were any, you would just
us os.remove or os.unlink and know that there would be an
exception or whatever if it was not an empty directory.

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

I would not make this change.

-Peter




More information about the Python-list mailing list