Cerno
Cerno

Reputation: 831

How to make `os.path.isdir()` work for mapped network drives?

When I boot my PC, os.path.isdir("R:\\") returns False, where R:\ is a mapped network drive. Once I access the mapped drive at least once from Windows Explorer, the command works and keeps working until I reboot.

I know that I can get around this using UNC paths but since my code is part of a UI, I can't force the user to not use mapped network drives.

How do I make Python reliably accept mapped network drives? I thought I might be able to simulate opening the folder from Windows by calling something like os.system("dir R:\\") but it did not work. Apparently there is some magic Windows is performing under the hood that I cannot replicate through a os.system call.

Here is my call history for reference to what I tried:

os.path.isdir("R:\\")  # False
os.system("dir R:\\")  # 1
os.path.isdir("R:\\")  # False
os.system("net use")  # 0
os.path.isdir("R:\\")  # False

# open R:\ in Windows Explorer

os.path.isdir("R:\\")  # True

This question deals with the same problem, but the accepted answer proposes the workaround to use UNC paths, but as explained, that does not fit my use case: os.path.isfile() returns false for file on network drive

Upvotes: 3

Views: 194

Answers (0)

Related Questions