Skip to content

os.rmdir fails with Errno 16 #5

Description

@dmik

The following test

import os
try:
 os.rmdir('1')
except:
 pass
os.mkdir('1')
os.chdir('1')
os.rmdir('../1')

fails like this (both in python2 and python3):

D:>python2.7 rmdir.py
Traceback (most recent call last):
  File "rmdir.py", line 8, in <module>
    os.rmdir('../1')
OSError: [Errno 16] Resource busy: '../1'

D:>python3.9 rmdir.py
Traceback (most recent call last):
  File "D:/Coding/Tests/python/rmdir.py", line 8, in <module>
    os.rmdir('../1')
OSError: [Errno 16] Resource busy: '../1'

Which is expected because on OS/2 you can't delete a directory which is the current one (on Linux and BSD this is allowed and the test succeeds). A solution is to just chdir("..") before calling the underlying Posix rmdir function in the implementation of os.rmdir. It will always work provided we are not deleting the root drive (which is impossible anyway).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions