join(): join one or more path components

This commit is contained in:
Barry Warsaw 1997-02-18 21:53:25 +00:00
parent 736bb0659f
commit 384d249006
3 changed files with 41 additions and 25 deletions

View File

@ -20,18 +20,24 @@ def isabs(s):
return ':' in s and s[0] <> ':' return ':' in s and s[0] <> ':'
# Join two pathnames. # Join pathnames.
# The result is equivalent to what the second pathname would refer to # Ignore the previous parts if a part is absolute.
# if the first pathname were the current directory. # Insert a '/' unless the first part is empty or already ends in '/'.
def join(s, t): def join(s, *p):
if (not s) or isabs(t): return t path = s
if t[:1] == ':': t = t[1:] for t in p:
if ':' not in s: if (not s) or isabs(t):
s = ':' + s path = t
if s[-1:] <> ':': continue
s = s + ':' if t[:1] == ':':
return s + t 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, # Split a pathname in two parts: the directory leading up to the final bit,

View File

@ -34,15 +34,20 @@ def isabs(s):
return s != '' and s[:1] in '/\\' return s != '' and s[:1] in '/\\'
# Join two pathnames. # Join pathnames.
# Ignore the first part if the second part is absolute. # Ignore the previous parts if a part is absolute.
# Insert a '/' unless the first part is empty or already ends in '/'. # Insert a '/' unless the first part is empty or already ends in '/'.
def join(a, b): def join(a, *p):
if isabs(b): return b path = a
if a == '' or a[-1:] in '/\\': return a + b for b in p:
# Note: join('x', '') returns 'x/'; is this what we want? if isabs(b):
return a + os.sep + 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 # Split a path in a drive specification (a drive letter followed by a

View File

@ -26,15 +26,20 @@ def isabs(s):
return s[:1] == '/' return s[:1] == '/'
# Join two pathnames. # Join pathnames.
# Ignore the first part if the second part is absolute. # Ignore the previous parts if a part is absolute.
# Insert a '/' unless the first part is empty or already ends in '/'. # Insert a '/' unless the first part is empty or already ends in '/'.
def join(a, b): def join(a, *p):
if b[:1] == '/': return b path = a
if a == '' or a[-1:] == '/': return a + b for b in p:
# Note: join('x', '') returns 'x/'; is this what we want? if b[:1] == '/':
return a + '/' + b 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 # Split a path in head (everything up to the last '/') and tail (the