annotate Lib/ntpath.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents fe813f5711a5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
1 # Module 'ntpath' -- common operations on WinNT/Win95 pathnames
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
2 """Common pathname manipulations, WindowsNT/95 version.
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
3
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
4 Instead of importing this module directly, import os and refer to this
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
5 module as os.path.
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
6 """
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 import os
45468
1d84bf5f9478 Patch #2167 from calvin: Remove unused imports
Christian Heimes <christian@cheimes.de>
parents: 44522
diff changeset
9 import sys
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 import stat
41057
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 40649
diff changeset
11 import genericpath
46771
b93005712e39 Add Py3k warnings to os.path.walk
Benjamin Peterson <benjamin@python.org>
parents: 45468
diff changeset
12 import warnings
b93005712e39 Add Py3k warnings to os.path.walk
Benjamin Peterson <benjamin@python.org>
parents: 45468
diff changeset
13
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents: 37908
diff changeset
14 from genericpath import *
13581
e871bd05c6a7 forgot to change copy.copy(m) to m[:]
Skip Montanaro <skip@pobox.com>
parents: 13577
diff changeset
15
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
16 __all__ = ["normcase","isabs","join","splitdrive","split","splitext",
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
17 "basename","dirname","commonprefix","getsize","getmtime",
35376
e7ed0e6aed2f Bug #1266283: lexists() is not exported from os.path
Georg Brandl <georg@python.org>
parents: 35308
diff changeset
18 "getatime","getctime", "islink","exists","lexists","isdir","isfile",
e7ed0e6aed2f Bug #1266283: lexists() is not exported from os.path
Georg Brandl <georg@python.org>
parents: 35308
diff changeset
19 "ismount","walk","expanduser","expandvars","normpath","abspath",
e7ed0e6aed2f Bug #1266283: lexists() is not exported from os.path
Georg Brandl <georg@python.org>
parents: 35308
diff changeset
20 "splitunc","curdir","pardir","sep","pathsep","defpath","altsep",
41218
f3d4274d063d Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents: 41174
diff changeset
21 "extsep","devnull","realpath","supports_unicode_filenames","relpath"]
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
23 # strings representing various path-related bits and pieces
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
24 curdir = '.'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
25 pardir = '..'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
26 extsep = '.'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
27 sep = '\\'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
28 pathsep = ';'
28427
9d3a25e99075 make nt altsep forward slash - closes bug 709428
Skip Montanaro <skip@pobox.com>
parents: 27908
diff changeset
29 altsep = '/'
27908
47b8cabab7b1 Tweak to Skip's checkin of patch 686397:
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 27897
diff changeset
30 defpath = '.;C:\\bin'
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
31 if 'ce' in sys.builtin_module_names:
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
32 defpath = '\\Windows'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
33 elif 'os2' in sys.builtin_module_names:
27908
47b8cabab7b1 Tweak to Skip's checkin of patch 686397:
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 27897
diff changeset
34 # OS/2 w/ VACPP
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
35 altsep = '/'
32302
e196b823e6c5 Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
36 devnull = 'nul'
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
37
5976
5b2c595428fa Rewrite normcase() using string.translate...
Guido van Rossum <guido@python.org>
parents: 5703
diff changeset
38 # Normalize the case of a pathname and map slashes to backslashes.
5b2c595428fa Rewrite normcase() using string.translate...
Guido van Rossum <guido@python.org>
parents: 5703
diff changeset
39 # Other normalizations (such as optimizing '../' away) are not done
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 # (this is done by normpath).
5976
5b2c595428fa Rewrite normcase() using string.translate...
Guido van Rossum <guido@python.org>
parents: 5703
diff changeset
41
7351
1baca5d2ba57 (1) Change normpath() to *not* also call normcase().
Guido van Rossum <guido@python.org>
parents: 7123
diff changeset
42 def normcase(s):
1baca5d2ba57 (1) Change normpath() to *not* also call normcase().
Guido van Rossum <guido@python.org>
parents: 7123
diff changeset
43 """Normalize case of pathname.
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
45 Makes all characters lowercase and all slashes into backslashes."""
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
46 return s.replace("/", "\\").lower()
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
5703
03dd1ded5cb3 Support $HOME in expanduser().
Guido van Rossum <guido@python.org>
parents: 5136
diff changeset
48
11804
57788da16488 Typo in a comment: "wheter" --> "whether"
Fred Drake <fdrake@acm.org>
parents: 11693
diff changeset
49 # Return whether a path is absolute.
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 # Trivial in Posix, harder on the Mac or MS-DOS.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 # For DOS it is absolute if it starts with a slash or backslash (current
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
52 # volume), or if a pathname after the volume letter and colon / UNC resource
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
53 # starts with a slash or backslash.
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 def isabs(s):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
56 """Test whether a path is absolute"""
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
57 s = splitdrive(s)[1]
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
58 return s != '' and s[:1] in '/\\'
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
5703
03dd1ded5cb3 Support $HOME in expanduser().
Guido van Rossum <guido@python.org>
parents: 5136
diff changeset
61 # Join two (or more) paths.
03dd1ded5cb3 Support $HOME in expanduser().
Guido van Rossum <guido@python.org>
parents: 5136
diff changeset
62
5133
666152b77027 join(): join one or more path components
Barry Warsaw <barry@python.org>
parents: 5027
diff changeset
63 def join(a, *p):
42747
9a347ba17cab Bug #1688564: document os.path.join's absolute path behavior in the docstring.
Georg Brandl <georg@python.org>
parents: 41218
diff changeset
64 """Join two or more pathname components, inserting "\\" as needed.
9a347ba17cab Bug #1688564: document os.path.join's absolute path behavior in the docstring.
Georg Brandl <georg@python.org>
parents: 41218
diff changeset
65 If any component is an absolute path, all previous path components
9a347ba17cab Bug #1688564: document os.path.join's absolute path behavior in the docstring.
Georg Brandl <georg@python.org>
parents: 41218
diff changeset
66 will be discarded."""
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
67 path = a
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
68 for b in p:
18847
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
69 b_wins = 0 # set to 1 iff b makes path irrelevant
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
70 if path == "":
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
71 b_wins = 1
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
72
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
73 elif isabs(b):
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
74 # This probably wipes out path so far. However, it's more
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
75 # complicated if path begins with a drive letter:
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
76 # 1. join('c:', '/a') == 'c:/a'
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
77 # 2. join('c:/', '/a') == 'c:/a'
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
78 # But
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
79 # 3. join('c:/a', '/b') == '/b'
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
80 # 4. join('c:', 'd:/') = 'd:/'
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
81 # 5. join('c:/', 'd:/') = 'd:/'
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
82 if path[1:2] != ":" or b[1:2] == ":":
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
83 # Path doesn't start with a drive letter, or cases 4 and 5.
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
84 b_wins = 1
18742
920e0361dd6e SF bug #44271: os.path.expanduser problem w/o HOME set.
Tim Peters <tim.peters@gmail.com>
parents: 17836
diff changeset
85
18847
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
86 # Else path has a drive letter, and b doesn't but is absolute.
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
87 elif len(path) > 3 or (len(path) == 3 and
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
88 path[-1] not in "/\\"):
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
89 # case 3
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
90 b_wins = 1
18742
920e0361dd6e SF bug #44271: os.path.expanduser problem w/o HOME set.
Tim Peters <tim.peters@gmail.com>
parents: 17836
diff changeset
91
18847
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
92 if b_wins:
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
93 path = b
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
94 else:
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
95 # Join, and ensure there's a separator.
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
96 assert len(path) > 0
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
97 if path[-1] in "/\\":
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
98 if b and b[0] in "/\\":
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
99 path += b[1:]
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
100 else:
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
101 path += b
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
102 elif path[-1] == ":":
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
103 path += b
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
104 elif b:
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
105 if b[0] in "/\\":
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
106 path += b
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
107 else:
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
108 path += "\\" + b
20887
0c17515b05db SF bug 478425: Change in os.path.join (ntpath.py)
Tim Peters <tim.peters@gmail.com>
parents: 20361
diff changeset
109 else:
0c17515b05db SF bug 478425: Change in os.path.join (ntpath.py)
Tim Peters <tim.peters@gmail.com>
parents: 20361
diff changeset
110 # path is not empty and does not end with a backslash,
0c17515b05db SF bug 478425: Change in os.path.join (ntpath.py)
Tim Peters <tim.peters@gmail.com>
parents: 20361
diff changeset
111 # but b is empty; since, e.g., split('a/') produces
0c17515b05db SF bug 478425: Change in os.path.join (ntpath.py)
Tim Peters <tim.peters@gmail.com>
parents: 20361
diff changeset
112 # ('a', ''), it's best if join() adds a backslash in
0c17515b05db SF bug 478425: Change in os.path.join (ntpath.py)
Tim Peters <tim.peters@gmail.com>
parents: 20361
diff changeset
113 # this case.
0c17515b05db SF bug 478425: Change in os.path.join (ntpath.py)
Tim Peters <tim.peters@gmail.com>
parents: 20361
diff changeset
114 path += '\\'
18742
920e0361dd6e SF bug #44271: os.path.expanduser problem w/o HOME set.
Tim Peters <tim.peters@gmail.com>
parents: 17836
diff changeset
115
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
116 return path
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
117
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
118
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 # Split a path in a drive specification (a drive letter followed by a
10523
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
120 # colon) and the path specification.
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 # It is always true that drivespec + pathspec == p
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 def splitdrive(p):
10523
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
123 """Split a pathname into drive and path specifiers. Returns a 2-tuple
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
124 "(drive,path)"; either part may be empty"""
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
125 if p[1:2] == ':':
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
126 return p[0:2], p[2:]
10523
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
127 return '', p
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
128
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
129
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
130 # Parse UNC paths
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
131 def splitunc(p):
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
132 """Split a pathname into UNC mount point and relative path specifiers.
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
133
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
134 Return a 2-tuple (unc, rest); either part may be empty.
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
135 If unc is not empty, it has the form '//host/mount' (or similar
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
136 using backslashes). unc+rest is always the input path.
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
137 Paths containing drive letters never have an UNC part.
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
138 """
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
139 if p[1:2] == ':':
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
140 return '', p # Drive letter present
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
141 firstTwo = p[0:2]
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
142 if firstTwo == '//' or firstTwo == '\\\\':
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
143 # is a UNC path:
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
144 # vvvvvvvvvvvvvvvvvvvv equivalent to drive letter
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
145 # \\machine\mountpoint\directories...
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
146 # directory ^^^^^^^^^^^^^^^
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
147 normp = normcase(p)
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
148 index = normp.find('\\', 2)
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
149 if index == -1:
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
150 ##raise RuntimeError, 'illegal UNC path: "' + p + '"'
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
151 return ("", p)
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
152 index = normp.find('\\', index + 1)
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
153 if index == -1:
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
154 index = len(p)
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
155 return p[:index], p[index:]
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
156 return '', p
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
157
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
158
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 # Split a path in head (everything up to the last '/') and tail (the
10433
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
160 # rest). After the trailing '/' is stripped, the invariant
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 # join(head, tail) == p holds.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 # The resulting head won't end in '/' unless it is the root.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
163
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 def split(p):
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
165 """Split a pathname.
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
166
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
167 Return tuple (head, tail) where tail is everything after the final slash.
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
168 Either part may be empty."""
10433
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
169
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
170 d, p = splitdrive(p)
10433
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
171 # set i to index beyond p's last slash
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
172 i = len(p)
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
173 while i and p[i-1] not in '/\\':
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
174 i = i - 1
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
175 head, tail = p[:i], p[i:] # now tail has no slashes
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
176 # remove trailing slashes from head, unless it's all slashes
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
177 head2 = head
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
178 while head2 and head2[-1] in '/\\':
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
179 head2 = head2[:-1]
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
180 head = head2 or head
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
181 return d + head, tail
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
182
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
183
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 # Split a path in root and extension.
5027
0ea4ea5b2eb7 Fix splitext() to go up to the last dot, not the first.
Guido van Rossum <guido@python.org>
parents: 4150
diff changeset
185 # The extension is everything starting at the last dot in the last
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 # pathname component; the root is everything before that.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 # It is always true that root + ext == p.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
188
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 def splitext(p):
41057
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 40649
diff changeset
190 return genericpath._splitext(p, sep, altsep, extsep)
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 40649
diff changeset
191 splitext.__doc__ = genericpath._splitext.__doc__
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
192
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
193
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 # Return the tail (basename) part of a path.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
195
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 def basename(p):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
197 """Returns the final component of a pathname"""
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
198 return split(p)[1]
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
199
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
200
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 # Return the head (dirname) part of a path.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
202
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 def dirname(p):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
204 """Returns the directory component of a pathname"""
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
205 return split(p)[0]
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
206
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 # Is a path a symbolic link?
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 # This will always return false on systems where posix.lstat doesn't exist.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
209
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 def islink(path):
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents: 37908
diff changeset
211 """Test for symbolic link.
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents: 37908
diff changeset
212 On WindowsNT/95 and OS/2 always returns false
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents: 37908
diff changeset
213 """
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22830
diff changeset
214 return False
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
215
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents: 37908
diff changeset
216 # alias exists to lexists
33332
062177de8685 Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents: 32595
diff changeset
217 lexists = exists
062177de8685 Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents: 32595
diff changeset
218
10523
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
219 # Is a path a mount point? Either a root (with or without drive letter)
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
220 # or an UNC path with at most a / or \ after the mount point.
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
221
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 def ismount(path):
7123
5d517ddcbd2e Fix to ismount(). Can't remember who told me this.
Guido van Rossum <guido@python.org>
parents: 6756
diff changeset
223 """Test whether a path is a mount point (defined as root of drive)"""
10523
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
224 unc, rest = splitunc(path)
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
225 if unc:
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
226 return rest in ("", "/", "\\")
7123
5d517ddcbd2e Fix to ismount(). Can't remember who told me this.
Guido van Rossum <guido@python.org>
parents: 6756
diff changeset
227 p = splitdrive(path)[1]
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
228 return len(p) == 1 and p[0] in '/\\'
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
229
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
230
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 # Directory tree walk.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 # For each directory under top (including top itself, but excluding
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 # '.' and '..'), func(arg, dirname, filenames) is called, where
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 # dirname is the name of the directory and filenames is the list
30723
6f15e73b59f2 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 28427
diff changeset
235 # of files (and subdirectories etc.) in the directory.
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 # The func may modify the filenames list, to implement a filter,
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 # or to impose a different order of visiting.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
238
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 def walk(top, func, arg):
20361
c408333d37f0 SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents: 19934
diff changeset
240 """Directory tree walk with callback function.
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
241
20361
c408333d37f0 SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents: 19934
diff changeset
242 For each directory in the directory tree rooted at top (including top
c408333d37f0 SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents: 19934
diff changeset
243 itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
c408333d37f0 SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents: 19934
diff changeset
244 dirname is the name of the directory, and fnames a list of the names of
c408333d37f0 SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents: 19934
diff changeset
245 the files and subdirectories in dirname (excluding '.' and '..'). func
c408333d37f0 SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents: 19934
diff changeset
246 may modify the fnames list in-place (e.g. via del or slice assignment),
c408333d37f0 SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents: 19934
diff changeset
247 and walk will only recurse into the subdirectories whose names remain in
c408333d37f0 SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents: 19934
diff changeset
248 fnames; this can be used to implement a filter, or to impose a specific
c408333d37f0 SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents: 19934
diff changeset
249 order of visiting. No semantics are defined for, or required of, arg,
c408333d37f0 SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents: 19934
diff changeset
250 beyond that arg is always passed to func. It can be used, e.g., to pass
c408333d37f0 SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents: 19934
diff changeset
251 a filename pattern, or a mutable object designed to accumulate
c408333d37f0 SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents: 19934
diff changeset
252 statistics. Passing None for arg is common."""
53903
a36fa2ce61b1 Merged revisions 72458 via svnmerge from
Philip Jenvey <pjenvey@underboss.org>
parents: 46771
diff changeset
253 warnings.warnpy3k("In 3.x, os.path.walk is removed in favor of os.walk.",
a36fa2ce61b1 Merged revisions 72458 via svnmerge from
Philip Jenvey <pjenvey@underboss.org>
parents: 46771
diff changeset
254 stacklevel=2)
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
255 try:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
256 names = os.listdir(top)
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
257 except os.error:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
258 return
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
259 func(arg, top, names)
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
260 for name in names:
44521
3bc0405de491 #1696393: don't check for '.' and '..' in ntpath.walk since
Georg Brandl <georg@python.org>
parents: 42747
diff changeset
261 name = join(top, name)
3bc0405de491 #1696393: don't check for '.' and '..' in ntpath.walk since
Georg Brandl <georg@python.org>
parents: 42747
diff changeset
262 if isdir(name):
3bc0405de491 #1696393: don't check for '.' and '..' in ntpath.walk since
Georg Brandl <georg@python.org>
parents: 42747
diff changeset
263 walk(name, func, arg)
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
264
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
265
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 # Expand paths beginning with '~' or '~user'.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 # '~' means $HOME; '~user' means that user's home directory.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 # If the path doesn't begin with '~', or if the user or $HOME is unknown,
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 # the path is returned unchanged (leaving error reporting to whatever
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 # function is called with the expanded path as argument).
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 # See also module 'glob' for expansion of *, ? and [...] in pathnames.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 # (A function should also be defined to do full *sh-style environment
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 # variable expansion.)
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
274
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 def expanduser(path):
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
276 """Expand ~ and ~user constructs.
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
277
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
278 If user or $HOME is unknown, do nothing."""
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 14910
diff changeset
279 if path[:1] != '~':
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
280 return path
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
281 i, n = 1, len(path)
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
282 while i < n and path[i] not in '/\\':
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
283 i = i + 1
41174
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
284
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
285 if 'HOME' in os.environ:
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
286 userhome = os.environ['HOME']
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
287 elif 'USERPROFILE' in os.environ:
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
288 userhome = os.environ['USERPROFILE']
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
289 elif not 'HOMEPATH' in os.environ:
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
290 return path
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
291 else:
41174
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
292 try:
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
293 drive = os.environ['HOMEDRIVE']
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
294 except KeyError:
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
295 drive = ''
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
296 userhome = join(drive, os.environ['HOMEPATH'])
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
297
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
298 if i != 1: #~user
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
299 userhome = join(dirname(userhome), path[1:i])
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
300
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
301 return userhome + path[i:]
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
302
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
303
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 # Expand paths containing shell variable substitutions.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 # The following rules apply:
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
306 # - no expansion within single quotes
41174
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
307 # - '$$' is translated into '$'
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
308 # - '%%' is translated into '%' if '%%' are not seen in %var1%%var2%
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
309 # - ${varname} is accepted.
41174
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
310 # - $varname is accepted.
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
311 # - %varname% is accepted.
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
312 # - varnames can be made out of letters, digits and the characters '_-'
68588
cd8d3c2d3bce #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65537
diff changeset
313 # (though is not verified in the ${varname} and %varname% cases)
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 # XXX With COMMAND.COM you can use any characters in a variable name,
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 # XXX except '^|<>='.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
316
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
317 def expandvars(path):
41174
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
318 """Expand shell variables of the forms $var, ${var} and %var%.
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
319
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
320 Unknown variables are left unchanged."""
41174
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
321 if '$' not in path and '%' not in path:
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
322 return path
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
323 import string
18781
01ae962df885 Use string.ascii_letters instead of string.letters (SF bug #226706).
Fred Drake <fdrake@acm.org>
parents: 18742
diff changeset
324 varchars = string.ascii_letters + string.digits + '_-'
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
325 res = ''
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
326 index = 0
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
327 pathlen = len(path)
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
328 while index < pathlen:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
329 c = path[index]
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
330 if c == '\'': # no expansion within single quotes
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
331 path = path[index + 1:]
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
332 pathlen = len(path)
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
333 try:
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
334 index = path.index('\'')
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
335 res = res + '\'' + path[:index + 1]
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
336 except ValueError:
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
337 res = res + path
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
338 index = pathlen - 1
41174
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
339 elif c == '%': # variable or '%'
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
340 if path[index + 1:index + 2] == '%':
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
341 res = res + c
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
342 index = index + 1
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
343 else:
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
344 path = path[index+1:]
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
345 pathlen = len(path)
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
346 try:
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
347 index = path.index('%')
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
348 except ValueError:
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
349 res = res + '%' + path
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
350 index = pathlen - 1
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
351 else:
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
352 var = path[:index]
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
353 if var in os.environ:
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
354 res = res + os.environ[var]
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
355 else:
f1f186460250 Patch #957650: "%var%" environment variable references are now properly
Georg Brandl <georg@python.org>
parents: 41057
diff changeset
356 res = res + '%' + var + '%'
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
357 elif c == '$': # variable or '$$'
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
358 if path[index + 1:index + 2] == '$':
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
359 res = res + c
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
360 index = index + 1
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
361 elif path[index + 1:index + 2] == '{':
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
362 path = path[index+2:]
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
363 pathlen = len(path)
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
364 try:
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
365 index = path.index('}')
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
366 var = path[:index]
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 22867
diff changeset
367 if var in os.environ:
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
368 res = res + os.environ[var]
40649
a4cdb9c8af25 Fixed ntpath.expandvars to not replace references to non-existing
Sjoerd Mullender <sjoerd@acm.org>
parents: 39689
diff changeset
369 else:
a4cdb9c8af25 Fixed ntpath.expandvars to not replace references to non-existing
Sjoerd Mullender <sjoerd@acm.org>
parents: 39689
diff changeset
370 res = res + '${' + var + '}'
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
371 except ValueError:
40649
a4cdb9c8af25 Fixed ntpath.expandvars to not replace references to non-existing
Sjoerd Mullender <sjoerd@acm.org>
parents: 39689
diff changeset
372 res = res + '${' + path
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
373 index = pathlen - 1
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
374 else:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
375 var = ''
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
376 index = index + 1
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
377 c = path[index:index + 1]
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
378 while c != '' and c in varchars:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
379 var = var + c
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
380 index = index + 1
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
381 c = path[index:index + 1]
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 22867
diff changeset
382 if var in os.environ:
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
383 res = res + os.environ[var]
40649
a4cdb9c8af25 Fixed ntpath.expandvars to not replace references to non-existing
Sjoerd Mullender <sjoerd@acm.org>
parents: 39689
diff changeset
384 else:
a4cdb9c8af25 Fixed ntpath.expandvars to not replace references to non-existing
Sjoerd Mullender <sjoerd@acm.org>
parents: 39689
diff changeset
385 res = res + '$' + var
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
386 if c != '':
40649
a4cdb9c8af25 Fixed ntpath.expandvars to not replace references to non-existing
Sjoerd Mullender <sjoerd@acm.org>
parents: 39689
diff changeset
387 index = index - 1
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
388 else:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
389 res = res + c
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
390 index = index + 1
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
391 return res
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
392
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
393
19628
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
394 # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A\B.
4150
8a0d43b13125 Don't do truncation to 8+3 format -- this is used on NT file systems!
Guido van Rossum <guido@python.org>
parents: 3953
diff changeset
395 # Previously, this function also truncated pathnames to 8+3 format,
8a0d43b13125 Don't do truncation to 8+3 format -- this is used on NT file systems!
Guido van Rossum <guido@python.org>
parents: 3953
diff changeset
396 # but as this module is called "ntpath", that's obviously wrong!
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
397
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 def normpath(path):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
399 """Normalize path, eliminating double slashes, etc."""
58269
8fd12ea63c6a #5827: make sure that normpath preserves unicode
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53903
diff changeset
400 # Preserve unicode (if path is unicode)
8fd12ea63c6a #5827: make sure that normpath preserves unicode
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53903
diff changeset
401 backslash, dot = (u'\\', u'.') if isinstance(path, unicode) else ('\\', '.')
63445
9f2fbee420da Merged revisions 83352,83356-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 58963
diff changeset
402 if path.startswith(('\\\\.\\', '\\\\?\\')):
9f2fbee420da Merged revisions 83352,83356-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 58963
diff changeset
403 # in the case of paths with these prefixes:
9f2fbee420da Merged revisions 83352,83356-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 58963
diff changeset
404 # \\.\ -> device names
9f2fbee420da Merged revisions 83352,83356-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 58963
diff changeset
405 # \\?\ -> literal paths
9f2fbee420da Merged revisions 83352,83356-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 58963
diff changeset
406 # do not do any normalization, but return the path unchanged
9f2fbee420da Merged revisions 83352,83356-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 58963
diff changeset
407 return path
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
408 path = path.replace("/", "\\")
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
409 prefix, path = splitdrive(path)
32595
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
410 # We need to be careful here. If the prefix is empty, and the path starts
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
411 # with a backslash, it could either be an absolute path on the current
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
412 # drive (\dir1\dir2\file) or a UNC filename (\\server\mount\dir1\file). It
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
413 # is therefore imperative NOT to collapse multiple backslashes blindly in
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
414 # that case.
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
415 # The code below preserves multiple backslashes when there is no drive
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
416 # letter. This means that the invalid filename \\\a\b is preserved
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
417 # unchanged, where a\\\b is normalised to a\b. It's not clear that there
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
418 # is any better behaviour for such edge cases.
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
419 if prefix == '':
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
420 # No drive letter - preserve initial backslashes
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
421 while path[:1] == "\\":
58269
8fd12ea63c6a #5827: make sure that normpath preserves unicode
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53903
diff changeset
422 prefix = prefix + backslash
32595
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
423 path = path[1:]
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
424 else:
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
425 # We have a drive letter - collapse initial backslashes
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
426 if path.startswith("\\"):
58269
8fd12ea63c6a #5827: make sure that normpath preserves unicode
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53903
diff changeset
427 prefix = prefix + backslash
32595
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
428 path = path.lstrip("\\")
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
429 comps = path.split("\\")
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
430 i = 0
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
431 while i < len(comps):
19628
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
432 if comps[i] in ('.', ''):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
433 del comps[i]
19628
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
434 elif comps[i] == '..':
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
435 if i > 0 and comps[i-1] != '..':
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
436 del comps[i-1:i+1]
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
437 i -= 1
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
438 elif i == 0 and prefix.endswith("\\"):
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
439 del comps[i]
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
440 else:
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
441 i += 1
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
442 else:
19628
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
443 i += 1
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
444 # If the path is now empty, substitute '.'
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
445 if not prefix and not comps:
58269
8fd12ea63c6a #5827: make sure that normpath preserves unicode
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53903
diff changeset
446 comps.append(dot)
8fd12ea63c6a #5827: make sure that normpath preserves unicode
Ezio Melotti <ezio.melotti@gmail.com>
parents: 53903
diff changeset
447 return prefix + backslash.join(comps)
10018
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
448
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
449
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
450 # Return an absolute path.
37908
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
451 try:
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
452 from nt import _getfullpathname
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
453
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
454 except ImportError: # not running on Windows - mock up something sensible
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
455 def abspath(path):
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
456 """Return the absolute version of a path."""
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
457 if not isabs(path):
58963
eaf8a93cdbff #3426: os.path.abspath now returns unicode when its arg is unicode.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58269
diff changeset
458 if isinstance(path, unicode):
eaf8a93cdbff #3426: os.path.abspath now returns unicode when its arg is unicode.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58269
diff changeset
459 cwd = os.getcwdu()
eaf8a93cdbff #3426: os.path.abspath now returns unicode when its arg is unicode.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58269
diff changeset
460 else:
eaf8a93cdbff #3426: os.path.abspath now returns unicode when its arg is unicode.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58269
diff changeset
461 cwd = os.getcwd()
eaf8a93cdbff #3426: os.path.abspath now returns unicode when its arg is unicode.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58269
diff changeset
462 path = join(cwd, path)
37908
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
463 return normpath(path)
21743
34f85a0a7faa Allow abspath to still do something sensisble if the nt module can not be imported.
Mark Hammond <mhammond@skippinet.com.au>
parents: 20887
diff changeset
464
37908
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
465 else: # use native Windows method on Windows
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
466 def abspath(path):
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
467 """Return the absolute version of a path."""
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
468
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
469 if path: # Empty path must return current working directory.
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
470 try:
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
471 path = _getfullpathname(path)
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
472 except WindowsError:
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
473 pass # Bad path - return unchanged.
58963
eaf8a93cdbff #3426: os.path.abspath now returns unicode when its arg is unicode.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58269
diff changeset
474 elif isinstance(path, unicode):
eaf8a93cdbff #3426: os.path.abspath now returns unicode when its arg is unicode.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58269
diff changeset
475 path = os.getcwdu()
37908
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
476 else:
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
477 path = os.getcwd()
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
478 return normpath(path)
19934
f2f25a738f7f SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents: 19628
diff changeset
479
f2f25a738f7f SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents: 19628
diff changeset
480 # realpath is a no-op on systems without islink support
f2f25a738f7f SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents: 19628
diff changeset
481 realpath = abspath
25666
4a825b902310 Add os.path.supports_unicode_filenames for all platforms,
Mark Hammond <mhammond@skippinet.com.au>
parents: 23625
diff changeset
482 # Win9x family and earlier have no Unicode filename support.
25676
bac5a4932905 Don't try to access sys.getwindowsversion unless it exists (ntpath is
Tim Peters <tim.peters@gmail.com>
parents: 25666
diff changeset
483 supports_unicode_filenames = (hasattr(sys, "getwindowsversion") and
bac5a4932905 Don't try to access sys.getwindowsversion unless it exists (ntpath is
Tim Peters <tim.peters@gmail.com>
parents: 25666
diff changeset
484 sys.getwindowsversion()[3] >= 2)
41218
f3d4274d063d Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents: 41174
diff changeset
485
65537
4f0dddaf82d6 Issue #5117: My previous commit for this issue broke posix buildbot.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65524
diff changeset
486 def _abspath_split(path):
4f0dddaf82d6 Issue #5117: My previous commit for this issue broke posix buildbot.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65524
diff changeset
487 abs = abspath(normpath(path))
4f0dddaf82d6 Issue #5117: My previous commit for this issue broke posix buildbot.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65524
diff changeset
488 prefix, rest = splitunc(abs)
4f0dddaf82d6 Issue #5117: My previous commit for this issue broke posix buildbot.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65524
diff changeset
489 is_unc = bool(prefix)
4f0dddaf82d6 Issue #5117: My previous commit for this issue broke posix buildbot.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65524
diff changeset
490 if not is_unc:
4f0dddaf82d6 Issue #5117: My previous commit for this issue broke posix buildbot.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65524
diff changeset
491 prefix, rest = splitdrive(abs)
4f0dddaf82d6 Issue #5117: My previous commit for this issue broke posix buildbot.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65524
diff changeset
492 return is_unc, prefix, [x for x in rest.split(sep) if x]
4f0dddaf82d6 Issue #5117: My previous commit for this issue broke posix buildbot.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65524
diff changeset
493
41218
f3d4274d063d Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents: 41174
diff changeset
494 def relpath(path, start=curdir):
f3d4274d063d Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents: 41174
diff changeset
495 """Return a relative version of a path"""
f3d4274d063d Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents: 41174
diff changeset
496
f3d4274d063d Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents: 41174
diff changeset
497 if not path:
f3d4274d063d Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents: 41174
diff changeset
498 raise ValueError("no path specified")
65537
4f0dddaf82d6 Issue #5117: My previous commit for this issue broke posix buildbot.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65524
diff changeset
499
4f0dddaf82d6 Issue #5117: My previous commit for this issue broke posix buildbot.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65524
diff changeset
500 start_is_unc, start_prefix, start_list = _abspath_split(start)
4f0dddaf82d6 Issue #5117: My previous commit for this issue broke posix buildbot.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65524
diff changeset
501 path_is_unc, path_prefix, path_list = _abspath_split(path)
4f0dddaf82d6 Issue #5117: My previous commit for this issue broke posix buildbot.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65524
diff changeset
502
4f0dddaf82d6 Issue #5117: My previous commit for this issue broke posix buildbot.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65524
diff changeset
503 if path_is_unc ^ start_is_unc:
4f0dddaf82d6 Issue #5117: My previous commit for this issue broke posix buildbot.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65524
diff changeset
504 raise ValueError("Cannot mix UNC and non-UNC paths (%s and %s)"
4f0dddaf82d6 Issue #5117: My previous commit for this issue broke posix buildbot.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65524
diff changeset
505 % (path, start))
4f0dddaf82d6 Issue #5117: My previous commit for this issue broke posix buildbot.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65524
diff changeset
506 if path_prefix.lower() != start_prefix.lower():
4f0dddaf82d6 Issue #5117: My previous commit for this issue broke posix buildbot.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65524
diff changeset
507 if path_is_unc:
4f0dddaf82d6 Issue #5117: My previous commit for this issue broke posix buildbot.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65524
diff changeset
508 raise ValueError("path is on UNC root %s, start on UNC root %s"
4f0dddaf82d6 Issue #5117: My previous commit for this issue broke posix buildbot.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65524
diff changeset
509 % (path_prefix, start_prefix))
41218
f3d4274d063d Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents: 41174
diff changeset
510 else:
f3d4274d063d Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents: 41174
diff changeset
511 raise ValueError("path is on drive %s, start on drive %s"
65537
4f0dddaf82d6 Issue #5117: My previous commit for this issue broke posix buildbot.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65524
diff changeset
512 % (path_prefix, start_prefix))
41218
f3d4274d063d Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents: 41174
diff changeset
513 # Work out how much of the filepath is shared by start and path.
65537
4f0dddaf82d6 Issue #5117: My previous commit for this issue broke posix buildbot.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65524
diff changeset
514 i = 0
4f0dddaf82d6 Issue #5117: My previous commit for this issue broke posix buildbot.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65524
diff changeset
515 for e1, e2 in zip(start_list, path_list):
4f0dddaf82d6 Issue #5117: My previous commit for this issue broke posix buildbot.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 65524
diff changeset
516 if e1.lower() != e2.lower():
41218
f3d4274d063d Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents: 41174
diff changeset
517 break
f3d4274d063d Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents: 41174
diff changeset
518 i += 1
f3d4274d063d Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents: 41174
diff changeset
519
f3d4274d063d Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents: 41174
diff changeset
520 rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
44522
4321a5abd6ce #1742: don't raise exception on os.path.relpath("a", "a"), but return os.curdir.
Georg Brandl <georg@python.org>
parents: 44521
diff changeset
521 if not rel_list:
4321a5abd6ce #1742: don't raise exception on os.path.relpath("a", "a"), but return os.curdir.
Georg Brandl <georg@python.org>
parents: 44521
diff changeset
522 return curdir
41218
f3d4274d063d Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents: 41174
diff changeset
523 return join(*rel_list)
70715
Brian Curtin <brian@python.org>
parents: 68588
diff changeset
524
Brian Curtin <brian@python.org>
parents: 68588
diff changeset
525 try:
Brian Curtin <brian@python.org>
parents: 68588
diff changeset
526 # The genericpath.isdir implementation uses os.stat and checks the mode
Brian Curtin <brian@python.org>
parents: 68588
diff changeset
527 # attribute to tell whether or not the path is a directory.
Brian Curtin <brian@python.org>
parents: 68588
diff changeset
528 # This is overkill on Windows - just pass the path to GetFileAttributes
Brian Curtin <brian@python.org>
parents: 68588
diff changeset
529 # and check the attribute from there.
70738
fe813f5711a5 Correction to f1509fc75435 - Issue #11583
Brian Curtin <brian@python.org>
parents: 70715
diff changeset
530 from nt import _isdir as isdir
70715
Brian Curtin <brian@python.org>
parents: 68588
diff changeset
531 except ImportError:
70738
fe813f5711a5 Correction to f1509fc75435 - Issue #11583
Brian Curtin <brian@python.org>
parents: 70715
diff changeset
532 # Use genericpath.isdir as imported above.
fe813f5711a5 Correction to f1509fc75435 - Issue #11583
Brian Curtin <brian@python.org>
parents: 70715
diff changeset
533 pass