Mercurial > cpython-lru_cache-weakref
annotate Lib/macpath.py @ 13163:a09e8635534d legacy-trunk
Don't try to guess the name of a .def file -- if one is supplied, use it,
otherwise just generate an '/export:' option.
| author | Greg Ward <gward@python.net> |
|---|---|
| date | Thu, 29 Jun 2000 23:09:20 +0000 |
| parents | b6f52c1ef6da |
| children | f119c2d8d560 |
| 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 | 2 |
|
579
6884346c04a0
Added 'normcase' function.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
3 import string |
|
7525
88992696b9e8
Instead of 'import mac', use 'import os' -- this way, the path syntax
Guido van Rossum <guido@python.org>
parents:
5576
diff
changeset
|
4 import os |
|
579
6884346c04a0
Added 'normcase' function.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
5 from stat import * |
| 103 | 6 |
|
579
6884346c04a0
Added 'normcase' function.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
7 |
|
6884346c04a0
Added 'normcase' function.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
8 # Normalize the case of a pathname. Dummy in Posix, but string.lower here. |
|
6884346c04a0
Added 'normcase' function.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
9 |
|
6884346c04a0
Added 'normcase' function.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
10 normcase = string.lower |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
11 |
| 103 | 12 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
13 def isabs(s): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
14 """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
|
15 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
|
16 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
|
17 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
|
18 volume name).""" |
| 103 | 19 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
20 return ':' in s and s[0] <> ':' |
| 103 | 21 |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
22 |
|
5133
666152b77027
join(): join one or more path components
Barry Warsaw <barry@python.org>
parents:
3793
diff
changeset
|
23 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
|
24 path = s |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
25 for t in p: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
26 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
|
27 path = t |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
28 continue |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
29 if t[:1] == ':': |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
30 t = t[1:] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
31 if ':' not in path: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
32 path = ':' + path |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
33 if path[-1:] <> ':': |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
34 path = path + ':' |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
35 path = path + t |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
36 return path |
| 103 | 37 |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
38 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
39 def split(s): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
40 """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
|
41 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
|
42 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
|
43 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
44 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
|
45 colon = 0 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
46 for i in range(len(s)): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
47 if s[i] == ':': colon = i+1 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
48 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
|
49 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
|
50 path = path + ':' |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
51 return path, file |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
52 |
|
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 splitext(p): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
55 """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
|
56 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
|
57 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
|
58 It is always true that root + ext == p.""" |
| 3793 | 59 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
60 root, ext = '', '' |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
61 for c in p: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
62 if c == ':': |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
63 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
|
64 elif c == '.': |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
65 if ext: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
66 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
|
67 else: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
68 ext = c |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
69 elif ext: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
70 ext = 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 root = root + c |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
73 return root, ext |
| 3793 | 74 |
| 75 | |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
76 def splitdrive(p): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
77 """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
|
78 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
|
79 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
|
80 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
|
81 being a separate current directory per drive).""" |
|
2947
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
82 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
83 return '', p |
|
2947
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
84 |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
85 |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
86 # Short interfaces to split() |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
87 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
88 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
|
89 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
|
90 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
91 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
92 def isdir(s): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
93 """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
|
94 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
95 try: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
96 st = os.stat(s) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
97 except os.error: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
98 return 0 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
99 return S_ISDIR(st[ST_MODE]) |
| 103 | 100 |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
101 |
|
8746
ca1b906149e6
Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents:
7525
diff
changeset
|
102 # Get size, mtime, atime of files. |
|
ca1b906149e6
Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents:
7525
diff
changeset
|
103 |
|
ca1b906149e6
Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents:
7525
diff
changeset
|
104 def getsize(filename): |
|
ca1b906149e6
Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents:
7525
diff
changeset
|
105 """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
|
106 st = os.stat(filename) |
|
11137
0a5a8f513d13
getsize(), getatime(), getmtime():
Fred Drake <fdrake@acm.org>
parents:
10018
diff
changeset
|
107 return st[ST_SIZE] |
|
8746
ca1b906149e6
Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents:
7525
diff
changeset
|
108 |
|
ca1b906149e6
Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents:
7525
diff
changeset
|
109 def getmtime(filename): |
|
ca1b906149e6
Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents:
7525
diff
changeset
|
110 """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
|
111 st = os.stat(filename) |
|
11137
0a5a8f513d13
getsize(), getatime(), getmtime():
Fred Drake <fdrake@acm.org>
parents:
10018
diff
changeset
|
112 return st[ST_MTIME] |
|
8746
ca1b906149e6
Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents:
7525
diff
changeset
|
113 |
|
ca1b906149e6
Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents:
7525
diff
changeset
|
114 def getatime(filename): |
|
ca1b906149e6
Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents:
7525
diff
changeset
|
115 """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
|
116 st = os.stat(filename) |
|
11137
0a5a8f513d13
getsize(), getatime(), getmtime():
Fred Drake <fdrake@acm.org>
parents:
10018
diff
changeset
|
117 return st[ST_MTIME] |
|
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 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
120 def islink(s): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
121 """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
|
122 Always false on the Mac, until we understand Aliases.)""" |
| 2260 | 123 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
124 return 0 |
| 2260 | 125 |
| 126 | |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
127 def isfile(s): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
128 """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
|
129 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
130 try: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
131 st = os.stat(s) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
132 except os.error: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
133 return 0 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
134 return S_ISREG(st[ST_MODE]) |
| 103 | 135 |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
136 |
| 103 | 137 def exists(s): |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
138 """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
|
139 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
140 try: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
141 st = os.stat(s) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
142 except os.error: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
143 return 0 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
144 return 1 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
145 |
|
3299
47763ac44c48
Added (dummy) expanduser and expandvar methods
Jack Jansen <jack.jansen@cwi.nl>
parents:
2947
diff
changeset
|
146 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
147 def expandvars(path): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
148 """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
|
149 return path |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
150 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
151 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
152 def expanduser(path): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
153 """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
|
154 return path |
|
2947
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
155 |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
156 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
|
157 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
158 def normpath(s): |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
159 """Normalize a pathname: get rid of '::' sequences by backing up, |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
160 e.g., 'foo:bar::bletch' becomes 'foo:bletch'. |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
161 Raise the exception norm_error below if backing up is impossible, |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
162 e.g., for '::foo'.""" |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
163 # XXX The Unix version doesn't raise an exception but simply |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
164 # returns an unnormalized path. Should do so here too. |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
165 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
166 import string |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
167 if ':' not in s: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
168 return ':' + s |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
169 f = string.splitfields(s, ':') |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
170 pre = [] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
171 post = [] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
172 if not f[0]: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
173 pre = f[:1] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
174 f = f[1:] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
175 if not f[len(f)-1]: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
176 post = f[-1:] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
177 f = f[:-1] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
178 res = [] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
179 for seg in f: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
180 if seg: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
181 res.append(seg) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
182 else: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
183 if not res: raise norm_error, 'path starts with ::' |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
184 del res[len(res)-1] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
185 if not (pre or res): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
186 raise norm_error, 'path starts with volume::' |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
187 if pre: res = pre + res |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
188 if post: res = res + post |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
189 s = res[0] |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
190 for seg in res[1:]: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
191 s = s + ':' + seg |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
192 return s |
|
2910
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
193 |
|
2947
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
194 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
195 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
|
196 """Directory tree walk. |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
197 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
|
198 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
|
199 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
|
200 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
|
201 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
|
202 or to impose a different order of visiting.""" |
|
2910
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
203 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
204 try: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
205 names = os.listdir(top) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
206 except os.error: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
207 return |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
208 func(arg, top, names) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
209 for name in names: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
210 name = join(top, name) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
211 if isdir(name): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
212 walk(name, func, arg) |
| 10018 | 213 |
| 214 | |
| 215 def abspath(path): | |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
216 """Return an absolute path.""" |
| 10018 | 217 if not isabs(path): |
| 218 path = join(os.getcwd(), path) | |
| 219 return normpath(path) |
