classification
Title: DirEntry.is_dir() evaluates True for a file on Windows
Type: behavior Stage: resolved
Components: Windows Versions: Python 3.5
process
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 2016-09-28 12:54 by zach.ware. This issue is now closed.

Files
File name Uploaded Description Edit
testmodule.zip 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:
            print(item.path)
    return

TEST_DIR = '.'
test_is_dir()


The console output is:
===============
Connected to pydev debugger (build 162.1967.10)
.\20160707
.\scratchpad.py

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
            print(item.path)
    return

TEST_DIR = '.'
test_is_dir()
History
Date User Action Args
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