Issue 32539: os.listdir(...) on deep path on windows in python2.7 fails with errno 123 (original) (raw)

This should be fixed. That said, we have to use a unicode string for a long path anyway. Prior to Windows 8, the conversion from ANSI to Unicode in the system runtime library uses a static MAX_PATH buffer, so the ANSI API is inherently limited to MAX_PATH. You'll see this in the documentation of all functions that allow using \?\ extended paths:

In the ANSI version of this function, the name is limited to 
MAX_PATH characters. To extend this limit to 32,767 wide 
characters, call the Unicode version of the function and 
prepend "\\?\" to the path.

Decoding ANSI strings uses a dynamically-sized buffer in Windows 8, but the change is undocumented and should not be relied upon.

Unfortunately, we can't reliably use a raw unicode string literal in Python 2, since \u and \U escapes are still evaluated. We can instead use forward slashes and normalize via os.path.normpath.