pythondirectorywindows-explorerwindows-security

prompt user to login to protected directory in python


I have a script that searches through a protected directory and opens file explorer to a selected location. The problem is that it only works after the user has already logged into the directory.

Example of what sort of thing the code is doing below:

subfolder = input("give a subfolder")

if os.path.isdir("\\\\directory\\path\\"):
    #run some stuff
    os.startfile("\\\\directory\\path\\" + subfolder)
else:
    print('error message- unable to connect to drive. Please log in')

If the user has opened "\\directory\path" on their own and entered their username/password into the windows security prompt, then my code works. If they haven't, I can't find the directory path.

Is there a way to open to the windows security prompt from Python? Ideally user puts in username and password and then can continue to the directory.


Solution

  • If you install win32, you can connect to network resources with a prompted username and password. It's not a simple solution (like an os call), but you can use the wnet_connect method from this example to pass a username/password in your except call.

    It boils down to:

    win32wnet.WNetAddConnection2(0, None, full_path, None, username, password)