This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: DirEntry.is_dir() evaluates True for a file on Windows
Type: behavior Stage: resolved
Components: Windows Versions: Python 3.5
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: David Staab, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2016-09-28 12:47 by David Staab, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit David Staab, 2016-09-28 12:47 Python module and directory structure for reproducing test
Messages (2)
msg277611 - (view) Author: David Staab (David Staab) Date: 2016-09-28 12:47
I'm using Python 3.5.2 on Windows 10 Pro to run the following code with the attached file structure.

The test code is:
from os import DirEntry, scandir

def test_is_dir():
    for item in os.scandir(TEST_DIR):
        if item.is_dir:

TEST_DIR = '.'

The console output is:
Connected to pydev debugger (build 162.1967.10)

Process finished with exit code 0

I would expect to only see '.\20160707', but "if item.is_dir:" always evaluates to True.

I notice that changing the import line to "from os import DirEntry, scandir" yields the exception "ImportError: cannot import name 'DirEntry'".
msg277613 - (view) Author: Paul Moore (paul.moore) * (Python committer) Date: 2016-09-28 12:50
is_dir is a *method*. To find out if an entry is a directory, you need to call it.

So you need

from os import DirEntry, scandir

def test_is_dir():
    for item in os.scandir(TEST_DIR):
        if item.is_dir():
            #         ^^ note change

TEST_DIR = '.'
Date User Action Args
2022-04-11 14:58:37adminsetgithub: 72486
2016-09-28 12:54:02zach.waresetstage: resolved
2016-09-28 12:50:52paul.mooresetstatus: open -> closed
resolution: not a bug
messages: + msg277613
2016-09-28 12:47:27David Staabcreate