annotate Lib/ntpath.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents 288c1ffd606e
children 3b3ce964adc9 a9dd5e6d6805
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
13581
e871bd05c6a7 forgot to change copy.copy(m) to m[:]
Skip Montanaro <skip@pobox.com>
parents: 13577
diff changeset
10
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
11 __all__ = ["normcase","isabs","join","splitdrive","split","splitext",
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
12 "basename","dirname","commonprefix","getsize","getmtime",
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
13 "getatime","islink","exists","isdir","isfile","ismount",
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
14 "walk","expanduser","expandvars","normpath","abspath","splitunc"]
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
5976
5b2c595428fa Rewrite normcase() using string.translate...
Guido van Rossum <guido@python.org>
parents: 5703
diff changeset
16 # 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
17 # 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
18 # (this is done by normpath).
5976
5b2c595428fa Rewrite normcase() using string.translate...
Guido van Rossum <guido@python.org>
parents: 5703
diff changeset
19
7351
1baca5d2ba57 (1) Change normpath() to *not* also call normcase().
Guido van Rossum <guido@python.org>
parents: 7123
diff changeset
20 def normcase(s):
1baca5d2ba57 (1) Change normpath() to *not* also call normcase().
Guido van Rossum <guido@python.org>
parents: 7123
diff changeset
21 """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
22
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
23 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
24 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
25
5703
03dd1ded5cb3 Support $HOME in expanduser().
Guido van Rossum <guido@python.org>
parents: 5136
diff changeset
26
11804
57788da16488 Typo in a comment: "wheter" --> "whether"
Fred Drake <fdrake@acm.org>
parents: 11693
diff changeset
27 # 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
28 # 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
29 # 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
30 # 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
31 # 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
32
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 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
34 """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
35 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
36 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
37
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
5703
03dd1ded5cb3 Support $HOME in expanduser().
Guido van Rossum <guido@python.org>
parents: 5136
diff changeset
39 # Join two (or more) paths.
03dd1ded5cb3 Support $HOME in expanduser().
Guido van Rossum <guido@python.org>
parents: 5136
diff changeset
40
5133
666152b77027 join(): join one or more path components
Barry Warsaw <barry@python.org>
parents: 5027
diff changeset
41 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
42 """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
43 path = a
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
44 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
45 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
46 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
47 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
48
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
49 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
50 # 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
51 # 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
52 # 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
53 # 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
54 # 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
55 # 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
56 # 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
57 # 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
58 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
59 # 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
60 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
61
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
62 # 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
63 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
64 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
65 # 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
66 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
67
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
68 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
69 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
70 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
71 # 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
72 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
73 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
74 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
75 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
76 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
77 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
78 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
79 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
80 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
81 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
82 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
83 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
84 path += "\\" + b
20887
0c17515b05db SF bug 478425: Change in os.path.join (ntpath.py)
Tim Peters <tim.peters@gmail.com>
parents: 20361
diff changeset
85 else:
0c17515b05db SF bug 478425: Change in os.path.join (ntpath.py)
Tim Peters <tim.peters@gmail.com>
parents: 20361
diff changeset
86 # 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
87 # 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
88 # ('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
89 # this case.
0c17515b05db SF bug 478425: Change in os.path.join (ntpath.py)
Tim Peters <tim.peters@gmail.com>
parents: 20361
diff changeset
90 path += '\\'
18742
920e0361dd6e SF bug #44271: os.path.expanduser problem w/o HOME set.
Tim Peters <tim.peters@gmail.com>
parents: 17836
diff changeset
91
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
92 return path
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
93
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
94
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 # 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
96 # 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
97 # 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
98 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
99 """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
100 "(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
101 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
102 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
103 return '', p
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
104
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
105
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
106 # Parse UNC paths
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
107 def splitunc(p):
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
108 """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
109
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
110 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
111 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
112 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
113 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
114 """
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
115 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
116 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
117 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
118 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
119 # 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
120 # 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
121 # \\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
122 # directory ^^^^^^^^^^^^^^^
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
123 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
124 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
125 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
126 ##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
127 return ("", p)
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
128 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
129 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
130 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
131 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
132 return '', p
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
133
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
134
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 # 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
136 # 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
137 # 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
138 # 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
139
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 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
141 """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
142
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
143 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
144 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
145
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
146 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
147 # 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
148 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
149 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
150 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
151 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
152 # 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
153 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
154 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
155 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
156 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
157 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
158
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
159
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 # 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
161 # 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
162 # 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
163 # 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
164
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 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
166 """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
167
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
168 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
169 Return (root, ext), 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
170 root, ext = '', ''
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
171 for c in p:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
172 if c in ['/','\\']:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
173 root, ext = root + ext + c, ''
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
174 elif c == '.':
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
175 if ext:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
176 root, ext = root + ext, c
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
177 else:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
178 ext = c
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
179 elif ext:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
180 ext = ext + c
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
181 else:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
182 root = root + c
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
183 return root, ext
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
184
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
185
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 # 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
187
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 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
189 """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
190 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
191
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 # 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
194
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 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
196 """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
197 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
198
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 # 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
201
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 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
203 "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
204 if not m: return ''
14121
73c564e4a0f3 revert semantics of commonprefix to work character-by-character
Skip Montanaro <skip@pobox.com>
parents: 13964
diff changeset
205 prefix = m[0]
73c564e4a0f3 revert semantics of commonprefix to work character-by-character
Skip Montanaro <skip@pobox.com>
parents: 13964
diff changeset
206 for item in m:
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
207 for i in range(len(prefix)):
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 14910
diff changeset
208 if prefix[:i+1] != item[: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
209 prefix = prefix[:i]
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
210 if i == 0: return ''
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
211 break
14121
73c564e4a0f3 revert semantics of commonprefix to work character-by-character
Skip Montanaro <skip@pobox.com>
parents: 13964
diff changeset
212 return prefix
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
213
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
214
8746
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
215 # Get size, mtime, atime of files.
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
216
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
217 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
218 """Return the size of a file, reported by os.stat()"""
8746
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
219 st = os.stat(filename)
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
220 return st[stat.ST_SIZE]
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
221
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
222 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
223 """Return the last modification time of a file, reported by os.stat()"""
8746
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
224 st = os.stat(filename)
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
225 return st[stat.ST_MTIME]
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
226
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
227 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
228 """Return the last access time of a file, reported by os.stat()"""
8746
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
229 st = os.stat(filename)
13286
7295f360264e Fix bug #345 reported by David Bolen <db3l@fitlinxx.com>:
Fred Drake <fdrake@acm.org>
parents: 11804
diff changeset
230 return st[stat.ST_ATIME]
8746
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
231
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
232
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 # 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
234 # 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
235
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 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
237 """Test for symbolic link. On WindowsNT/95 always returns false"""
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
238 return 0
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
239
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
240
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 # 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
242 # This is false for dangling symbolic links.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
243
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 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
245 """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
246 try:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
247 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
248 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
249 return 0
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
250 return 1
1561
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
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 # 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
254 # 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
255 # 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
256
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 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
258 """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
259 try:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
260 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
261 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
262 return 0
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
263 return stat.S_ISDIR(st[stat.ST_MODE])
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 # 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
267 # 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
268 # 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
269
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 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
271 """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
272 try:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
273 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
274 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
275 return 0
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
276 return stat.S_ISREG(st[stat.ST_MODE])
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
277
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
278
10523
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
279 # 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
280 # 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
281
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 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
283 """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
284 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
285 if unc:
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
286 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
287 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
288 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
289
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
290
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 # Directory tree walk.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 # 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
293 # '.' 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
294 # dirname is the name of the directory and filenames is the list
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 # files files (and subdirectories etc.) in the directory.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 # 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
297 # 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
298
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 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
300 """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
301
20361
c408333d37f0 SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents: 19934
diff changeset
302 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
303 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
304 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
305 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
306 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
307 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
308 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
309 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
310 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
311 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
312 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
313
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
314 try:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
315 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
316 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
317 return
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
318 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
319 exceptions = ('.', '..')
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
320 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
321 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
322 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
323 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
324 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
325
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
326
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 # 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
328 # '~' 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
329 # 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
330 # 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
331 # 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
332 # 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
333 # (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
334 # variable expansion.)
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 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
337 """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
338
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
339 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
340 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
341 return path
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
342 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
343 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
344 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
345 if i == 1:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
346 if os.environ.has_key('HOME'):
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
347 userhome = os.environ['HOME']
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
348 elif not os.environ.has_key('HOMEPATH'):
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
349 return path
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
350 else:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
351 try:
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
352 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
353 except KeyError:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
354 drive = ''
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
355 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
356 else:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
357 return path
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
358 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
359
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
360
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 # 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
362 # 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
363 # - 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
364 # - 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
365 # - ${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
366 # - 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
367 # 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
368 # XXX except '^|<>='.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
369
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
370 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
371 """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
372
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
373 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
374 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
375 return path
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
376 import string
18781
01ae962df885 Use string.ascii_letters instead of string.letters (SF bug #226706).
Fred Drake <fdrake@acm.org>
parents: 18742
diff changeset
377 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
378 res = ''
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
379 index = 0
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
380 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
381 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
382 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
383 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
384 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
385 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
386 try:
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
387 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
388 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
389 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
390 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
391 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
392 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
393 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
394 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
395 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
396 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
397 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
398 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
399 try:
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
400 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
401 var = path[:index]
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
402 if os.environ.has_key(var):
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
403 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
404 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
405 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
406 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
407 else:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
408 var = ''
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
409 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
410 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
411 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
412 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
413 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
414 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
415 if os.environ.has_key(var):
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
416 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
417 if c != '':
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
418 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
419 else:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
420 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
421 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
422 return res
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
423
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
424
19628
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
425 # 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
426 # 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
427 # 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
428
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
429 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
430 """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
431 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
432 prefix, path = splitdrive(path)
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
433 while path[:1] == "\\":
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
434 prefix = prefix + "\\"
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
435 path = path[1:]
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
436 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
437 i = 0
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
438 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
439 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
440 del comps[i]
19628
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
441 elif comps[i] == '..':
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
442 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
443 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
444 i -= 1
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
445 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
446 del comps[i]
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
447 else:
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
448 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
449 else:
19628
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
450 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
451 # 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
452 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
453 comps.append('.')
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
454 return prefix + "\\".join(comps)
10018
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
455
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
456
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
457 # Return an absolute path.
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
458 def abspath(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
459 """Return the absolute version of a path"""
25609
288c1ffd606e Backport 1.45:
Raymond Hettinger <python@rcn.com>
parents: 20887
diff changeset
460 try:
288c1ffd606e Backport 1.45:
Raymond Hettinger <python@rcn.com>
parents: 20887
diff changeset
461 from nt import _getfullpathname
288c1ffd606e Backport 1.45:
Raymond Hettinger <python@rcn.com>
parents: 20887
diff changeset
462 except ImportError: # Not running on Windows - mock up something sensible.
288c1ffd606e Backport 1.45:
Raymond Hettinger <python@rcn.com>
parents: 20887
diff changeset
463 global abspath
288c1ffd606e Backport 1.45:
Raymond Hettinger <python@rcn.com>
parents: 20887
diff changeset
464 def _abspath(path):
288c1ffd606e Backport 1.45:
Raymond Hettinger <python@rcn.com>
parents: 20887
diff changeset
465 if not isabs(path):
288c1ffd606e Backport 1.45:
Raymond Hettinger <python@rcn.com>
parents: 20887
diff changeset
466 path = join(os.getcwd(), path)
288c1ffd606e Backport 1.45:
Raymond Hettinger <python@rcn.com>
parents: 20887
diff changeset
467 return normpath(path)
288c1ffd606e Backport 1.45:
Raymond Hettinger <python@rcn.com>
parents: 20887
diff changeset
468 abspath = _abspath
288c1ffd606e Backport 1.45:
Raymond Hettinger <python@rcn.com>
parents: 20887
diff changeset
469 return _abspath(path)
288c1ffd606e Backport 1.45:
Raymond Hettinger <python@rcn.com>
parents: 20887
diff changeset
470
13964
a256c101bf27 Fix for Bug #110673: os.abspatth() now always returns os.getcwd() on Windows, if an empty path is specified. It previously did not if an empty path was delegated to win32api.GetFullPathName())
Mark Hammond <mhammond@skippinet.com.au>
parents: 13632
diff changeset
471 if path: # Empty path must return current working directory.
a256c101bf27 Fix for Bug #110673: os.abspatth() now always returns os.getcwd() on Windows, if an empty path is specified. It previously did not if an empty path was delegated to win32api.GetFullPathName())
Mark Hammond <mhammond@skippinet.com.au>
parents: 13632
diff changeset
472 try:
17817
f7c385226f65 Add support for Windows using "mbcs" as the default Unicode encoding when dealing with the file system. As discussed on python-dev and in patch 410465.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16460
diff changeset
473 path = _getfullpathname(path)
f7c385226f65 Add support for Windows using "mbcs" as the default Unicode encoding when dealing with the file system. As discussed on python-dev and in patch 410465.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16460
diff changeset
474 except WindowsError:
17836
ad500b52673c abspath(): Fix inconsistent indentation.
Fred Drake <fdrake@acm.org>
parents: 17817
diff changeset
475 pass # Bad path - return unchanged.
13964
a256c101bf27 Fix for Bug #110673: os.abspatth() now always returns os.getcwd() on Windows, if an empty path is specified. It previously did not if an empty path was delegated to win32api.GetFullPathName())
Mark Hammond <mhammond@skippinet.com.au>
parents: 13632
diff changeset
476 else:
a256c101bf27 Fix for Bug #110673: os.abspatth() now always returns os.getcwd() on Windows, if an empty path is specified. It previously did not if an empty path was delegated to win32api.GetFullPathName())
Mark Hammond <mhammond@skippinet.com.au>
parents: 13632
diff changeset
477 path = os.getcwd()
11498
756351707cac In abspath(), always use normpath(), even when win32api is available
Guido van Rossum <guido@python.org>
parents: 11428
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