easier conversion pathToURL possible?
christof hoeke
csad7 at yahoo.com
Tue Jan 7 15:46:15 EST 2003
hi,
for a program i need filepaths like "\test\index.html" that might come as
input from the command line to be converted to urls like "/test/index.html".
(somehow something contrary to what os.path can do to a path..., not to make
it os specific but generalize it).
also absolute paths like "c:\test\index.html" should be converted to an url
like file:///c:/test/index.html.
i could not find a solution in the standard library, is there one?
the following is the function i wrote for doing this. is there an easier
solution to it and does it work for all platforms at all? i could only test
this on windows.
def pathToURL(path):
"""
converts a os specific filepath into a standard URL?
"""
#delete garbage like /./
path = os.path.normpath(path)
#use file: protocoll for complete paths with drivenames
drive, p = os.path.splitdrive(path)
#change possible \ to /
url = string.replace(p, "\\", "/")
if drive:
return "file:///%s%s" % (drive, url)
else:
return url
does this code work on unix and mac at all?
thanks a lot!
chris
More information about the Python-list
mailing list