我在Windows上显示来自网络驱动器的文件时遇到问题。
path = "\\\\nexus\\File Server\\Technical\\MyDrive\\Software\\Releases\\%s\\%s\\" %(release, module)
其中\\nexus\
是网络驱动器。
我的主要问题是用户输入了正确的变量,我无法显示所请求目录的内容(“模块”的内容)。
os.listdir(path)
上面一行的问题是它返回一个Windows错误[123],这是一个找不到目录错误。这是因为listdir()似乎是所有反斜杠的两倍
导致:
"\\\\\\\\nexus\\File Server\\\\Technical\\\\MyDrive\\\\Software\\\\Releases\\\\release\\\\module\\\\"
print(glob.glob(path))
我真的不确切知道它是如何工作的:P但它似乎只是显示提供的目录而不是结束目录的内容
\\nexus\File Server\Technical\MyDrive\Software\Releases\release\module\"
我见过一个os.walk
但是我不确定它是如何工作的,因为它如何定义什么是基本目录/目录以及路径的其余部分
额外注意事项:'module'的内容将始终为zip文件,目录通常最多包含5个zip文件。
答案 0 :(得分:15)
刚刚在我的XP PC,Python 2.7,SMB共享\\myshare
os.listdir('\\\\myshare') # Fails with "WindowsError: [Error 53] The network path was not found"
os.listdir('\\\\myshare/folder') # Succeeds
我认为一些混淆可能是由WindowsError显示路径的repr()
引起的,而不是实际的路径 -
>>> repr(path)
"'\\\\myshare'"
>>> str(path)
'\\myshare'
如果这是Python 3& unicode问题,我建议先尝试修复字符串:
path = "\\\\myshare\folder"
path = bytes(path, "utf-8").decode("unicode_escape")
print os.listdir(path)
(遗憾的是我无法测试这个,因为我没有安装Python 3,但请告诉我它是否有效,我会编辑我的答案)
答案 1 :(得分:2)
这个对我有用:
os.listdir('\\\\server\folder\subfolder\etc')
(在Win7 64b上使用Python 2.7 32b)
答案 2 :(得分:0)
此问题的解决方法如下:
os.listdir('\\networkshares\\folder1\\folder2\\folder3')
这意味着您必须使用双斜杠而不是单斜杠。