OS-specific conversion from a file system path to a relative URL of the 'file' scheme; not recommended for general use.
(p)
| 43 | return urllib.parse.unquote(url.replace('/', '\\')) |
| 44 | |
| 45 | def pathname2url(p): |
| 46 | """OS-specific conversion from a file system path to a relative URL |
| 47 | of the 'file' scheme; not recommended for general use.""" |
| 48 | # e.g. |
| 49 | # C:\foo\bar\spam.foo |
| 50 | # becomes |
| 51 | # ///C:/foo/bar/spam.foo |
| 52 | import ntpath |
| 53 | import urllib.parse |
| 54 | # First, clean up some special forms. We are going to sacrifice |
| 55 | # the additional information anyway |
| 56 | p = p.replace('\\', '/') |
| 57 | if p[:4] == '//?/': |
| 58 | p = p[4:] |
| 59 | if p[:4].upper() == 'UNC/': |
| 60 | p = '//' + p[4:] |
| 61 | drive, root, tail = ntpath.splitroot(p) |
| 62 | if drive: |
| 63 | if drive[1:] == ':': |
| 64 | # DOS drive specified. Add three slashes to the start, producing |
| 65 | # an authority section with a zero-length authority, and a path |
| 66 | # section starting with a single slash. |
| 67 | drive = f'///{drive}' |
| 68 | drive = urllib.parse.quote(drive, safe='/:') |
| 69 | elif root: |
| 70 | # Add explicitly empty authority to path beginning with one slash. |
| 71 | root = f'//{root}' |
| 72 | |
| 73 | tail = urllib.parse.quote(tail) |
| 74 | return drive + root + tail |