blob: 17f01f5aa5f0fc13847a9b92db350ae42499d0ea [file] [log] [blame]
Guido van Rossum746ea351996-06-26 19:47:56 +00001#
2# nturl2path convert a NT pathname to a file URL and
3# vice versa
4
5def url2pathname(url):
6 """ Convert a URL to a DOS path...
7 Currently only works for absolute paths
8
9 ///C|/foo/bar/spam.foo
10
11 becomes
12
13 C:\foo\bar\spam.foo
14 """
15 import string
16 comp = string.splitfields(url, '|')
17 if len(comp) != 2 or comp[0][-1] not in string.letters:
18 error = 'Bad URL: ' + url
19 raise IOError, error
20 drive = string.upper(comp[0][-1])
21 components = string.splitfields(comp[1], '/')
22 path = drive + ':'
23 for comp in components:
24 if comp:
25 path = path + '\\' + comp
26 return path
27
28def pathname2url(p):
29
30 """ Convert a DOS path name to a file url...
31 Currently only works for absolute paths
32
33 C:\foo\bar\spam.foo
34
35 becomes
36
37 ///C|/foo/bar/spam.foo
38 """
39
40 import string
41 comp = string.splitfields(p, ':')
42 if len(comp) != 2 or len(comp[0]) > 1:
43 error = 'Bad path: ' + p
44 raise IOError, error
45
46 drive = string.upper(comp[0])
47 components = string.splitfields(comp[1], '\\')
48 path = '///' + drive + '|'
49 for comp in components:
50 if comp:
51 path = path + '/' + comp
52 return path