Alessandro Di Maggio
Alessandro Di Maggio

Reputation: 147

Python - Access protected network resource

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

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?

Upvotes: 2

Views: 13087

Answers (1)

phihag
phihag

Reputation: 287915

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.

Upvotes: 5

Related Questions