11# Module 'ntpath' -- common operations on WinNT/Win95 pathnames
22"""Common pathname manipulations, WindowsNT/95 version.
3- Instead of importing this module
4- directly, import os and refer to this module as os.path.
3+
4+ Instead of importing this module directly, import os and refer to this
5+ module as os.path.
56"""
67
78import os
1617def normcase (s ):
1718 """Normalize case of pathname.
1819
19- Makes all characters lowercase and all slashes into backslashes.
20-
21- """
20+ Makes all characters lowercase and all slashes into backslashes."""
2221 return string .lower (string .replace (s , "/" , "\\ " ))
2322
2423
2524# Return wheter a path is absolute.
2625# Trivial in Posix, harder on the Mac or MS-DOS.
2726# For DOS it is absolute if it starts with a slash or backslash (current
28- # volume), or if a pathname after the volume letter and colon starts with
29- # a slash or backslash.
27+ # volume), or if a pathname after the volume letter and colon / UNC resource
28+ # starts with a slash or backslash.
3029
3130def isabs (s ):
3231 """Test whether a path is absolute"""
@@ -50,13 +49,30 @@ def join(a, *p):
5049
5150
5251# Split a path in a drive specification (a drive letter followed by a
53- # colon) and the path specification.
52+ # colon, or a UNC resource ) and the path specification.
5453# It is always true that drivespec + pathspec == p
5554def splitdrive (p ):
56- """Split a pathname into drive and path specifiers. Returns a 2-tuple
57- "(drive,path)"; either part may be empty"""
55+ """Split a pathname into drive and path specifiers.
56+
57+ Return a 2-tuple (drive, path); either part may be empty.
58+ This recognizes UNC paths (e.g. '\\ \\ host\\ mountpoint\\ dir\\ file')"""
5859 if p [1 :2 ] == ':' :
5960 return p [0 :2 ], p [2 :]
61+ firstTwo = p [0 :2 ]
62+ if firstTwo == '//' or firstTwo == '\\ \\ ' :
63+ # is a UNC path:
64+ # vvvvvvvvvvvvvvvvvvvv equivalent to drive letter
65+ # \\machine\mountpoint\directories...
66+ # directory ^^^^^^^^^^^^^^^
67+ normp = normcase (p )
68+ index = string .find (normp , '\\ ' , 2 )
69+ if index == - 1 :
70+ ##raise RuntimeError, 'illegal UNC path: "' + p + '"'
71+ return ("" , p )
72+ index = string .find (normp , '\\ ' , index + 1 )
73+ if index == - 1 :
74+ index = len (p )
75+ return p [:index ], p [index :]
6076 return '' , p
6177
6278
@@ -67,8 +83,10 @@ def splitdrive(p):
6783# The resulting head won't end in '/' unless it is the root.
6884
6985def split (p ):
70- """Split a pathname. Returns tuple "(head, tail)" where "tail" is
71- everything after the final slash. Either part may be empty"""
86+ """Split a pathname.
87+
88+ Return tuple (head, tail) where tail is everything after the final slash.
89+ Either part may be empty."""
7290 d , p = splitdrive (p )
7391 slashes = ''
7492 while p and p [- 1 :] in '/\\ ' :
@@ -96,8 +114,10 @@ def split(p):
96114# It is always true that root + ext == p.
97115
98116def splitext (p ):
99- """Split the extension from a pathname. Extension is everything from the
100- last dot to the end. Returns "(root, ext)", either part may be empty"""
117+ """Split the extension from a pathname.
118+
119+ Extension is everything from the last dot to the end.
120+ Return (root, ext), either part may be empty."""
101121 root , ext = '' , ''
102122 for c in p :
103123 if c in ['/' ,'\\ ' ]:
@@ -146,17 +166,17 @@ def commonprefix(m):
146166# Get size, mtime, atime of files.
147167
148168def getsize (filename ):
149- """Return the size of a file, reported by os.stat(). """
169+ """Return the size of a file, reported by os.stat()"""
150170 st = os .stat (filename )
151171 return st [stat .ST_SIZE ]
152172
153173def getmtime (filename ):
154- """Return the last modification time of a file, reported by os.stat(). """
174+ """Return the last modification time of a file, reported by os.stat()"""
155175 st = os .stat (filename )
156176 return st [stat .ST_MTIME ]
157177
158178def getatime (filename ):
159- """Return the last access time of a file, reported by os.stat(). """
179+ """Return the last access time of a file, reported by os.stat()"""
160180 st = os .stat (filename )
161181 return st [stat .ST_MTIME ]
162182
@@ -208,7 +228,7 @@ def isfile(path):
208228
209229
210230# Is a path a mount point?
211- # XXX This degenerates in: 'is this the root?' on DOS
231+ # XXX This degenerates in: 'is this the root?' on DOS/Windows
212232
213233def ismount (path ):
214234 """Test whether a path is a mount point (defined as root of drive)"""
@@ -225,10 +245,11 @@ def ismount(path):
225245# or to impose a different order of visiting.
226246
227247def walk (top , func , arg ):
228- """walk(top,func,args) calls func(arg, d, files) for each directory "d"
229- in the tree rooted at "top" (including "top" itself). "files" is a list
230- of all the files and subdirs in directory "d".
231- """
248+ """Directory tree walk whth callback function.
249+
250+ walk(top, func, args) calls func(arg, d, files) for each directory d
251+ in the tree rooted at top (including top itself); files is a list
252+ of all the files and subdirs in directory d."""
232253 try :
233254 names = os .listdir (top )
234255 except os .error :
@@ -252,8 +273,9 @@ def walk(top, func, arg):
252273# variable expansion.)
253274
254275def expanduser (path ):
255- """Expand ~ and ~user constructions. If user or $HOME is unknown,
256- do nothing"""
276+ """Expand ~ and ~user constructs.
277+
278+ If user or $HOME is unknown, do nothing."""
257279 if path [:1 ] <> '~' :
258280 return path
259281 i , n = 1 , len (path )
@@ -287,8 +309,9 @@ def expanduser(path):
287309varchars = string .letters + string .digits + '_-'
288310
289311def expandvars (path ):
290- """Expand shell variables of form $var and ${var}. Unknown variables
291- are left unchanged"""
312+ """Expand shell variables of form $var and ${var}.
313+
314+ Unknown variables are left unchanged."""
292315 if '$' not in path :
293316 return path
294317 res = ''
@@ -369,6 +392,7 @@ def normpath(path):
369392
370393# Return an absolute path.
371394def abspath (path ):
395+ """Return the absolute version of a path"""
372396 try :
373397 import win32api
374398 return win32api .GetFullPathName (path )
0 commit comments