annotate Lib/macpath.py @ 27998:e27f58a50737 legacy-trunk

SF bug #690377: Dud footnote reference in distutils docs
author Raymond Hettinger <python@rcn.com>
date Fri, 21 Feb 2003 02:47:09 +0000
parents cf1efca82b18
children 2d59ca29f384
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
1 """Pathname and path-related operations for the Macintosh."""
103
1229ba124e0b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
7525
88992696b9e8 Instead of 'import mac', use 'import os' -- this way, the path syntax
Guido van Rossum <guido@python.org>
parents: 5576
diff changeset
3 import os
579
6884346c04a0 Added 'normcase' function.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
4 from stat import *
103
1229ba124e0b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
16198
32755b456369 a few more modules get __all__
Skip Montanaro <skip@pobox.com>
parents: 15543
diff changeset
6 __all__ = ["normcase","isabs","join","splitdrive","split","splitext",
32755b456369 a few more modules get __all__
Skip Montanaro <skip@pobox.com>
parents: 15543
diff changeset
7 "basename","dirname","commonprefix","getsize","getmtime",
26946
acdb39b714b1 Patch #658927: Add getctime to os.path.
Martin v. Löwis <martin@v.loewis.de>
parents: 26627
diff changeset
8 "getatime","getctime", "islink","exists","isdir","isfile",
25666
4a825b902310 Add os.path.supports_unicode_filenames for all platforms,
Mark Hammond <mhammond@skippinet.com.au>
parents: 23625
diff changeset
9 "walk","expanduser","expandvars","normpath","abspath",
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27507
diff changeset
10 "curdir","pardir","sep","pathsep","defpath","altsep","extsep",
27056
94b9cf4274de Fix SF #659228, 'realpath' function missing from os.path
Neal Norwitz <nnorwitz@gmail.com>
parents: 26946
diff changeset
11 "realpath","supports_unicode_filenames"]
579
6884346c04a0 Added 'normcase' function.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
12
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27507
diff changeset
13 # strings representing various path-related bits and pieces
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27507
diff changeset
14 curdir = ':'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27507
diff changeset
15 pardir = '::'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27507
diff changeset
16 extsep = '.'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27507
diff changeset
17 sep = ':'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27507
diff changeset
18 pathsep = '\n'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27507
diff changeset
19 defpath = ':'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27507
diff changeset
20 altsep = None
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27507
diff changeset
21
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14128
diff changeset
22 # Normalize the case of a pathname. Dummy in Posix, but <s>.lower() here.
579
6884346c04a0 Added 'normcase' function.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
23
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14128
diff changeset
24 def normcase(path):
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14128
diff changeset
25 return path.lower()
105
b4d5fdcbf83c Added explanatory comments.
Guido van Rossum <guido@python.org>
parents: 103
diff changeset
26
103
1229ba124e0b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
28 def isabs(s):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
29 """Return true if a path is absolute.
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
30 On the Mac, relative paths begin with a colon,
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
31 but as a special case, paths with no colons at all are also relative.
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
32 Anything else is absolute (the string up to the first colon is the
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
33 volume name)."""
103
1229ba124e0b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 14910
diff changeset
35 return ':' in s and s[0] != ':'
103
1229ba124e0b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
105
b4d5fdcbf83c Added explanatory comments.
Guido van Rossum <guido@python.org>
parents: 103
diff changeset
37
5133
666152b77027 join(): join one or more path components
Barry Warsaw <barry@python.org>
parents: 3793
diff changeset
38 def join(s, *p):
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
39 path = s
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
40 for t in p:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
41 if (not s) or isabs(t):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
42 path = t
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
43 continue
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
44 if t[:1] == ':':
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
45 t = t[1:]
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
46 if ':' not in path:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
47 path = ':' + path
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 14910
diff changeset
48 if path[-1:] != ':':
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
49 path = path + ':'
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
50 path = path + t
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
51 return path
103
1229ba124e0b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
105
b4d5fdcbf83c Added explanatory comments.
Guido van Rossum <guido@python.org>
parents: 103
diff changeset
53
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
54 def split(s):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
55 """Split a pathname into two parts: the directory leading up to the final
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
56 bit, and the basename (the filename, without colons, in that directory).
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
57 The result (s, t) is such that join(s, t) yields the original argument."""
105
b4d5fdcbf83c Added explanatory comments.
Guido van Rossum <guido@python.org>
parents: 103
diff changeset
58
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
59 if ':' not in s: return '', s
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
60 colon = 0
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
61 for i in range(len(s)):
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14128
diff changeset
62 if s[i] == ':': colon = i + 1
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
63 path, file = s[:colon-1], s[colon:]
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
64 if path and not ':' in path:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
65 path = path + ':'
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
66 return path, file
105
b4d5fdcbf83c Added explanatory comments.
Guido van Rossum <guido@python.org>
parents: 103
diff changeset
67
b4d5fdcbf83c Added explanatory comments.
Guido van Rossum <guido@python.org>
parents: 103
diff changeset
68
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
69 def splitext(p):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
70 """Split a path into root and extension.
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
71 The extension is everything starting at the last dot in the last
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
72 pathname component; the root is everything before that.
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
73 It is always true that root + ext == p."""
3793
ca02d9278180 Added splitext()
Guido van Rossum <guido@python.org>
parents: 3299
diff changeset
74
26627
7babdaea7d19 Patch #536661: Improve performance of splitext. Add test_macpath.
Martin v. Löwis <martin@v.loewis.de>
parents: 25666
diff changeset
75 i = p.rfind('.')
7babdaea7d19 Patch #536661: Improve performance of splitext. Add test_macpath.
Martin v. Löwis <martin@v.loewis.de>
parents: 25666
diff changeset
76 if i<=p.rfind(':'):
7babdaea7d19 Patch #536661: Improve performance of splitext. Add test_macpath.
Martin v. Löwis <martin@v.loewis.de>
parents: 25666
diff changeset
77 return p, ''
7babdaea7d19 Patch #536661: Improve performance of splitext. Add test_macpath.
Martin v. Löwis <martin@v.loewis.de>
parents: 25666
diff changeset
78 else:
7babdaea7d19 Patch #536661: Improve performance of splitext. Add test_macpath.
Martin v. Löwis <martin@v.loewis.de>
parents: 25666
diff changeset
79 return p[:i], p[i:]
3793
ca02d9278180 Added splitext()
Guido van Rossum <guido@python.org>
parents: 3299
diff changeset
80
ca02d9278180 Added splitext()
Guido van Rossum <guido@python.org>
parents: 3299
diff changeset
81
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
82 def splitdrive(p):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
83 """Split a pathname into a drive specification and the rest of the
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
84 path. Useful on DOS/Windows/NT; on the Mac, the drive is always
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
85 empty (don't use the volume name -- it doesn't have the same
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
86 syntactic and semantic oddities as DOS drive letters, such as there
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
87 being a separate current directory per drive)."""
2947
a980d076052d added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents: 2910
diff changeset
88
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
89 return '', p
2947
a980d076052d added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents: 2910
diff changeset
90
a980d076052d added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents: 2910
diff changeset
91
1086
db817f3a1c65 * change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents: 770
diff changeset
92 # Short interfaces to split()
db817f3a1c65 * change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents: 770
diff changeset
93
db817f3a1c65 * change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents: 770
diff changeset
94 def dirname(s): return split(s)[0]
db817f3a1c65 * change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents: 770
diff changeset
95 def basename(s): return split(s)[1]
db817f3a1c65 * change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents: 770
diff changeset
96
27257
3fbffd736d9d Added ismount().
Jack Jansen <jack.jansen@cwi.nl>
parents: 27056
diff changeset
97 def ismount(s):
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27257
diff changeset
98 if not isabs(s):
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27257
diff changeset
99 return False
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27257
diff changeset
100 components = split(s)
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27257
diff changeset
101 return len(components) == 2 and components[1] == ''
1086
db817f3a1c65 * change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents: 770
diff changeset
102
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
103 def isdir(s):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
104 """Return true if the pathname refers to an existing directory."""
105
b4d5fdcbf83c Added explanatory comments.
Guido van Rossum <guido@python.org>
parents: 103
diff changeset
105
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
106 try:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
107 st = os.stat(s)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
108 except os.error:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
109 return 0
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
110 return S_ISDIR(st.st_mode)
103
1229ba124e0b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
111
105
b4d5fdcbf83c Added explanatory comments.
Guido van Rossum <guido@python.org>
parents: 103
diff changeset
112
8746
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 7525
diff changeset
113 # Get size, mtime, atime of files.
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 7525
diff changeset
114
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 7525
diff changeset
115 def getsize(filename):
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 7525
diff changeset
116 """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: 23521
diff changeset
117 return os.stat(filename).st_size
8746
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 7525
diff changeset
118
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 7525
diff changeset
119 def getmtime(filename):
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 7525
diff changeset
120 """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: 23521
diff changeset
121 return os.stat(filename).st_mtime
8746
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 7525
diff changeset
122
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 7525
diff changeset
123 def getatime(filename):
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 7525
diff changeset
124 """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: 23521
diff changeset
125 return os.stat(filename).st_atime
8746
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 7525
diff changeset
126
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 7525
diff changeset
127
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
128 def islink(s):
23220
bd08c6955e9b Fixes based on ideas from Christopher Smith:
Jack Jansen <jack.jansen@cwi.nl>
parents: 22867
diff changeset
129 """Return true if the pathname refers to a symbolic link."""
2260
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 1810
diff changeset
130
23220
bd08c6955e9b Fixes based on ideas from Christopher Smith:
Jack Jansen <jack.jansen@cwi.nl>
parents: 22867
diff changeset
131 try:
23237
5d135e414c76 whitespace fixup. test__all__ and test_sundry were failing
Anthony Baxter <anthonybaxter@gmail.com>
parents: 23220
diff changeset
132 import macfs
23220
bd08c6955e9b Fixes based on ideas from Christopher Smith:
Jack Jansen <jack.jansen@cwi.nl>
parents: 22867
diff changeset
133 return macfs.ResolveAliasFile(s)[2]
bd08c6955e9b Fixes based on ideas from Christopher Smith:
Jack Jansen <jack.jansen@cwi.nl>
parents: 22867
diff changeset
134 except:
bd08c6955e9b Fixes based on ideas from Christopher Smith:
Jack Jansen <jack.jansen@cwi.nl>
parents: 22867
diff changeset
135 return False
2260
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 1810
diff changeset
136
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 1810
diff changeset
137
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
138 def isfile(s):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
139 """Return true if the pathname refers to an existing regular file."""
105
b4d5fdcbf83c Added explanatory comments.
Guido van Rossum <guido@python.org>
parents: 103
diff changeset
140
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
141 try:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
142 st = os.stat(s)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
143 except os.error:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22830
diff changeset
144 return False
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
145 return S_ISREG(st.st_mode)
103
1229ba124e0b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
146
26946
acdb39b714b1 Patch #658927: Add getctime to os.path.
Martin v. Löwis <martin@v.loewis.de>
parents: 26627
diff changeset
147 def getctime(filename):
acdb39b714b1 Patch #658927: Add getctime to os.path.
Martin v. Löwis <martin@v.loewis.de>
parents: 26627
diff changeset
148 """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
149 return os.stat(filename).st_ctime
105
b4d5fdcbf83c Added explanatory comments.
Guido van Rossum <guido@python.org>
parents: 103
diff changeset
150
103
1229ba124e0b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 def exists(s):
22830
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 22704
diff changeset
152 """Return True if the pathname refers to an existing file or directory."""
1086
db817f3a1c65 * change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents: 770
diff changeset
153
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
154 try:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
155 st = os.stat(s)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
156 except os.error:
22830
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 22704
diff changeset
157 return False
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 22704
diff changeset
158 return True
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
159
14128
039583c090c4 Restored commonprefix() semantics.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13876
diff changeset
160 # Return the longest prefix of all list elements.
039583c090c4 Restored commonprefix() semantics.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13876
diff changeset
161
039583c090c4 Restored commonprefix() semantics.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13876
diff changeset
162 def commonprefix(m):
039583c090c4 Restored commonprefix() semantics.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13876
diff changeset
163 "Given a list of pathnames, returns the longest common leading component"
039583c090c4 Restored commonprefix() semantics.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13876
diff changeset
164 if not m: return ''
039583c090c4 Restored commonprefix() semantics.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13876
diff changeset
165 prefix = m[0]
039583c090c4 Restored commonprefix() semantics.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13876
diff changeset
166 for item in m:
039583c090c4 Restored commonprefix() semantics.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13876
diff changeset
167 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
168 if prefix[:i+1] != item[:i+1]:
14128
039583c090c4 Restored commonprefix() semantics.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13876
diff changeset
169 prefix = prefix[:i]
039583c090c4 Restored commonprefix() semantics.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13876
diff changeset
170 if i == 0: return ''
039583c090c4 Restored commonprefix() semantics.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13876
diff changeset
171 break
039583c090c4 Restored commonprefix() semantics.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13876
diff changeset
172 return prefix
3299
47763ac44c48 Added (dummy) expanduser and expandvar methods
Jack Jansen <jack.jansen@cwi.nl>
parents: 2947
diff changeset
173
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
174 def expandvars(path):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
175 """Dummy to retain interface-compatibility with other operating systems."""
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
176 return path
1086
db817f3a1c65 * change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents: 770
diff changeset
177
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
178
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
179 def expanduser(path):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
180 """Dummy to retain interface-compatibility with other operating systems."""
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
181 return path
2947
a980d076052d added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents: 2910
diff changeset
182
22704
18dccdf2c46d Use docstrings for exception classes
Neal Norwitz <nnorwitz@gmail.com>
parents: 22702
diff changeset
183 class norm_error(Exception):
18dccdf2c46d Use docstrings for exception classes
Neal Norwitz <nnorwitz@gmail.com>
parents: 22702
diff changeset
184 """Path cannot be normalized"""
1086
db817f3a1c65 * change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents: 770
diff changeset
185
db817f3a1c65 * change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents: 770
diff changeset
186 def normpath(s):
13876
1c00fea9ae40 Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents: 13577
diff changeset
187 """Normalize a pathname. Will return the same result for
1c00fea9ae40 Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents: 13577
diff changeset
188 equivalent paths."""
1c00fea9ae40 Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents: 13577
diff changeset
189
1c00fea9ae40 Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents: 13577
diff changeset
190 if ":" not in s:
1c00fea9ae40 Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents: 13577
diff changeset
191 return ":"+s
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
192
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14128
diff changeset
193 comps = s.split(":")
13876
1c00fea9ae40 Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents: 13577
diff changeset
194 i = 1
1c00fea9ae40 Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents: 13577
diff changeset
195 while i < len(comps)-1:
1c00fea9ae40 Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents: 13577
diff changeset
196 if comps[i] == "" and comps[i-1] != "":
1c00fea9ae40 Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents: 13577
diff changeset
197 if i > 1:
1c00fea9ae40 Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents: 13577
diff changeset
198 del comps[i-1:i+1]
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14128
diff changeset
199 i = i - 1
13876
1c00fea9ae40 Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents: 13577
diff changeset
200 else:
1c00fea9ae40 Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents: 13577
diff changeset
201 # best way to handle this is to raise an exception
19080
27b4dbd428fe Typo fix (spelling mistake in error message).
Greg Ward <gward@python.net>
parents: 16198
diff changeset
202 raise norm_error, 'Cannot use :: immediately after volume name'
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
203 else:
13876
1c00fea9ae40 Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents: 13577
diff changeset
204 i = i + 1
1c00fea9ae40 Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents: 13577
diff changeset
205
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14128
diff changeset
206 s = ":".join(comps)
13876
1c00fea9ae40 Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents: 13577
diff changeset
207
1c00fea9ae40 Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents: 13577
diff changeset
208 # remove trailing ":" except for ":" and "Volume:"
1c00fea9ae40 Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents: 13577
diff changeset
209 if s[-1] == ":" and len(comps) > 2 and s != ":"*len(s):
1c00fea9ae40 Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents: 13577
diff changeset
210 s = s[:-1]
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
211 return s
2910
5a2612fb6c0e Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents: 2260
diff changeset
212
2947
a980d076052d added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents: 2910
diff changeset
213
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
214 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
215 """Directory tree walk with callback function.
c408333d37f0 SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents: 19934
diff changeset
216
c408333d37f0 SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents: 19934
diff changeset
217 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
218 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
219 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
220 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
221 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
222 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
223 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
224 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
225 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
226 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
227 statistics. Passing None for arg is common."""
2910
5a2612fb6c0e Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents: 2260
diff changeset
228
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
229 try:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
230 names = os.listdir(top)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
231 except os.error:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
232 return
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
233 func(arg, top, names)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
234 for name in names:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
235 name = join(top, name)
23220
bd08c6955e9b Fixes based on ideas from Christopher Smith:
Jack Jansen <jack.jansen@cwi.nl>
parents: 22867
diff changeset
236 if isdir(name) and not islink(name):
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
237 walk(name, func, arg)
10018
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
238
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
239
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
240 def abspath(path):
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
241 """Return an absolute path."""
10018
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
242 if not isabs(path):
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
243 path = join(os.getcwd(), path)
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
244 return normpath(path)
19934
f2f25a738f7f SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents: 19080
diff changeset
245
f2f25a738f7f SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents: 19080
diff changeset
246 # realpath is a no-op on systems without islink support
23220
bd08c6955e9b Fixes based on ideas from Christopher Smith:
Jack Jansen <jack.jansen@cwi.nl>
parents: 22867
diff changeset
247 def realpath(path):
23521
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23237
diff changeset
248 path = abspath(path)
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23237
diff changeset
249 try:
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23237
diff changeset
250 import macfs
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23237
diff changeset
251 except ImportError:
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23237
diff changeset
252 return path
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23237
diff changeset
253 if not path:
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23237
diff changeset
254 return path
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23237
diff changeset
255 components = path.split(':')
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23237
diff changeset
256 path = components[0] + ':'
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23237
diff changeset
257 for c in components[1:]:
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23237
diff changeset
258 path = join(path, c)
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23237
diff changeset
259 path = macfs.ResolveAliasFile(path)[0].as_pathname()
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23237
diff changeset
260 return path
25666
4a825b902310 Add os.path.supports_unicode_filenames for all platforms,
Mark Hammond <mhammond@skippinet.com.au>
parents: 23625
diff changeset
261
4a825b902310 Add os.path.supports_unicode_filenames for all platforms,
Mark Hammond <mhammond@skippinet.com.au>
parents: 23625
diff changeset
262 supports_unicode_filenames = False