[issue46084] Python 3.9.6 scan_dir returns filenotfound on long paths, but os_walk does not

Eryk Sun report at bugs.python.org
Wed Dec 15 12:32:36 EST 2021


Eryk Sun <eryksun at gmail.com> added the comment:

It works as expected for me:

    >>> len(p)
    261
    >>> print(p)
    C:\Temp\Jim\Documents\jschw_uiowtv3_old\AppData\Local\Google\Chrome\User Data\Default\Extensions\nenlahapcbofgnanklpelkaejcehkggg\0.1.823.675_0\notifications\pages\Cashback\components\CashBackResolve\components\RewardsActivation\components\CashbackSectionSimple

os.walk() can list the files in directory `p` if the \\?\ prefix is prepended:

    >>> next(os.walk('\\\\?\\' + p))[-1]
    ['spam.txt']

Without the prefix, the internal os.scandir() call fails, but by default the error is ignored:

    >>> next(os.walk(p))[-1]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    StopIteration

We can print the exception to see that it's the expected ERROR_PATH_NOT_FOUND (3) error for a path that's too long:

    >>> next(os.walk(p, onerror=print))[-1]
    [WinError 3] The system cannot find the path specified: 'C:\\Temp\\Jim\\Documents\\jschw_uiowtv3_old\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Extensions\\nenlahapcbofgnanklpelkaejcehkggg\\0.1.823.675_0\\notifications\\pages\\Cashback\\components\\CashBackResolve\\components\\RewardsActivation\\components\\CashbackSectionSimple'
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    StopIteration

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46084>
_______________________________________


More information about the Python-bugs-list mailing list