Skip to content

os.scandir incorrectly reports symlink as file #144287

@Mabooka

Description

@Mabooka

Bug report

Bug description:

I started rewriting my code to replace os.stat for every file with a smarter / more efficient os.scandir.
Nice. Thank you.
However:

os.scandir() entries report symlinks as FILES, -- which is absolutely incorrect and breaks everything.

To reproduce:

vlad@MacBook:~/tmp$ rm *
vlad@MacBook:~/tmp$ touch foo
vlad@MacBook:~/tmp$ ln -s foo bar
vlad@MacBook:~/tmp$ ls -lart 
total 0
drwxr-xr-x  22 vlad  staff  704 Jan 27 22:04 ..
-rw-r--r--   1 vlad  staff    0 Jan 27 22:08 foo
lrwxr-xr-x   1 vlad  staff    3 Jan 27 22:08 bar -> foo
drwxr-xr-x   4 vlad  staff  128 Jan 27 22:08 .

# so we have one file 'foo' and one symlink 'bar' in the cwd.
# Now: 

vlad@MacBook:~/tmp$ python
Python 3.9.6 (default, May  7 2023, 23:32:44) 
[Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> files = []
>>> entries = os.scandir('.')
>>> for e in entries:
...     if e.is_file(): # is FILE? 
...             print("Found a file:", e.name)
...             files.append(e.name)
... 
Found a file: foo
Found a file: bar
>>> files
['foo', 'bar']

# However, UNIX thinks different:  only one file here:
>>> os.system("find . -type f")
./foo
0
>>> 

As one can see here, 'bar' is treated as a file.
The UNIX find cannot find it with the option '-type f'.
It is NOT a file.

Please fix.

CPython versions tested on:

3.9

Operating systems tested on:

macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions