Totte Karlsson
Totte Karlsson

Reputation: 1341

How to get Python's os.path.realpath returning the 'real' path on a Windows substed drive?

Python's os.path.realpath, when used with a substed drive on the Windows paltform, don't resolve the substed path to the real path.

For example creating a substed drive like this:

subst S: C:\Users\Public\Desktop

and checking real path in python like this:

import os
myPath = "S:\\"
print("Real path of: " + myPath + " is: " + os.path.realpath(myPath) )

prints

Real path of: S:\ is: S:\

In the docs for the subst command, a substed drive is referred to as a virtual drive. Virtual, suggesting something being "not real", indicates that the Python realpath command is not working as one would assume on Windows.

The code is used to setup a Docker container and the path is used in a mount. Docker will ask the user for permission to used the substed drive as a shared drive, which will fail, as docker can't use shared substed drives. This is why it's crucial getting the real, proper path.

How to get the proper path above, i.e. from S:\\ get C:\Users\Public\Desktop, in Python?

Update: After filing a Python Bugreport, word was given that there is a pull request for an updated, proper, version of realpath on Windows. In the meantime, one can use:

pathlib.Path('S:\\').resolve()

which resolves to the real path, i.e. c:\Users... etc!

Upvotes: 1

Views: 1670

Answers (1)

Totte Karlsson
Totte Karlsson

Reputation: 1341

Currently proper answer is:

pathlib.Path('S:\\').resolve()

Thanks to Eryk Sun, see https://bugs.python.org/issue36112

Upvotes: 1

Related Questions