Python 从网络驱动器上的目录中检索内容(Windows)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14354113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieving contents from a directory on a network drive (windows)
提问by Verric
I'm have an issue about displaying the files from a network drive on Windows.
我有一个关于在 Windows 上显示网络驱动器中的文件的问题。
path = "\\nexus\File Server\Technical\MyDrive\Software\Releases\%s\%s\" %(release, module)
where \\nexus\is a network drive.
\\nexus\网络驱动器在哪里。
My main issue is given that a user enters correct variables, i'm unable to show the contents of the directory requested (the contents of 'module').
我的主要问题是用户输入了正确的变量,我无法显示所请求目录的内容(“模块”的内容)。
Things I've tried
我尝试过的事情
os.listdir(path)
The issue with the line above is that it returns a windows error [123] which is, a can not find directory error. This is because listdir() seems to double all the back slashes resulting in :"\\\\nexus\File Server\\Technical\\MyDrive\\Software\\Releases\\release\\module\\"print(glob.glob(path))
I didn't really know exactly how it works :P but it seems just to display the directory supplied and not the contents of the ending directory\nexus\File Server\Technical\MyDrive\Software\Releases\release\module\"
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\"
I've seen an os.walkhowever im not sure how its works, in that how does it defines what is the base directory /directories and what is the rest of the path
我见过一个os.walk但是我不确定它是如何工作的,因为它如何定义基本目录 /directories 以及路径的其余部分是什么
Extra notes: The contents of 'module' will always be a zip file, also the directory will generally contain at maximum five zip files.
额外说明:“模块”的内容将始终是一个 zip 文件,该目录通常最多包含五个 zip 文件。
采纳答案by Alex L
Just tested on my XP PC, Python 2.7, SMB share \\myshare
刚刚在我的 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
I think some of the confusion could be caused by WindowsError showing the repr()of the path, rather than the actual path -
我认为一些混乱可能是由 WindowsError 显示repr()路径而不是实际路径引起的 -
>>> repr(path)
"'\\myshare'"
>>> str(path)
'\myshare'
If this is a Python 3 & unicode problem, I suggest trying to fix the string first:
如果这是 Python 3 和 unicode 问题,我建议先尝试修复字符串:
path = "\\myshare\folder"
path = bytes(path, "utf-8").decode("unicode_escape")
print os.listdir(path)
(unfortunately I can't test this since I don't have Python 3 installed, but please let me know if it works and I'll edit my answer)
(不幸的是我无法测试这个,因为我没有安装 Python 3,但请告诉我它是否有效,我会编辑我的答案)
回答by Viktor Malyi
The workaround for this issue is following:
此问题的解决方法如下:
os.listdir('\networkshares\folder1\folder2\folder3')
It means that you have to use double slashes instead of single one.
这意味着您必须使用双斜线而不是单斜线。
回答by matekatona
This one worked for me:
这个对我有用:
os.listdir('\\server\folder\subfolder\etc')
(with Python 2.7 32b on Win7 64b)
(在 Win7 64b 上使用 Python 2.7 32b)

