开发者

Python - Access protected network resource

I need to open a file on my local network from a Python s开发者_开发百科cript.

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?


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

'\\\\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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜