os.remove() behaviour on empty directories (was Re: [Python-Dev] If you thought there were too many PEPs...)

Peter Funk pf@artcom-gmbh.de
Wed, 30 Aug 2000 18:37:16 +0200 (MEST)


Hi,

effbot:
> > btw, Python's remove/unlink implementation is slightly
> > broken -- they both map to unlink, but that's not the
> > right way to do it:
> > 
> > from SUSv2:
> > 
> >     int remove(const char *path);
> > 
> >     If path does not name a directory, remove(path)
> >     is equivalent to unlink(path). 
> > 
> >     If path names a directory, remove(path) is equi-
> >     valent to rmdir(path). 
> > 
> > should I fix this?

BDFL:
> That's a new one -- didn't exist when I learned Unix.

Yes, this 'remove()' has been added relatively late to Unix.  It didn't
existed for example in SCO XENIX 386 (the first "real" OS available
for relatively inexpensive IBM-PC arch boxes long before the advent
of Linux).

Changing the behaviour of Pythons 'os.remove()' on Unices might break 
some existing code (although such code is not portable to WinXX anyway):

pf@artcom0:ttyp3 ~ 7> mkdir emptydir
pf@artcom0:ttyp3 ~ 8> python
Python 1.5.2 (#1, Jul 23 1999, 06:38:16)  [GCC egcs-2.91.66 19990314/Linux (egcs- on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import os
>>> try:
...     os.remove('emptydir')
... except OSError:
...     print 'emptydir is a directory'
... 
emptydir is a directory
>>> 

> I guess we can fix this in 2.1.

Please don't do this without a heavy duty warning in a section about
expected upgrade problems.  

This change might annoy people, who otherwise don't care about
portability and use Python on Unices only.  I imagine people using
something like this:

    def cleanup_junkfiles(targetdir)
        for n in os.listdir(targetdir):
            try:
                os.remove(n)
            except OSError:
                pass

Regards, Peter
-- 
Peter Funk, Oldenburger Str.86, D-27777 Ganderkesee, Germany, Fax:+49 4222950260
office: +49 421 20419-0 (ArtCom GmbH, Grazer Str.8, D-28359 Bremen)