how to get os.py to use an ./ntpath.py instead of Lib/ntpath.py

ruck john.ruckstuhl at gmail.com
Mon Sep 10 13:25:29 EDT 2012


In Python 2.7.2 on Windows 7,

os.walk() uses isdir(),
which comes from os.path,
which really comes from ntpath.py,
which really comes from genericpath.py

I want os.walk() to use a modified isdir() on my Windows 7.
Not knowing any better, it seems to me like ntpath.py would be a good place to intercept.

When os.py does "import ntpath as path",
how can I get python to process my customized ntpath.py 
instead of Lib/ntpath.py ?

Thanks for any comments.
John

BTW, here's my mod to ntpath.py:
    $ diff ntpath.py.standard ntpath.py
    14c14,19
    < from genericpath import *
    ---
    > from genericpath import *
    >
    > def isdir(s):
    >     return genericpath.isdir('\\\\?\\' + abspath(s + '\\'))
    > def isfile(s):
    >     return genericpath.isfile('\\\\?\\' + abspath(s + '\\'))

Why?  Because the genericpath implementation relies on os.stat() which
uses Windows API function that presumes or enforces some naming
conventions like "doesn't end with a space or a period".
But the NTFS actually supports such filenames and dirnames, and some sw
(like cygwin) lets users make files & dirs without restricting.
So, cygwin users like me may have file 'voo...\\doo' which os.walk()
cannot ordinarily walk.  That is, the isdir('voo...') returns false
because the underlying os.stat is assessing 'voo' instead of 'voo...' .
The workaround is to pass os.stat a fullpathname that is prefixed
with r'\\?\' so the Windows API recognizes that you do NOT want the
name filtered.

Better said by Microsoft:
"For file I/O, the "\\?\" prefix to a path string tells 
the Windows APIs to disable all string parsing and to 
send the string that follows it straight to the file 
system. For example, if the file system supports large 
paths and file names, you can exceed the MAX_PATH limits 
that are otherwise enforced by the Windows APIs." 



More information about the Python-list mailing list