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'".
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
2022-04-11 14:58:37
admin
set
github: 72486
2016-09-28 12:54:02
zach.ware
set
stage: resolved
2016-09-28 12:50:52
paul.moore
set
status: open -> closedresolution: not a bugmessages: +