On this page
  1. ntpath
  2. Import
  3. Overview
  4. Quick Start
  5. Implementation Walkthrough
  6. 1. Handling Drive Letters and UNC Shares
  7. Gotchas & Boundaries
  8. Common Functions
  9. Constants & Attributes

ntpath

Prev: nt | Up: stdlib_module | Next: nturl2path
Rel: pathlib, os, io, time, fileinput
Ref: docs.python.org — ntpath
Tags: #public #python3 #stdlib


Import

import ntpath

Overview

Windows (backslash-separated C:\dir\file and UNC \\server\share) path-manipulation implementation backing os.path on Windows. Import ntpath directly only when you need to parse or normalize Windows paths on macOS/Linux (for example, processing Windows manifests or build logs); otherwise use pathlib (Path / PureWindowsPath) or os.path.

Quick Start

import ntpath

# Parse Windows paths on any operating system
drive, tail = ntpath.splitdrive(r"C:\Users\Ada\notes.txt")
assert drive == "C:" and tail == r"\Users\Ada\notes.txt"
assert ntpath.basename(r"C:\Users\Ada\notes.txt") == "notes.txt"

Implementation Walkthrough

1. Handling Drive Letters and UNC Shares

ntpath.splitdrive() cleanly separates both drive letters (C:) and UNC network roots (\\host\share):

import ntpath

root, rest = ntpath.splitdrive(r"\\fileserver\shared\report.pdf")
assert root == r"\\fileserver\shared"
assert rest == r"\report.pdf"

Gotchas & Boundaries

Common Functions

Constants & Attributes

Linked from

Related notes