os.walk() not processing subdirectories when using

2019-09-02 06:12发布

I'm having trouble with os.walk() in python 2.7.8 on Windows.

When I supply it with a 'normal' path such as "D:\Test\master" it works as expected. However when I supply it with a UNC path such as "\\?\D:\Test\master" it will report the root directory as expected but it will not drill down into the sub directories, nor will it raise an exception.

My research: I read on the help page that os.walk() accepts a function argument to handle errors. By default this argument is None so no error is reported.

I passed a simple function to print the error and received the following for every directory.

def WalkError(Error):
    raise Exception(Error)

Stack trace:

Traceback (most recent call last):
  File "Compare.py", line 988, in StartServer
    for root, dirs, files in os.walk(ROOT_DIR,True,WalkError):
  File "C:\Program Files (x86)\Python2.7.8\lib\os.py", line 296, in walk
    for x in walk(new_path, topdown, onerror, followlinks):
  File "C:\Program Files (x86)\Python2.7.8\lib\os.py", line 281, in walk
    onerror(err)
  File "Compare.py", line 62, in WalkError
    raise Exception(Error)
Exception: [Error 123] The filename, directory name, or volume label syntax is incorrect: '\\\\?\\D:\\Test\\master\\localization/*.*'

1条回答
Fickle 薄情
2楼-- · 2019-09-02 06:56

Answer from the original author (originally posted as an edit to the question):

Instant update: In the process of inspecting \lib\os.py, I discovered the error stems from os.listdir(). I searched for the above error message in relation to os.listdir() and found this solution which worked for me.

It looks like if you're going to use UNC style paths with os. modules they need to Unixised (have their \ converted to /). \\\\?\\D:\\Test\\master\\ becomes //?/D:/Test/master/ (note: you no longer need to escape the \ which is handy).

This runs counter to the UNC 'spec' so be aware if you're working with other modules which respect Microsoft's UNC implementation.

(Sorry for the self-solution, I was going to close the tab but felt there was knowledge here which couldn't be found elsewhere.)

查看更多
登录 后发表回答