Iam Riz
Iam Riz

Reputation: 127

PermissionError: How to read a file from a remote file server (accessible by IP) with authentication?

How can I read a file from a remote file server (accessible by IP) using Python3? How to set the user and password with this IP file server setup?

Example of file location (with authentication needed): \\10.123.123.132\example.jpg

The function I tried is as below which is throwing a PermissionError error. I am using django as a web framework. When the file server access is pre-authenticated via Windows authentication (manually) and django project is run locally on localhost, the code works fine. However, when the django project is served using winsw and waitress (https://github.com/winsw/winsw, https://docs.pylonsproject.org/projects/waitress/en/stable/runner.html), the file server access code fails to work with PermissionError logged.

def copy_file(fullname, filename):
    with open(filename, 'wb') as output:
        data_file = open(fullname, "rb")
        output.write(data_file.read())
        output.close()

Error:

Traceback (most recent call last):
  File "D:\app\services.py", line 21, in copy_file
    data_file = open(fullname, "rb")
PermissionError: [Errno 13] Permission denied: '\\\\10.123.123.132\\example.jpg'

Will appreciate any feedback and clue, thank you very much.

Upvotes: 0

Views: 268

Answers (1)

Tim Roberts
Tim Roberts

Reputation: 54726

By default, services run as a special system user that does not have permission for your file shares. Have you set the username for your service?

You can set the user name in the services control panel. Run services.msc to bring up the control panel, find your service, and edit its parameters.

By the way, you can use import shutil / shutil.copyfile(src,dst) to copy files by name.

Upvotes: 1

Related Questions