Python Glossary Русский

all Python 3urllib.requestfunction

urllib.request.url2pathname()

paths and URLs

The inverse of pathname2url(): it decodes %XX in the path part of a file: URL and returns a path for this operating system.

Syntax

urllib.request.url2pathname(url, *, require_scheme=False, resolve_host=False)

By default it expects a URL without the scheme: it does not parse the whole string 'file:///tmp/x' and returns it unchanged. Since 3.14 require_scheme=True accepts a full URL, and an address with a foreign host ('//server/share') raises URLError on Linux and macOS instead of a silently glued path; on Windows it becomes a UNC path.

Examples

import urllib.request
print(urllib.request.url2pathname('docs/my%20file.txt'))  # → docs/my file.txt
print(urllib.request.url2pathname('file:///tmp/x'))  # → file:///tmp/x — схему без require_scheme не разбирает
# Python 3.14+
import urllib.request
print(urllib.request.url2pathname('file:///tmp/a%20b', require_scheme=True))  # → /tmp/a b

See also

Open in the full glossarydocs.python.orgReport an inaccuracy