From 384d249006b786b7d1eed21795a83a5ab703cb96 Mon Sep 17 00:00:00 2001 From: Barry Warsaw Date: Tue, 18 Feb 1997 21:53:25 +0000 Subject: [PATCH] join(): join one or more path components --- Lib/macpath.py | 28 +++++++++++++++++----------- Lib/ntpath.py | 19 ++++++++++++------- Lib/posixpath.py | 19 ++++++++++++------- 3 files changed, 41 insertions(+), 25 deletions(-) diff --git a/Lib/macpath.py b/Lib/macpath.py index c45ecbf5049..a6cf66b19ff 100644 --- a/Lib/macpath.py +++ b/Lib/macpath.py @@ -20,18 +20,24 @@ def isabs(s): return ':' in s and s[0] <> ':' -# Join two pathnames. -# The result is equivalent to what the second pathname would refer to -# if the first pathname were the current directory. +# Join pathnames. +# Ignore the previous parts if a part is absolute. +# Insert a '/' unless the first part is empty or already ends in '/'. -def join(s, t): - if (not s) or isabs(t): return t - if t[:1] == ':': t = t[1:] - if ':' not in s: - s = ':' + s - if s[-1:] <> ':': - s = s + ':' - return s + t +def join(s, *p): + path = s + for t in p: + if (not s) or isabs(t): + path = t + continue + if t[:1] == ':': + t = t[1:] + if ':' not in path: + path = ':' + path + if path[-1:] <> ':': + path = path + ':' + path = path + t + return path # Split a pathname in two parts: the directory leading up to the final bit, diff --git a/Lib/ntpath.py b/Lib/ntpath.py index 6a77ec849be..d3951545e41 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -34,15 +34,20 @@ def isabs(s): return s != '' and s[:1] in '/\\' -# Join two pathnames. -# Ignore the first part if the second part is absolute. +# Join pathnames. +# Ignore the previous parts if a part is absolute. # Insert a '/' unless the first part is empty or already ends in '/'. -def join(a, b): - if isabs(b): return b - if a == '' or a[-1:] in '/\\': return a + b - # Note: join('x', '') returns 'x/'; is this what we want? - return a + os.sep + b +def join(a, *p): + path = a + for b in p: + if isabs(b): + path = b + elif path == '' or path[-1:] in '/\\': + path = path + b + else: + path = path + os.sep + b + return path # Split a path in a drive specification (a drive letter followed by a diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 014dfe27983..965184bc3c9 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -26,15 +26,20 @@ def isabs(s): return s[:1] == '/' -# Join two pathnames. -# Ignore the first part if the second part is absolute. +# Join pathnames. +# Ignore the previous parts if a part is absolute. # Insert a '/' unless the first part is empty or already ends in '/'. -def join(a, b): - if b[:1] == '/': return b - if a == '' or a[-1:] == '/': return a + b - # Note: join('x', '') returns 'x/'; is this what we want? - return a + '/' + b +def join(a, *p): + path = a + for b in p: + if b[:1] == '/': + path = b + elif path == '' or path[-1:] == '/': + path = path + b + else: + path = path + '/' + b + return path # Split a path in head (everything up to the last '/') and tail (the