annotate Lib/macpath.py @ 17581:c1937faabb23 2.1

Added support for optional explanation parameter to the \versionadded macro. Refactored do_cmd_versionadded() and do_cmd_versionchanged() to do most of the work in a helper function, with the do_cmd_*() wrappers just supplying a portion of the replacement text.
author Fred Drake <fdrake@acm.org>
date Wed, 18 Apr 2001 17:29:14 +0000
parents 32755b456369
children 27b4dbd428fe
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",
32755b456369 a few more modules get __all__
Skip Montanaro <skip@pobox.com>
parents: 15543
diff changeset
8 "getatime","islink","exists","isdir","isfile",
32755b456369 a few more modules get __all__
Skip Montanaro <skip@pobox.com>
parents: 15543
diff changeset
9 "walk","expanduser","expandvars","normpath","abspath"]
579
6884346c04a0 Added 'normcase' function.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
10
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14128
diff changeset
11 # 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
12
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14128
diff changeset
13 def normcase(path):
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14128
diff changeset
14 return path.lower()
105
b4d5fdcbf83c Added explanatory comments.
Guido van Rossum <guido@python.org>
parents: 103
diff changeset
15
103
1229ba124e0b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
17 def isabs(s):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
18 """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
19 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
20 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
21 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
22 volume name)."""
103
1229ba124e0b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 14910
diff changeset
24 return ':' in s and s[0] != ':'
103
1229ba124e0b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
105
b4d5fdcbf83c Added explanatory comments.
Guido van Rossum <guido@python.org>
parents: 103
diff changeset
26
5133
666152b77027 join(): join one or more path components
Barry Warsaw <barry@python.org>
parents: 3793
diff changeset
27 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
28 path = s
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
29 for t in p:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
30 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
31 path = t
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
32 continue
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
33 if t[:1] == ':':
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
34 t = t[1:]
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
35 if ':' not in path:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
36 path = ':' + path
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 14910
diff changeset
37 if path[-1:] != ':':
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
38 path = path + ':'
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
39 path = path + t
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
40 return path
103
1229ba124e0b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
105
b4d5fdcbf83c Added explanatory comments.
Guido van Rossum <guido@python.org>
parents: 103
diff changeset
42
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
43 def split(s):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
44 """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
45 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
46 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
47
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
48 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
49 colon = 0
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
50 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
51 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
52 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
53 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
54 path = path + ':'
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
55 return path, file
105
b4d5fdcbf83c Added explanatory comments.
Guido van Rossum <guido@python.org>
parents: 103
diff changeset
56
b4d5fdcbf83c Added explanatory comments.
Guido van Rossum <guido@python.org>
parents: 103
diff changeset
57
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
58 def splitext(p):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
59 """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
60 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
61 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
62 It is always true that root + ext == p."""
3793
ca02d9278180 Added splitext()
Guido van Rossum <guido@python.org>
parents: 3299
diff changeset
63
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
64 root, ext = '', ''
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
65 for c in p:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
66 if c == ':':
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
67 root, ext = root + ext + c, ''
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
68 elif c == '.':
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
69 if ext:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
70 root, ext = root + ext, c
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
71 else:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
72 ext = c
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
73 elif ext:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
74 ext = ext + c
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
75 else:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
76 root = root + c
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
77 return root, ext
3793
ca02d9278180 Added splitext()
Guido van Rossum <guido@python.org>
parents: 3299
diff changeset
78
ca02d9278180 Added splitext()
Guido van Rossum <guido@python.org>
parents: 3299
diff changeset
79
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
80 def splitdrive(p):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
81 """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
82 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
83 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
84 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
85 being a separate current directory per drive)."""
2947
a980d076052d added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents: 2910
diff changeset
86
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
87 return '', p
2947
a980d076052d added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents: 2910
diff changeset
88
a980d076052d added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents: 2910
diff changeset
89
1086
db817f3a1c65 * change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents: 770
diff changeset
90 # Short interfaces to split()
db817f3a1c65 * change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents: 770
diff changeset
91
db817f3a1c65 * change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents: 770
diff changeset
92 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
93 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
94
db817f3a1c65 * change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents: 770
diff changeset
95
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
96 def isdir(s):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
97 """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
98
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
99 try:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
100 st = os.stat(s)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
101 except os.error:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
102 return 0
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
103 return S_ISDIR(st[ST_MODE])
103
1229ba124e0b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
104
105
b4d5fdcbf83c Added explanatory comments.
Guido van Rossum <guido@python.org>
parents: 103
diff changeset
105
8746
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 7525
diff changeset
106 # Get size, mtime, atime of files.
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 7525
diff changeset
107
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 7525
diff changeset
108 def getsize(filename):
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 7525
diff changeset
109 """Return the size of a file, reported by os.stat()."""
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 7525
diff changeset
110 st = os.stat(filename)
11137
0a5a8f513d13 getsize(), getatime(), getmtime():
Fred Drake <fdrake@acm.org>
parents: 10018
diff changeset
111 return st[ST_SIZE]
8746
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 7525
diff changeset
112
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 7525
diff changeset
113 def getmtime(filename):
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 7525
diff changeset
114 """Return the last modification time of a file, reported by os.stat()."""
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 7525
diff changeset
115 st = os.stat(filename)
11137
0a5a8f513d13 getsize(), getatime(), getmtime():
Fred Drake <fdrake@acm.org>
parents: 10018
diff changeset
116 return st[ST_MTIME]
8746
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 7525
diff changeset
117
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 7525
diff changeset
118 def getatime(filename):
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 7525
diff changeset
119 """Return the last access time of a file, reported by os.stat()."""
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 7525
diff changeset
120 st = os.stat(filename)
13288
f119c2d8d560 getatime() returned the mtime instead of the atime.
Guido van Rossum <guido@python.org>
parents: 11706
diff changeset
121 return st[ST_ATIME]
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
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
124 def islink(s):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
125 """Return true if the pathname refers to a symbolic link.
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
126 Always false on the Mac, until we understand Aliases.)"""
2260
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 1810
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 return 0
2260
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 1810
diff changeset
129
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 1810
diff changeset
130
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
131 def isfile(s):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
132 """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
133
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
134 try:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
135 st = os.stat(s)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
136 except os.error:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
137 return 0
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
138 return S_ISREG(st[ST_MODE])
103
1229ba124e0b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
139
105
b4d5fdcbf83c Added explanatory comments.
Guido van Rossum <guido@python.org>
parents: 103
diff changeset
140
103
1229ba124e0b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 def exists(s):
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
142 """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
143
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
144 try:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
145 st = os.stat(s)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
146 except os.error:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
147 return 0
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
148 return 1
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
149
14128
039583c090c4 Restored commonprefix() semantics.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13876
diff changeset
150 # Return the longest prefix of all list elements.
039583c090c4 Restored commonprefix() semantics.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13876
diff changeset
151
039583c090c4 Restored commonprefix() semantics.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13876
diff changeset
152 def commonprefix(m):
039583c090c4 Restored commonprefix() semantics.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13876
diff changeset
153 "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
154 if not m: return ''
039583c090c4 Restored commonprefix() semantics.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13876
diff changeset
155 prefix = m[0]
039583c090c4 Restored commonprefix() semantics.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13876
diff changeset
156 for item in m:
039583c090c4 Restored commonprefix() semantics.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13876
diff changeset
157 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
158 if prefix[:i+1] != item[:i+1]:
14128
039583c090c4 Restored commonprefix() semantics.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13876
diff changeset
159 prefix = prefix[:i]
039583c090c4 Restored commonprefix() semantics.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13876
diff changeset
160 if i == 0: return ''
039583c090c4 Restored commonprefix() semantics.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13876
diff changeset
161 break
039583c090c4 Restored commonprefix() semantics.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13876
diff changeset
162 return prefix
3299
47763ac44c48 Added (dummy) expanduser and expandvar methods
Jack Jansen <jack.jansen@cwi.nl>
parents: 2947
diff changeset
163
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
164 def expandvars(path):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
165 """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
166 return path
1086
db817f3a1c65 * change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents: 770
diff changeset
167
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
168
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
169 def expanduser(path):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
170 """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
171 return path
2947
a980d076052d added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents: 2910
diff changeset
172
a980d076052d added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents: 2910
diff changeset
173 norm_error = 'macpath.norm_error: 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
174
db817f3a1c65 * change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents: 770
diff changeset
175 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
176 """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
177 equivalent paths."""
1c00fea9ae40 Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents: 13577
diff changeset
178
1c00fea9ae40 Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents: 13577
diff changeset
179 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
180 return ":"+s
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
181
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14128
diff changeset
182 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
183 i = 1
1c00fea9ae40 Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents: 13577
diff changeset
184 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
185 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
186 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
187 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
188 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
189 else:
1c00fea9ae40 Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents: 13577
diff changeset
190 # best way to handle this is to raise an exception
1c00fea9ae40 Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents: 13577
diff changeset
191 raise norm_error, 'Cannot use :: immedeately after volume name'
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
192 else:
13876
1c00fea9ae40 Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents: 13577
diff changeset
193 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
194
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14128
diff changeset
195 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
196
1c00fea9ae40 Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents: 13577
diff changeset
197 # 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
198 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
199 s = s[:-1]
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
200 return s
2910
5a2612fb6c0e Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents: 2260
diff changeset
201
2947
a980d076052d added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents: 2910
diff changeset
202
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
203 def walk(top, func, arg):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
204 """Directory tree walk.
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
205 For each directory under top (including top itself),
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
206 func(arg, dirname, filenames) is called, where
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
207 dirname is the name of the directory and filenames is the list
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
208 of files (and subdirectories etc.) in the directory.
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
209 The func may modify the filenames list, to implement a filter,
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
210 or to impose a different order of visiting."""
2910
5a2612fb6c0e Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents: 2260
diff changeset
211
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
212 try:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
213 names = os.listdir(top)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
214 except os.error:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
215 return
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
216 func(arg, top, names)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
217 for name in names:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
218 name = join(top, name)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
219 if isdir(name):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
220 walk(name, func, arg)
10018
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
221
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
222
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
223 def abspath(path):
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11137
diff changeset
224 """Return an absolute path."""
10018
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
225 if not isabs(path):
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
226 path = join(os.getcwd(), path)
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
227 return normpath(path)