Python can i change the username and password for os.listdir?

Python 3.4, Django 1.7, Windows apache 2.4.12

I try to list all the files on a shared Windows drive (which is limited to specific users), and then write a couple of files to a shared drive.

I am using os.listdir for this. It works well if I just run the web application on my machine, but after it is deployed to the server itself, it will stop working.

The problem is the resolution on the Windows shared drive. The user must first log in so that I have a username and password.

My question is how to specify os.listdir username and password?

I tried os.listdir ('// windows / share / drive / dir @ domanin / username: password'), but the system will try to find the file instead of passing the username and password.

Does anyone know how to solve this? Or do I need to map a drive (how do I match a drive with credentials?), View files, burn files, and then disconnect the mapped drive?

Thank you very much.

+1
source share
2 answers

You should take a look at the net use Windows command. It allows you to mount any network resources with the specified credentials.

Before accessing a network resource, you can run the Python net use command, for example:

 net use \\computername\path\to\dir /user:username password 

If the credentials are correct, the network resource will be available, and os.listdir() will work.

There may be some problems if this resource is already installed with different credentials. In this case, you must unmount them first ( net use \\computername\path\to\dir /delete )

+1
source

According to the Python Guide, os.listdir(path) takes the name path as an argument. It probably works for you locally using the Django test server, since permissions in this environment are different than using a production web server such as Apache or Nginx. Try looking at the following pre-existing related SO questions and answers - they may offer you some recommendations, especially in the Win window:

What is the best way to map Windows drives with Python?

python copy files to network location in windows without mapping drive

0
source

All Articles