summaryrefslogtreecommitdiffstats
path: root/Lib/posixpath.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1992-11-05 10:43:02 (GMT)
committerGuido van Rossum <guido@python.org>1992-11-05 10:43:02 (GMT)
commitc629d34c4f1797b690a6c93ea3e2a5b82698b686 (patch)
tree812dcbc9fd664f9a5354e3301d7aa2375d85517a /Lib/posixpath.py
parent1115ab2a7469746859655cfa7f717c794a8a22ab (diff)
downloadcpython-c629d34c4f1797b690a6c93ea3e2a5b82698b686.zip
cpython-c629d34c4f1797b690a6c93ea3e2a5b82698b686.tar.gz
cpython-c629d34c4f1797b690a6c93ea3e2a5b82698b686.tar.bz2
* change default line numbers for 'list' in pdb.py
* changed eval() into getattr() in cmd.py * added dirname(), basename() and (dummy) normath() to macpath.py * renamed nntp.py to nntplib.py * Made string.index() compatible with strop.index() * Make string.atoi('') raise string.atoi_error rather than ValueError * Added dirname() and normpath() to posixpath.
Diffstat (limited to 'Lib/posixpath.py')
-rw-r--r--Lib/posixpath.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/Lib/posixpath.py b/Lib/posixpath.py
index d2bda10..57b0af6 100644
--- a/Lib/posixpath.py
+++ b/Lib/posixpath.py
@@ -75,6 +75,12 @@ def basename(p):
return split(p)[1]
+# Return the head (dirname) part of a path.
+
+def dirname(p):
+ return split(p)[0]
+
+
# Return the longest prefix of all list elements.
def commonprefix(m):
@@ -256,3 +262,31 @@ def expandvars(path):
if res[-1:] == '\n':
res = res[:-1]
return res
+
+
+# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
+# It should be understood that this may change the meaning of the path
+# if it contains symbolic links!
+
+def normpath(path):
+ import string
+ comps = string.splitfields(path, '/')
+ # If the path begins with '/', comps[0] is '', which we leave alone;
+ # we also leave leading multiple slashes alone for compatibility
+ # with certain networking naming schemes using //host/path
+ i = 0
+ while i < len(comps):
+ if comps[i] == '.':
+ del comps[i]
+ elif comps[i] == '..' and i > 0 and \
+ comps[i-1] not in ('', '..'):
+ del comps[i-1:i+1]
+ i = i-1
+ elif comps[i] == '' and i > 0 and comps[i-1] <> '':
+ del comps[i]
+ else:
+ i = i+1
+ # If the path is now empty, substitute '.'
+ if not comps:
+ comps.append('.')
+ return string.joinfields(comps, '/')