Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python can I suppy username and password to os.listdir?

Tags:

python

django

Python 3.4, Django 1.7, Windows apache 2.4.12

I am trying to list all files on a Windows shared drive (which is restricted to certain users), and later write couple of files to the shared drive.

I am using os.listdir to do this. It works well if I just run the web app on my machine, but once it's deployed on the actual server it will stop working.

The problem is the permission on the Windows shared drive. User has to login first so I do have their username and password.

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

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

Does any one know how to solve this? Or I need to map the drive (how do I map drive with credential?), list files, write files, then disconnect the mapped drive?

Thank you so much.

like image 491
wolf97084 Avatar asked Oct 13 '25 08:10

wolf97084


1 Answers

You should take a look on net use Windows command. It lets you to mount any network resources with given credentials.

Before accessing network resource, you can execute from Python net use command, like this:

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

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

There can be some issues if given resource was already mounted with different credentials. In such case you should unmount them first (net use \\computername\path\to\dir /delete)

like image 173
Igor Pomaranskiy Avatar answered Oct 14 '25 20:10

Igor Pomaranskiy