windows Python - 访问受保护的网络资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7529439/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:08:30  来源:igfitidea点击:

Python - Access protected network resource

pythonwindowsfilenetworking

提问by Alessandro Di Maggio

I need to open a file on my local network from a Python script.

我需要从 Python 脚本打开本地网络上的文件。

In the basic case is very simple:

在基本情况下非常简单:

fh = open('\servername\path\resource.txt', 'r')
...

The problem is that the access to this network resource is protected. I tried something like:

问题是对该网络资源的访问受到保护。我试过类似的东西:

fh = open('\servername\path\resource.txt@username:pass', 'r')

but it doesn't work.

但它不起作用。

Any idea?

任何的想法?

回答by phihag

First of all, backslashes in Python need to be escaped, so your path string is

首先,Python中的反斜杠需要转义,所以你的路径字符串是

'\\servername\path\resource.txt'
# or ..
r'\servername\path\resource.txt'

Python's open function has no support for passwords. You'll need to use windows functions to specify passwords. Here's an example program doing exactly that.

Python 的 open 函数不支持密码。您需要使用 Windows 函数来指定密码。这是一个示例程序,正是这样做的