os.listdir等在共享Windows路径上失败(Python 2.5) [英] os.listdir etc fails on shared windows path (Python 2.5)
问题描述
在解析共享路径(服务器上的共享路径,例如\ storage \ Builds)时,我看到一些奇怪的行为
I am seeing some weird behavior while parsing shared paths (shared paths on server, e.g. \storage\Builds)
我正在读取文本文件,其中包含我要进一步处理的目录路径.为此,请按以下步骤操作:
I am reading text file which contains directory paths which I want to process further. In order to do so I do as below:
def toWin(path):
return path.replace("\\", "\\\\")
for line in open(fileName):
l = toWin(line).strip()
if os.path.isdir(l):
print l # os.listdir(l) etc..
这适用于本地目录,但不适用于共享系统上指定的路径.
This works for local directories but fails for paths specified on shared system.
e.g.
E:\Test -- works
\\StorageMachine\Test -- fails [internally converts to \\\\StorageMachine\\Test]
\\StorageMachine\Test\ -- fails [internally converts to \\\\StorageMachine\\Test\\]
但是,如果我打开python shell,导入脚本并使用相同的路径字符串调用函数,那么它将起作用!
But if I open python shell, import script and invoke function with same path string then it works!
在两种情况下,解析Windows共享路径的行为似乎有所不同.
It seems that parsing windows shared paths is behaving differently in both cases.
有什么想法/建议吗?
推荐答案
必须将输入转换为正斜杠(unix样式),以便os.*模块正确解析.
Have to convert input to forward slash (unix-style) for os.* modules to parse correctly.
更改代码如下
def toUnix(path):
return path.replace("\\", "/")
现在所有模块都能正确解析.
Now all modules parse correctly.
这篇关于os.listdir等在共享Windows路径上失败(Python 2.5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!