summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1995-08-10 18:09:16 (GMT)
committerGuido van Rossum <guido@python.org>1995-08-10 18:09:16 (GMT)
commit0ec3126222c756b71f8847c6241b3e1f0a3fb3e0 (patch)
tree0eb5e673580fa73eab56e0fa0b64d9557c2e189e
parentf68ec3924e1a0abbda55f59303362eac2212def4 (diff)
downloadcpython-0ec3126222c756b71f8847c6241b3e1f0a3fb3e0.zip
cpython-0ec3126222c756b71f8847c6241b3e1f0a3fb3e0.tar.gz
cpython-0ec3126222c756b71f8847c6241b3e1f0a3fb3e0.tar.bz2
added normpath() and splitdrive()
-rw-r--r--Lib/macpath.py84
1 files changed, 45 insertions, 39 deletions
diff --git a/Lib/macpath.py b/Lib/macpath.py
index 32bf147..5317c36 100644
--- a/Lib/macpath.py
+++ b/Lib/macpath.py
@@ -46,48 +46,20 @@ def split(s):
return s[:colon-1], s[colon:]
-# Short interfaces to split()
+# Split a pathname into a drive specification and the rest of the
+# path. Useful on DOS/Windows/NT; on the Mac, the drive is always
+# empty (don't use the volume name -- it doesn't have the same
+# syntactic and semantic oddities as DOS drive letters, such as there
+# being a separate current directory per drive).
-def dirname(s): return split(s)[0]
-def basename(s): return split(s)[1]
+def splitdrive(p):
+ return '', p
-# XXX This is undocumented and may go away!
-# Normalize a pathname: get rid of '::' sequences by backing up,
-# e.g., 'foo:bar::bletch' becomes 'foo:bletch'.
-# Raise the exception norm_error below if backing up is impossible,
-# e.g., for '::foo'.
-
-norm_error = 'macpath.norm_error: path cannot be normalized'
+# Short interfaces to split()
-def norm(s):
- import string
- if ':' not in s:
- return ':' + s
- f = string.splitfields(s, ':')
- pre = []
- post = []
- if not f[0]:
- pre = f[:1]
- f = f[1:]
- if not f[len(f)-1]:
- post = f[-1:]
- f = f[:-1]
- res = []
- for seg in f:
- if seg:
- res.append(seg)
- else:
- if not res: raise norm_error, 'path starts with ::'
- del res[len(res)-1]
- if not (pre or res):
- raise norm_error, 'path starts with volume::'
- if pre: res = pre + res
- if post: res = res + post
- s = res[0]
- for seg in res[1:]:
- s = s + ':' + seg
- return s
+def dirname(s): return split(s)[0]
+def basename(s): return split(s)[1]
# Return true if the pathname refers to an existing directory.
@@ -127,11 +99,45 @@ def exists(s):
return 1
-# Normalize path, removing things like ...:A:..:... (yet to be written)
+# Normalize a pathname: get rid of '::' sequences by backing up,
+# e.g., 'foo:bar::bletch' becomes 'foo:bletch'.
+# Raise the exception norm_error below if backing up is impossible,
+# e.g., for '::foo'.
+# XXX The Unix version doesn't raise an exception but simply
+# returns an unnormalized path. Should do so here too.
+
+norm_error = 'macpath.norm_error: path cannot be normalized'
def normpath(s):
+ import string
+ if ':' not in s:
+ return ':' + s
+ f = string.splitfields(s, ':')
+ pre = []
+ post = []
+ if not f[0]:
+ pre = f[:1]
+ f = f[1:]
+ if not f[len(f)-1]:
+ post = f[-1:]
+ f = f[:-1]
+ res = []
+ for seg in f:
+ if seg:
+ res.append(seg)
+ else:
+ if not res: raise norm_error, 'path starts with ::'
+ del res[len(res)-1]
+ if not (pre or res):
+ raise norm_error, 'path starts with volume::'
+ if pre: res = pre + res
+ if post: res = res + post
+ s = res[0]
+ for seg in res[1:]:
+ s = s + ':' + seg
return s
+
# Directory tree walk.
# For each directory under top (including top itself),
# func(arg, dirname, filenames) is called, where