Incorrect Python errors when using os.remove to delete a directory
Michael Wild
themiwi@gmail.com
Fri Nov 27 15:20:00 GMT 2015
On Fri, Nov 27, 2015 at 3:51 PM, Adam Dinwoodie <adam@dinwoodie.org> wrote:
> If I use os.remove in Python to remove a directory, I expect it to fail
> with an OSError on Python2 or a IsADirectoryError on Python3. On
> Python2, I get OSError, but with the wrong error code, whereas on
> Python3 I get completely the wrong exception.
>
> Simple testcases:
>
> $ rm -rf testdir && mkdir testdir && python -c 'import os; os.remove("testdir")'
> Traceback (most recent call last):
> File "<string>", line 1, in <module>
> OSError: [Errno 1] Operation not permitted: 'testdir'
>
> $ rm -rf testdir && mkdir testdir && python3 -c 'import os; os.remove("testdir")'
> Traceback (most recent call last):
> File "<string>", line 1, in <module>
> PermissionError: [Errno 1] Operation not permitted: 'testdir'
>
> On my handy CentOS box, I see the following instead:
>
> $ rm -rf testdir && mkdir testdir && python -c 'import os; os.remove("testdir")'
> Traceback (most recent call last):
> File "<string>", line 1, in <module>
> OSError: [Errno 21] Is a directory: 'testdir'
>
> $ rm -rf testdir && mkdir testdir && python3 -c 'import os; os.remove("testdir")'
> Traceback (most recent call last):
> File "<string>", line 1, in <module>
> IsADirectoryError: [Errno 21] Is a directory: 'testdir'
>
> Now I've realised what's going wrong I can work around it, but it'd be
> nice if the correct error were raised in the first place.
This is a bug in Python itself, and not Cygwin specific. At least I
get the same behavior with my Anaconda installation.
Not really a fix for the bug itself, but usually I consider it good
practice to first call os.isdir().
Cheers
--
Problem reports: http://cygwin.com/problems.html
FAQ: http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
More information about the Cygwin
mailing list