MCPcopy Index your code
hub / github.com/python/cpython / pathname2url

Function pathname2url

Lib/urllib/request.py:1700–1732  ·  view source on GitHub ↗

Convert the given local file system path to a file URL. The 'file:' scheme prefix is omitted unless *add_scheme* is set to true.

(pathname, *, add_scheme=False)

Source from the content-addressed store, hash-verified

1698
1699
1700def pathname2url(pathname, *, add_scheme=False):
1701 """Convert the given local file system path to a file URL.
1702
1703 The 'file:' scheme prefix is omitted unless *add_scheme*
1704 is set to true.
1705 """
1706 if os.name == 'nt':
1707 pathname = pathname.replace('\\', '/')
1708 encoding = sys.getfilesystemencoding()
1709 errors = sys.getfilesystemencodeerrors()
1710 scheme = 'file:' if add_scheme else ''
1711 drive, root, tail = os.path.splitroot(pathname)
1712 if drive:
1713 # First, clean up some special forms. We are going to sacrifice the
1714 # additional information anyway
1715 if drive[:4] == '//?/':
1716 drive = drive[4:]
1717 if drive[:4].upper() == 'UNC/':
1718 drive = '//' + drive[4:]
1719 if drive[1:] == ':':
1720 # DOS drive specified. Add three slashes to the start, producing
1721 # an authority section with a zero-length authority, and a path
1722 # section starting with a single slash.
1723 drive = '///' + drive
1724 drive = quote(drive, encoding=encoding, errors=errors, safe='/:')
1725 elif root:
1726 # Add explicitly empty authority to absolute path. If the path
1727 # starts with exactly one slash then this change is mostly
1728 # cosmetic, but if it begins with two or more slashes then this
1729 # avoids interpreting the path as a URL authority.
1730 root = '//' + root
1731 tail = quote(tail, encoding=encoding, errors=errors)
1732 return scheme + drive + root + tail
1733
1734
1735# Utility functions

Callers 4

as_uriMethod · 0.90
open_local_fileMethod · 0.70

Calls 3

quoteFunction · 0.90
replaceMethod · 0.45
upperMethod · 0.45

Used in the wild real call sites across dependent graphs

searching dependent graphs…