[Python-ideas] Have os.unlink remove directories on Unix (was: junction points on Windows)

Kim Gräsman kim.grasman at gmail.com
Mon Aug 26 13:42:12 CEST 2013


Hi random,

On Mon, Aug 26, 2013 at 6:19 AM,  <random832 at fastmail.us> wrote:
> On Sun, Aug 25, 2013, at 14:26, Kim Gräsman wrote:
>> Ping?
>>
>> Can I clarify something to move this forward? It seems like a good
>> idea to me, but I don't have the history of Py_DeleteFileW -- maybe
>> somebody tried this already?
>
> What happens if you call os.rmdir? And just out of curiosity, what
> happens if you call msvcrt's _wremove and _wrmdir functions?

os.rmdir just delegates to RemoveDirectoryW and so successfully
removes junction points too.

This seems slightly against the spirit of POSIX:

"
 The rmdir() function shall remove a directory whose name is given by
path. The directory shall be removed only if it is an empty directory.
 [...]
 If path names a symbolic link, then rmdir() shall fail and set errno
to [ENOTDIR].
"
- http://pubs.opengroup.org/onlinepubs/009695399/functions/rmdir.html

The junction point is removed irrespective of whether the target is
empty or not.

Junction points are sort of symbolic links, but are removed without error.

I can't speak for _wremove and _wrmdir, but I assume they're POSIX
compat shims, so they probably follow remove and rmdir, possibly with
less understanding of links in general.

I wouldn't switch to using os.rmdir, however -- if the path names an
actual directory rather than a symlink or a junction point, os.rmdir
will delete it, whereas os.unlink will fail with access denied (as I
believe it should.)

> While we're on the subject of os.remove, can someone explain to me why
> it doesn't work on directories in Unix? [...]

I have nothing to offer here, sorry. It seems a little dangerous to
muck about with the details of os file management, there must be
millions of lines of code relying on the current behavior in one way
or another.

Thanks,
- Kim


More information about the Python-ideas mailing list