Here is code that demonstrates the problem when converting a file:// URL to an OS path.
import os
import pathlib
import sys
import urllib.parse
print ("sys.argv[0] = {0}".format(sys.argv[0]))
varFilename = sys.argv[0]
varFilename = os.path.abspath(varFilename)
print ("abs varFilename = {0}".format(varFilename))
varMainFolder = os.path.dirname(varFilename)
print ("varMainFolder = {0}".format(varMainFolder))
varTarget = os.path.join(varMainFolder,"test test.py")
print ("varTarget = {0}".format(varTarget))
varURL = pathlib.Path(varTarget).as_uri()
print ("varURL = {0}".format(varURL))
varPathRaw = urllib.parse.urlparse(varURL).path
print ("varPathRaw = {0}".format(varPathRaw))
varPathDecode = urllib.parse.unquote(varPathRaw)
print ("varPathDecode = {0}".format(varPathDecode))
varOSPath = os.path.normpath(varPathDecode)
print ("varOSPath = {0}".format(varOSPath))
In Linux, this code prints:
sys.argv[0] = test.py
abs varFilename = /home/ldbader/test.py
varMainFolder = /home/ldbader
varTarget = /home/ldbader/test test.py
varURL = file:///home/ldbader/test%20test.py
varPathRaw = /home/ldbader/test%20test.py
varPathDecode = /home/ldbader/test test.py
varOSPath = /home/ldbader/test test.py
Notice the varOSPath is a perfectly valid absolute path. But in Windows, the code prints:
sys.argv[0] = test.py
abs varFilename = C:\mli\Junk\test.py
varMainFolder = C:\mli\Junk
varTarget = C:\mli\Junk\test test.py
varURL = file:///C:/mli/Junk/test%20test.py
varPathRaw = /C:/mli/Junk/test%20test.py
varPathDecode = /C:/mli/Junk/test test.py
varOSPath = \C:\mli\Junk\test test.py
Notice that varOSPath has an absolute path preceded by an invalid backslash. Attempting to open a file with this path will fail. I would have expected os.path.normpath() to discard a slash to the left of a drive specification, but it doesn't.
What should I do so the same logic gives me a valid absolute path on both platforms?