On this page
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
- Prefer
pathlib.PureWindowsPathfor new code: It provides the same cross-platform Windows path parsing with cleaner composition. - Filesystem calls use the host OS:
ntpath.exists()orntpath.abspath()interact with the local OS, not a simulated Windows filesystem.
Common Functions
- ntpath.abspath() —
abspath(path)— Return the absolute version of a path. - ntpath.basename() —
basename(p)— Returns the final component of a pathname - ntpath.commonpath() —
commonpath(paths)— Given an iterable of path names, returns the longest common sub-path. - ntpath.commonprefix() —
commonprefix(m)— Given a list of pathnames, returns the longest common leading component - ntpath.dirname() —
dirname(p)— Returns the directory component of a pathname - ntpath.exists() —
exists(path)— Test whether a path exists. Returns False for broken symbolic links - ntpath.expanduser() —
expanduser(path)— Expand ~ and ~user constructs. - ntpath.expandvars() —
expandvars(path)— Expand shell variables of the forms $var, ${var} and %var%. - ntpath.getatime() —
getatime(filename)— Return the last access time of a file, reported by os.stat(). - ntpath.getctime() —
getctime(filename)— Return the metadata change time of a file, reported by os.stat(). - ntpath.getmtime() —
getmtime(filename)— Return the last modification time of a file, reported by os.stat(). - ntpath.getsize() —
getsize(filename)— Return the size of a file, reported by os.stat(). - ntpath.isabs() —
isabs(s)— Test whether a path is absolute - ntpath.isdevdrive() —
isdevdrive(path)— Determines whether the specified path is on a Windows Dev Drive. - ntpath.isdir() —
isdir(s)— Return true if the pathname refers to an existing directory. - ntpath.isfile() —
isfile(path)— Test whether a path is a regular file - ntpath.isjunction() —
isjunction(path)— Test whether a path is a junction Junctions are not supported on the current platform - ntpath.islink() —
islink(path)— Test whether a path is a symbolic link - ntpath.ismount() —
ismount(path)— Test whether a path is a mount point (a drive root, the root of a share, or a mounted volume) - ntpath.isreserved() —
isreserved(path)— Return true if the pathname is reserved by the system. - ntpath.join() —
join(path, *paths)—ntpath.join()function. - ntpath.lexists() —
lexists(path)— Test whether a path exists. Returns True for broken symbolic links - ntpath.normcase() —
normcase(s)— Normalize case of pathname. - ntpath.normpath() —
normpath(path)— Normalize path, eliminating double slashes, etc. - ntpath.realpath() —
realpath(path, *, strict=False)—ntpath.realpath()function. - ntpath.relpath() —
relpath(path, start=None)— Return a relative version of a path - ntpath.samefile() —
samefile(f1, f2)— Test whether two pathnames reference the same actual file or directory This is determined by the device number and i-node number and raises an exception if an os.stat() call on either pathname fails. - ntpath.sameopenfile() —
sameopenfile(fp1, fp2)— Test whether two open file objects reference the same file - ntpath.samestat() —
samestat(s1, s2)— Test whether two stat buffers reference the same file - ntpath.split() —
split(p)— Split a pathname. - ntpath.splitdrive() —
splitdrive(p)— Split a pathname into drive/UNC sharepoint and relative path specifiers. - ntpath.splitext() —
splitext(p)— Split the extension from a pathname. - ntpath.splitroot() —
splitroot(p)— Split a pathname into drive, root and tail.
Constants & Attributes
ntpath.ALLOW_MISSING(ALLOW_MISSING) =os.path.ALLOW_MISSINGntpath.altsep(str) ='/'ntpath.curdir(str) ='.'ntpath.defpath(str) ='.;C:\\bin'ntpath.devnull(str) ='nul'ntpath.extsep(str) ='.'ntpath.pardir(str) ='..'ntpath.pathsep(str) =';'ntpath.sep(str) ='\\'ntpath.supports_unicode_filenames(bool) =True
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/genericpath
- tuts/python3/stdlib_module/nt
- tuts/python3/stdlib_module/ntpath/abspath
- tuts/python3/stdlib_module/ntpath/basename
- tuts/python3/stdlib_module/ntpath/commonpath
- tuts/python3/stdlib_module/ntpath/commonprefix
- tuts/python3/stdlib_module/ntpath/dirname
- tuts/python3/stdlib_module/ntpath/exists
- tuts/python3/stdlib_module/ntpath/expanduser
- tuts/python3/stdlib_module/ntpath/expandvars
- tuts/python3/stdlib_module/ntpath/getatime
- tuts/python3/stdlib_module/ntpath/getctime
- tuts/python3/stdlib_module/ntpath/getmtime
- tuts/python3/stdlib_module/ntpath/getsize
- tuts/python3/stdlib_module/ntpath/isabs
- tuts/python3/stdlib_module/ntpath/isdevdrive
- tuts/python3/stdlib_module/ntpath/isdir
- tuts/python3/stdlib_module/ntpath/isfile
- tuts/python3/stdlib_module/ntpath/isjunction
- tuts/python3/stdlib_module/ntpath/islink
- tuts/python3/stdlib_module/ntpath/ismount
- tuts/python3/stdlib_module/ntpath/isreserved
- tuts/python3/stdlib_module/ntpath/join
- tuts/python3/stdlib_module/ntpath/lexists
- tuts/python3/stdlib_module/ntpath/normcase
- tuts/python3/stdlib_module/ntpath/normpath
- tuts/python3/stdlib_module/ntpath/realpath
- tuts/python3/stdlib_module/ntpath/relpath
- tuts/python3/stdlib_module/ntpath/samefile
- tuts/python3/stdlib_module/ntpath/sameopenfile
- tuts/python3/stdlib_module/ntpath/samestat
- tuts/python3/stdlib_module/ntpath/split
- tuts/python3/stdlib_module/ntpath/splitdrive
- tuts/python3/stdlib_module/ntpath/splitext
- tuts/python3/stdlib_module/ntpath/splitroot
- tuts/python3/stdlib_module/nturl2path
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/nt Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ntpath/abspath Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ntpath/basename Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ntpath/commonpath Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ntpath/commonprefix Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ntpath/dirname Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ntpath/exists Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ntpath/expanduser Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ntpath/expandvars Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ntpath/getatime Linked here; links to this page; shared tags: #stdlib