From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 8387 invoked by alias); 27 Nov 2015 15:10:16 -0000 Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner@cygwin.com Mail-Followup-To: cygwin@cygwin.com Received: (qmail 8373 invoked by uid 89); 27 Nov 2015 15:10:16 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.2 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-lf0-f54.google.com Received: from mail-lf0-f54.google.com (HELO mail-lf0-f54.google.com) (209.85.215.54) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Fri, 27 Nov 2015 15:10:15 +0000 Received: by lfdl133 with SMTP id l133so131946793lfd.2 for ; Fri, 27 Nov 2015 07:10:11 -0800 (PST) X-Received: by 10.25.158.134 with SMTP id h128mr3803020lfe.53.1448637011740; Fri, 27 Nov 2015 07:10:11 -0800 (PST) MIME-Version: 1.0 Received: by 10.114.172.243 with HTTP; Fri, 27 Nov 2015 07:09:52 -0800 (PST) In-Reply-To: <20151127145105.GG14466@dinwoodie.org> References: <20151127145105.GG14466@dinwoodie.org> From: Michael Wild Date: Fri, 27 Nov 2015 15:20:00 -0000 Message-ID: Subject: Re: Incorrect Python errors when using os.remove to delete a directory To: The Cygwin Mailing List Content-Type: text/plain; charset=UTF-8 X-IsSubscribed: yes X-SW-Source: 2015-11/txt/msg00456.txt.bz2 On Fri, Nov 27, 2015 at 3:51 PM, Adam Dinwoodie 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 "", line 1, in > 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 "", line 1, in > 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 "", line 1, in > 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 "", line 1, in > 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