On this page
nturl2path
Prev: ntpath | Up: stdlib_module | Next: numbers
Rel: email, json, mailbox, mimetypes, base64
Ref: docs.python.org — nturl2path
Tags: #public #python3 #stdlib
Import
import nturl2path
Overview
Internal Windows helper module used by urllib.request to convert between local Windows paths (C:\path\file) and URL path components (///C:/path/file). Do not import nturl2path directly—use urllib.request.pathname2url(), urllib.request.url2pathname(), or pathlib.Path.as_uri() instead.
Quick Start
import nturl2path
url_part = nturl2path.pathname2url(r"C:\Users\Ada\report.txt")
assert url_part == "///C:/Users/Ada/report.txt"
assert nturl2path.url2pathname(url_part) == r"C:\Users\Ada\report.txt"
Implementation Walkthrough
1. Portable Alternative via urllib.request
For cross-platform code, call urllib.request.pathname2url() and urllib.request.url2pathname(), which automatically dispatch to nturl2path on Windows and standard percent-encoding on POSIX.
Gotchas & Boundaries
- Internal helper:
nturl2pathis an undocumented implementation detail of urllib; preferurllib.requestorPath.as_uri()/Path.from_uri()(Python 3.13+). - Windows path assumptions: Expects Windows drive-letter or UNC syntax and does not produce a full scheme prefix on its own.
Common Functions
- nturl2path.pathname2url() —
pathname2url(p)— OS-specific conversion from a file system path to a relative URL of the 'file' scheme; not recommended for general use. - nturl2path.url2pathname() —
url2pathname(url)— OS-specific conversion from a relative URL of the 'file' scheme to a file system path; not recommended for general use.
Linked from
Related notes
- tuts/python3/stdlib_module/ntpath Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/nturl2path/pathname2url Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/nturl2path/url2pathname Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/numbers Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/base64 Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/email Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/json Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/mailbox Linked here; shared tags: #stdlib