Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(268)

Side by Side Diff: Lib/ntpath.py

Issue 4755: Common path prefix
Patch Set: Created 10 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Module 'ntpath' -- common operations on WinNT/Win95 pathnames 1 # Module 'ntpath' -- common operations on WinNT/Win95 pathnames
2 """Common pathname manipulations, WindowsNT/95 version. 2 """Common pathname manipulations, WindowsNT/95 version.
3 3
4 Instead of importing this module directly, import os and refer to this 4 Instead of importing this module directly, import os and refer to this
5 module as os.path. 5 module as os.path.
6 """ 6 """
7 7
8 import os 8 import os
9 import sys 9 import sys
10 import stat 10 import stat
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 if isinstance(path, bytes): 56 if isinstance(path, bytes):
57 return b'.' 57 return b'.'
58 else: 58 else:
59 return '.' 59 return '.'
60 60
61 def _get_colon(path): 61 def _get_colon(path):
62 if isinstance(path, bytes): 62 if isinstance(path, bytes):
63 return b':' 63 return b':'
64 else: 64 else:
65 return ':' 65 return ':'
66
67 # Return the longest prefix of all list elements.
68 def commonpathprefix(m, separator=None):
69 """Given a list of pathnames, returns the longest common leading component.
70
71 Unlike commonprefix(), commonpathprefix() considers path elements as
72 delimited by the defined separator (default: os.sep).
73 """
74 if not m:
75 return ''
76 if separator is None:
77 separator = r"\\|/"
78 import re
79 common = []
80 s1 = re.split(separator, min(m))
81 s2 = re.split(separator, max(m))
82 while s1 and s2 and s1[0] == s2[0]:
83 common.append(s1[0])
84 del s1[0], s2[0]
85 return "\\".join(common)
66 86
67 # Normalize the case of a pathname and map slashes to backslashes. 87 # Normalize the case of a pathname and map slashes to backslashes.
68 # Other normalizations (such as optimizing '../' away) are not done 88 # Other normalizations (such as optimizing '../' away) are not done
69 # (this is done by normpath). 89 # (this is done by normpath).
70 90
71 def normcase(s): 91 def normcase(s):
72 """Normalize case of pathname. 92 """Normalize case of pathname.
73 93
74 Makes all characters lowercase and all slashes into backslashes.""" 94 Makes all characters lowercase and all slashes into backslashes."""
75 return s.replace(_get_altsep(s), _get_sep(s)).lower() 95 return s.replace(_get_altsep(s), _get_sep(s)).lower()
(...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after
547 i += 1 567 i += 1
548 568
549 if isinstance(path, bytes): 569 if isinstance(path, bytes):
550 pardir = b'..' 570 pardir = b'..'
551 else: 571 else:
552 pardir = '..' 572 pardir = '..'
553 rel_list = [pardir] * (len(start_list)-i) + path_list[i:] 573 rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
554 if not rel_list: 574 if not rel_list:
555 return _get_dot(path) 575 return _get_dot(path)
556 return join(*rel_list) 576 return join(*rel_list)
OLDNEW
« Doc/library/os.path.rst ('K') | « Lib/macpath.py ('k') | Lib/os2emxpath.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+