Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python getcwd of a subst ( O:\ )folder

I am running a python script from a subst folder in windows:

c:\MyFolder\Tmp\Code\subst O: .
O:\script.py

script.py is running the getcwd (and abspath) command and it needs the real folder name, but instead it shows O:.

Is there a way to get the real folder name (without running the subst command and iterating the drives) and use an getcwd alternative?


1 Answers

As marking comments as accepted answer is impossible, I will quote @eryksun's comments:

The native path of the working directory will be something like "\Device\HarddiskVolume2\MyFolder\Tmp\Code". If you open a handle to this directory via the subst drive "O:" and call GetFinalPathNameByHandle, Windows can reconstruct either the Volume GUID path or the DOS path. It gets the native device and file path via NtQueryObject and NtQueryInformationFile. Then it opens the mount-point manager "\Device\MountPointManager" to query the mapping from the native device path to either the "Volume{GUID}" name or the DOS logical drive letter (or folder if there is no drive letter mount)

In Python 3, GetFinalPathNameByHandle is wrapped as os.path._getfinalpathname (only for getting the final DOS path). It's used by pathlib.Path.resolve

Much appreciated, thanks.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!