提示用户登录 python 中的受保护目录

prompt user to login to protected directory in python

我有一个脚本可以搜索受保护的目录并将文件资源管理器打开到选定的位置。问题是它只有在用户已经登录目录后才有效。

下面的代码正在做什么的示例:

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')

如果用户自己打开“\\directory\path”并在 windows 安全提示中输入他们的 username/password,那么我的代码有效。如果没有,我找不到目录路径。

有没有办法从Python打开到windows security prompt?理想情况下,用户输入用户名和密码,然后可以继续访问该目录。

如果您 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 exampleexcept 调用中传递 username/password。

归结为:

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