annotate Lib/ntpath.py @ 39042:c02fa187e0f4 legacy-trunk

Part of SF patch #1484695. This removes dead code. The chksum was already verified in .frombuf() on the lines above. If there was a problem an exception is raised, so there was no way this condition could have been true.
author Neal Norwitz <nnorwitz@gmail.com>
date Mon, 10 Jul 2006 00:23:17 +0000
parents c7ef92b9045c
children d0ed7dbaad56
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
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 import stat
25666
4a825b902310 Add os.path.supports_unicode_filenames for all platforms,
Mark Hammond <mhammond@skippinet.com.au>
parents: 23625
diff changeset
10 import sys
13581
e871bd05c6a7 forgot to change copy.copy(m) to m[:]
Skip Montanaro <skip@pobox.com>
parents: 13577
diff changeset
11
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
12 __all__ = ["normcase","isabs","join","splitdrive","split","splitext",
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
13 "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
14 "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
15 "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
16 "splitunc","curdir","pardir","sep","pathsep","defpath","altsep",
e7ed0e6aed2f Bug #1266283: lexists() is not exported from os.path
Georg Brandl <georg@python.org>
parents: 35308
diff changeset
17 "extsep","devnull","realpath","supports_unicode_filenames"]
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
19 # 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
20 curdir = '.'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
21 pardir = '..'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
22 extsep = '.'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
23 sep = '\\'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
24 pathsep = ';'
28427
9d3a25e99075 make nt altsep forward slash - closes bug 709428
Skip Montanaro <skip@pobox.com>
parents: 27908
diff changeset
25 altsep = '/'
27908
47b8cabab7b1 Tweak to Skip's checkin of patch 686397:
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 27897
diff changeset
26 defpath = '.;C:\\bin'
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
27 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
28 defpath = '\\Windows'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
29 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
30 # OS/2 w/ VACPP
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
31 altsep = '/'
32302
e196b823e6c5 Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
32 devnull = 'nul'
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
33
5976
5b2c595428fa Rewrite normcase() using string.translate...
Guido van Rossum <guido@python.org>
parents: 5703
diff changeset
34 # 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
35 # 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
36 # (this is done by normpath).
5976
5b2c595428fa Rewrite normcase() using string.translate...
Guido van Rossum <guido@python.org>
parents: 5703
diff changeset
37
7351
1baca5d2ba57 (1) Change normpath() to *not* also call normcase().
Guido van Rossum <guido@python.org>
parents: 7123
diff changeset
38 def normcase(s):
1baca5d2ba57 (1) Change normpath() to *not* also call normcase().
Guido van Rossum <guido@python.org>
parents: 7123
diff changeset
39 """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
40
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
41 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
42 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
43
5703
03dd1ded5cb3 Support $HOME in expanduser().
Guido van Rossum <guido@python.org>
parents: 5136
diff changeset
44
11804
57788da16488 Typo in a comment: "wheter" --> "whether"
Fred Drake <fdrake@acm.org>
parents: 11693
diff changeset
45 # 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
46 # 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
47 # 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
48 # 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
49 # 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
50
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 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
52 """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
53 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
54 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
55
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
56
5703
03dd1ded5cb3 Support $HOME in expanduser().
Guido van Rossum <guido@python.org>
parents: 5136
diff changeset
57 # Join two (or more) paths.
03dd1ded5cb3 Support $HOME in expanduser().
Guido van Rossum <guido@python.org>
parents: 5136
diff changeset
58
5133
666152b77027 join(): join one or more path components
Barry Warsaw <barry@python.org>
parents: 5027
diff changeset
59 def join(a, *p):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
60 """Join two or more pathname components, inserting "\\" as needed"""
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
61 path = a
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
62 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
63 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
64 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
65 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
66
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
67 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
68 # 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
69 # 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
70 # 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
71 # 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
72 # 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
73 # 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
74 # 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
75 # 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
76 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
77 # 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
78 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
79
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
80 # 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
81 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
82 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
83 # 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
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 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
87 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
88 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
89 # 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
90 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
91 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
92 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
93 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
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 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
96 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
97 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
98 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
99 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
100 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
101 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
102 path += "\\" + b
20887
0c17515b05db SF bug 478425: Change in os.path.join (ntpath.py)
Tim Peters <tim.peters@gmail.com>
parents: 20361
diff changeset
103 else:
0c17515b05db SF bug 478425: Change in os.path.join (ntpath.py)
Tim Peters <tim.peters@gmail.com>
parents: 20361
diff changeset
104 # 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
105 # 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
106 # ('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
107 # this case.
0c17515b05db SF bug 478425: Change in os.path.join (ntpath.py)
Tim Peters <tim.peters@gmail.com>
parents: 20361
diff changeset
108 path += '\\'
18742
920e0361dd6e SF bug #44271: os.path.expanduser problem w/o HOME set.
Tim Peters <tim.peters@gmail.com>
parents: 17836
diff changeset
109
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
110 return path
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
111
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
112
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 # 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
114 # 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
115 # 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
116 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
117 """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
118 "(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
119 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
120 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
121 return '', p
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
122
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
123
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
124 # Parse UNC paths
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
125 def splitunc(p):
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
126 """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
127
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
128 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
129 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
130 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
131 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
132 """
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
133 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
134 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
135 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
136 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
137 # 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
138 # 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
139 # \\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
140 # directory ^^^^^^^^^^^^^^^
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
141 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
142 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
143 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
144 ##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
145 return ("", p)
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
146 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
147 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
148 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
149 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
150 return '', p
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
151
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
152
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 # 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
154 # 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
155 # 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
156 # 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
157
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 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
159 """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
160
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
161 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
162 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
163
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
164 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
165 # 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
166 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
167 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
168 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
169 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
170 # 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
171 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
172 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
173 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
174 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
175 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
176
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
177
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 # 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
179 # 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
180 # 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
181 # 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
182
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 def splitext(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
184 """Split the extension from 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
185
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
186 Extension is everything from the last dot to the end.
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
187 Return (root, ext), either part may be empty."""
26627
7babdaea7d19 Patch #536661: Improve performance of splitext. Add test_macpath.
Martin v. Löwis <martin@v.loewis.de>
parents: 25676
diff changeset
188
7babdaea7d19 Patch #536661: Improve performance of splitext. Add test_macpath.
Martin v. Löwis <martin@v.loewis.de>
parents: 25676
diff changeset
189 i = p.rfind('.')
7babdaea7d19 Patch #536661: Improve performance of splitext. Add test_macpath.
Martin v. Löwis <martin@v.loewis.de>
parents: 25676
diff changeset
190 if i<=max(p.rfind('/'), p.rfind('\\')):
7babdaea7d19 Patch #536661: Improve performance of splitext. Add test_macpath.
Martin v. Löwis <martin@v.loewis.de>
parents: 25676
diff changeset
191 return p, ''
7babdaea7d19 Patch #536661: Improve performance of splitext. Add test_macpath.
Martin v. Löwis <martin@v.loewis.de>
parents: 25676
diff changeset
192 else:
7babdaea7d19 Patch #536661: Improve performance of splitext. Add test_macpath.
Martin v. Löwis <martin@v.loewis.de>
parents: 25676
diff changeset
193 return p[:i], p[i:]
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
194
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 # 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
197
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 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
199 """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
200 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
201
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 # 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
204
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 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
206 """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
207 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
208
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 # Return the longest prefix of all list elements.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
211
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 def commonprefix(m):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
213 "Given a list of pathnames, returns the longest common leading component"
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
214 if not m: return ''
35308
d533f08a9dfa patch [ 1105730 ] Faster commonprefix in macpath, ntpath, etc.
Georg Brandl <georg@python.org>
parents: 33332
diff changeset
215 s1 = min(m)
d533f08a9dfa patch [ 1105730 ] Faster commonprefix in macpath, ntpath, etc.
Georg Brandl <georg@python.org>
parents: 33332
diff changeset
216 s2 = max(m)
d533f08a9dfa patch [ 1105730 ] Faster commonprefix in macpath, ntpath, etc.
Georg Brandl <georg@python.org>
parents: 33332
diff changeset
217 n = min(len(s1), len(s2))
d533f08a9dfa patch [ 1105730 ] Faster commonprefix in macpath, ntpath, etc.
Georg Brandl <georg@python.org>
parents: 33332
diff changeset
218 for i in xrange(n):
d533f08a9dfa patch [ 1105730 ] Faster commonprefix in macpath, ntpath, etc.
Georg Brandl <georg@python.org>
parents: 33332
diff changeset
219 if s1[i] != s2[i]:
d533f08a9dfa patch [ 1105730 ] Faster commonprefix in macpath, ntpath, etc.
Georg Brandl <georg@python.org>
parents: 33332
diff changeset
220 return s1[:i]
d533f08a9dfa patch [ 1105730 ] Faster commonprefix in macpath, ntpath, etc.
Georg Brandl <georg@python.org>
parents: 33332
diff changeset
221 return s1[:n]
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
222
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
223
8746
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
224 # Get size, mtime, atime of files.
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
225
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
226 def getsize(filename):
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
227 """Return the size of a file, reported by os.stat()"""
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23620
diff changeset
228 return os.stat(filename).st_size
8746
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
229
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
230 def getmtime(filename):
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
231 """Return the last modification time of a file, reported by os.stat()"""
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23620
diff changeset
232 return os.stat(filename).st_mtime
8746
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
233
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
234 def getatime(filename):
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
235 """Return the last access time of a file, reported by os.stat()"""
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23620
diff changeset
236 return os.stat(filename).st_atime
8746
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
237
26946
acdb39b714b1 Patch #658927: Add getctime to os.path.
Martin v. Löwis <martin@v.loewis.de>
parents: 26627
diff changeset
238 def getctime(filename):
acdb39b714b1 Patch #658927: Add getctime to os.path.
Martin v. Löwis <martin@v.loewis.de>
parents: 26627
diff changeset
239 """Return the creation time of a file, reported by os.stat()."""
acdb39b714b1 Patch #658927: Add getctime to os.path.
Martin v. Löwis <martin@v.loewis.de>
parents: 26627
diff changeset
240 return os.stat(filename).st_ctime
8746
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
241
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 # 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
243 # 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
244
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 def islink(path):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
246 """Test for symbolic link. On WindowsNT/95 always returns false"""
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22830
diff changeset
247 return False
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
248
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
249
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 # Does a path exist?
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
251
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 def exists(path):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
253 """Test whether a path exists"""
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
254 try:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
255 st = os.stat(path)
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
256 except os.error:
22830
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 21743
diff changeset
257 return False
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 21743
diff changeset
258 return True
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
259
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
260 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
261
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
262
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 # Is a path a dos directory?
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 # This follows symbolic links, so both islink() and isdir() can be true
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 # for the same path.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
266
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 def isdir(path):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
268 """Test whether a path is a directory"""
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
269 try:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
270 st = os.stat(path)
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
271 except os.error:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22830
diff changeset
272 return False
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23620
diff changeset
273 return stat.S_ISDIR(st.st_mode)
1561
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
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 # Is a path a regular file?
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 # This follows symbolic links, so both islink() and isdir() can be true
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 # for the same path.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
279
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 def isfile(path):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
281 """Test whether a path is a regular file"""
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
282 try:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
283 st = os.stat(path)
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
284 except os.error:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22830
diff changeset
285 return False
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23620
diff changeset
286 return stat.S_ISREG(st.st_mode)
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
287
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
288
10523
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
289 # 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
290 # 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
291
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 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
293 """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
294 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
295 if unc:
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
296 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
297 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
298 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
299
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
300
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 # Directory tree walk.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 # 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
303 # '.' 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
304 # 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
305 # 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
306 # 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
307 # 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
308
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 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
310 """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
311
20361
c408333d37f0 SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents: 19934
diff changeset
312 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
313 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
314 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
315 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
316 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
317 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
318 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
319 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
320 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
321 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
322 statistics. Passing None for arg is common."""
c408333d37f0 SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents: 19934
diff changeset
323
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
324 try:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
325 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
326 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
327 return
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
328 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
329 exceptions = ('.', '..')
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
330 for name in names:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
331 if name not in exceptions:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
332 name = join(top, name)
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
333 if isdir(name):
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
334 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
335
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
336
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 # 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
338 # '~' 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
339 # 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
340 # 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
341 # 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
342 # 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
343 # (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
344 # variable expansion.)
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
345
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 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
347 """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
348
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
349 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
350 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
351 return path
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
352 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
353 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
354 i = 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
355 if i == 1:
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 22867
diff changeset
356 if 'HOME' 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
357 userhome = os.environ['HOME']
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 22867
diff changeset
358 elif not 'HOMEPATH' 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
359 return path
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
360 else:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
361 try:
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
362 drive = os.environ['HOMEDRIVE']
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
363 except KeyError:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
364 drive = ''
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
365 userhome = join(drive, os.environ['HOMEPATH'])
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
366 else:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
367 return path
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
368 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
369
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
370
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 # 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
372 # 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
373 # - 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
374 # - no escape character, except for '$$' which is translated into '$'
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
375 # - ${varname} is accepted.
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
376 # - varnames can be made out of letters, digits and the character '_'
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 # 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
378 # XXX except '^|<>='.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
379
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
380 def expandvars(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
381 """Expand shell variables of form $var and ${var}.
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
382
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
383 Unknown variables are left unchanged."""
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
384 if '$' not in path:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
385 return path
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
386 import string
18781
01ae962df885 Use string.ascii_letters instead of string.letters (SF bug #226706).
Fred Drake <fdrake@acm.org>
parents: 18742
diff changeset
387 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
388 res = ''
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
389 index = 0
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
390 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
391 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
392 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
393 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
394 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
395 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
396 try:
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
397 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
398 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
399 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
400 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
401 index = pathlen - 1
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
402 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
403 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
404 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
405 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
406 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
407 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
408 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
409 try:
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
410 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
411 var = path[:index]
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 22867
diff changeset
412 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
413 res = res + os.environ[var]
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
414 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
415 res = res + path
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
416 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
417 else:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
418 var = ''
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
419 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
420 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
421 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
422 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
423 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
424 c = path[index:index + 1]
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 22867
diff changeset
425 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
426 res = res + os.environ[var]
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
427 if c != '':
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
428 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
429 else:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
430 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
431 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
432 return res
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
433
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
434
19628
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
435 # 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
436 # 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
437 # 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
438
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 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
440 """Normalize path, eliminating double slashes, etc."""
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
441 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
442 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
443 # 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
444 # 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
445 # 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
446 # 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
447 # 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
448 # 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
449 # 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
450 # 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
451 # 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
452 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
453 # 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
454 while 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
455 prefix = prefix + "\\"
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
456 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
457 else:
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
458 # 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
459 if path.startswith("\\"):
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
460 prefix = prefix + "\\"
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
461 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
462 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
463 i = 0
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
464 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
465 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
466 del comps[i]
19628
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
467 elif comps[i] == '..':
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
468 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
469 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
470 i -= 1
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
471 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
472 del comps[i]
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
473 else:
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
474 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
475 else:
19628
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
476 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
477 # 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
478 if not prefix and not comps:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
479 comps.append('.')
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
480 return prefix + "\\".join(comps)
10018
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
481
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
482
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
483 # Return an absolute path.
37908
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
484 try:
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
485 from nt import _getfullpathname
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
486
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
487 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
488 def abspath(path):
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
489 """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
490 if not isabs(path):
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
491 path = join(os.getcwd(), path)
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
492 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
493
37908
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
494 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
495 def abspath(path):
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
496 """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
497
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
498 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
499 try:
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
500 path = _getfullpathname(path)
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
501 except WindowsError:
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
502 pass # Bad path - return unchanged.
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
503 else:
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
504 path = os.getcwd()
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
505 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
506
f2f25a738f7f SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents: 19628
diff changeset
507 # 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
508 realpath = abspath
25666
4a825b902310 Add os.path.supports_unicode_filenames for all platforms,
Mark Hammond <mhammond@skippinet.com.au>
parents: 23625
diff changeset
509 # 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
510 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
511 sys.getwindowsversion()[3] >= 2)