1 | \section{\module{os.path} ---
|
---|
2 | Common pathname manipulations}
|
---|
3 | \declaremodule{standard}{os.path}
|
---|
4 |
|
---|
5 | \modulesynopsis{Common pathname manipulations.}
|
---|
6 |
|
---|
7 | This module implements some useful functions on pathnames.
|
---|
8 | \index{path!operations}
|
---|
9 |
|
---|
10 | \warning{On Windows, many of these functions do not properly
|
---|
11 | support UNC pathnames. \function{splitunc()} and \function{ismount()}
|
---|
12 | do handle them correctly.}
|
---|
13 |
|
---|
14 |
|
---|
15 | \begin{funcdesc}{abspath}{path}
|
---|
16 | Return a normalized absolutized version of the pathname \var{path}.
|
---|
17 | On most platforms, this is equivalent to
|
---|
18 | \code{normpath(join(os.getcwd(), \var{path}))}.
|
---|
19 | \versionadded{1.5.2}
|
---|
20 | \end{funcdesc}
|
---|
21 |
|
---|
22 | \begin{funcdesc}{basename}{path}
|
---|
23 | Return the base name of pathname \var{path}. This is the second half
|
---|
24 | of the pair returned by \code{split(\var{path})}. Note that the
|
---|
25 | result of this function is different from the
|
---|
26 | \UNIX{} \program{basename} program; where \program{basename} for
|
---|
27 | \code{'/foo/bar/'} returns \code{'bar'}, the \function{basename()}
|
---|
28 | function returns an empty string (\code{''}).
|
---|
29 | \end{funcdesc}
|
---|
30 |
|
---|
31 | \begin{funcdesc}{commonprefix}{list}
|
---|
32 | Return the longest path prefix (taken character-by-character) that is a
|
---|
33 | prefix of all paths in
|
---|
34 | \var{list}. If \var{list} is empty, return the empty string
|
---|
35 | (\code{''}). Note that this may return invalid paths because it works a
|
---|
36 | character at a time.
|
---|
37 | \end{funcdesc}
|
---|
38 |
|
---|
39 | \begin{funcdesc}{dirname}{path}
|
---|
40 | Return the directory name of pathname \var{path}. This is the first
|
---|
41 | half of the pair returned by \code{split(\var{path})}.
|
---|
42 | \end{funcdesc}
|
---|
43 |
|
---|
44 | \begin{funcdesc}{exists}{path}
|
---|
45 | Return \code{True} if \var{path} refers to an existing path. Returns
|
---|
46 | \code{False} for broken symbolic links. On some platforms, this
|
---|
47 | function may return \code{False} if permission is not granted to
|
---|
48 | execute \function{os.stat()} on the requested file, even if the
|
---|
49 | \var{path} physically exists.
|
---|
50 | \end{funcdesc}
|
---|
51 |
|
---|
52 | \begin{funcdesc}{lexists}{path}
|
---|
53 | Return \code{True} if \var{path} refers to an existing path.
|
---|
54 | Returns \code{True} for broken symbolic links.
|
---|
55 | Equivalent to \function{exists()} on platforms lacking
|
---|
56 | \function{os.lstat()}.
|
---|
57 | \versionadded{2.4}
|
---|
58 | \end{funcdesc}
|
---|
59 |
|
---|
60 | \begin{funcdesc}{expanduser}{path}
|
---|
61 | On \UNIX, return the argument with an initial component of \samp{\~} or
|
---|
62 | \samp{\~\var{user}} replaced by that \var{user}'s home directory.
|
---|
63 | An initial \samp{\~} is replaced by the environment variable
|
---|
64 | \envvar{HOME} if it is set; otherwise the current user's home directory
|
---|
65 | is looked up in the password directory through the built-in module
|
---|
66 | \refmodule{pwd}\refbimodindex{pwd}.
|
---|
67 | An initial \samp{\~\var{user}} is looked up directly in the
|
---|
68 | password directory.
|
---|
69 |
|
---|
70 | On Windows, only \samp{\~} is supported; it is replaced by the
|
---|
71 | environment variable \envvar{HOME} or by a combination of
|
---|
72 | \envvar{HOMEDRIVE} and \envvar{HOMEPATH}.
|
---|
73 |
|
---|
74 | If the expansion fails or if the
|
---|
75 | path does not begin with a tilde, the path is returned unchanged.
|
---|
76 | \end{funcdesc}
|
---|
77 |
|
---|
78 | \begin{funcdesc}{expandvars}{path}
|
---|
79 | Return the argument with environment variables expanded. Substrings
|
---|
80 | of the form \samp{\$\var{name}} or \samp{\$\{\var{name}\}} are
|
---|
81 | replaced by the value of environment variable \var{name}. Malformed
|
---|
82 | variable names and references to non-existing variables are left
|
---|
83 | unchanged.
|
---|
84 | \end{funcdesc}
|
---|
85 |
|
---|
86 | \begin{funcdesc}{getatime}{path}
|
---|
87 | Return the time of last access of \var{path}. The return
|
---|
88 | value is a number giving the number of seconds since the epoch (see the
|
---|
89 | \refmodule{time} module). Raise \exception{os.error} if the file does
|
---|
90 | not exist or is inaccessible.
|
---|
91 | \versionadded{1.5.2}
|
---|
92 | \versionchanged[If \function{os.stat_float_times()} returns True, the result is a floating point number]{2.3}
|
---|
93 | \end{funcdesc}
|
---|
94 |
|
---|
95 | \begin{funcdesc}{getmtime}{path}
|
---|
96 | Return the time of last modification of \var{path}. The return
|
---|
97 | value is a number giving the number of seconds since the epoch (see the
|
---|
98 | \refmodule{time} module). Raise \exception{os.error} if the file does
|
---|
99 | not exist or is inaccessible.
|
---|
100 | \versionadded{1.5.2}
|
---|
101 | \versionchanged[If \function{os.stat_float_times()} returns True, the result is a floating point number]{2.3}
|
---|
102 | \end{funcdesc}
|
---|
103 |
|
---|
104 | \begin{funcdesc}{getctime}{path}
|
---|
105 | Return the system's ctime which, on some systems (like \UNIX) is the
|
---|
106 | time of the last change, and, on others (like Windows), is the
|
---|
107 | creation time for \var{path}. The return
|
---|
108 | value is a number giving the number of seconds since the epoch (see the
|
---|
109 | \refmodule{time} module). Raise \exception{os.error} if the file does
|
---|
110 | not exist or is inaccessible.
|
---|
111 | \versionadded{2.3}
|
---|
112 | \end{funcdesc}
|
---|
113 |
|
---|
114 | \begin{funcdesc}{getsize}{path}
|
---|
115 | Return the size, in bytes, of \var{path}. Raise
|
---|
116 | \exception{os.error} if the file does not exist or is inaccessible.
|
---|
117 | \versionadded{1.5.2}
|
---|
118 | \end{funcdesc}
|
---|
119 |
|
---|
120 | \begin{funcdesc}{isabs}{path}
|
---|
121 | Return \code{True} if \var{path} is an absolute pathname (begins with a
|
---|
122 | slash).
|
---|
123 | \end{funcdesc}
|
---|
124 |
|
---|
125 | \begin{funcdesc}{isfile}{path}
|
---|
126 | Return \code{True} if \var{path} is an existing regular file. This follows
|
---|
127 | symbolic links, so both \function{islink()} and \function{isfile()}
|
---|
128 | can be true for the same path.
|
---|
129 | \end{funcdesc}
|
---|
130 |
|
---|
131 | \begin{funcdesc}{isdir}{path}
|
---|
132 | Return \code{True} if \var{path} is an existing directory. This follows
|
---|
133 | symbolic links, so both \function{islink()} and \function{isdir()} can
|
---|
134 | be true for the same path.
|
---|
135 | \end{funcdesc}
|
---|
136 |
|
---|
137 | \begin{funcdesc}{islink}{path}
|
---|
138 | Return \code{True} if \var{path} refers to a directory entry that is a
|
---|
139 | symbolic link. Always \code{False} if symbolic links are not supported.
|
---|
140 | \end{funcdesc}
|
---|
141 |
|
---|
142 | \begin{funcdesc}{ismount}{path}
|
---|
143 | Return \code{True} if pathname \var{path} is a \dfn{mount point}: a point in
|
---|
144 | a file system where a different file system has been mounted. The
|
---|
145 | function checks whether \var{path}'s parent, \file{\var{path}/..}, is
|
---|
146 | on a different device than \var{path}, or whether \file{\var{path}/..}
|
---|
147 | and \var{path} point to the same i-node on the same device --- this
|
---|
148 | should detect mount points for all \UNIX{} and \POSIX{} variants.
|
---|
149 | \end{funcdesc}
|
---|
150 |
|
---|
151 | \begin{funcdesc}{join}{path1\optional{, path2\optional{, ...}}}
|
---|
152 | Join one or more path components intelligently. If any component is
|
---|
153 | an absolute path, all previous components (on Windows, including the
|
---|
154 | previous drive letter, if there was one) are thrown away, and joining
|
---|
155 | continues. The return value is the concatenation of \var{path1}, and
|
---|
156 | optionally \var{path2}, etc., with exactly one directory separator
|
---|
157 | (\code{os.sep}) inserted between components, unless \var{path2} is
|
---|
158 | empty. Note that on Windows, since there is a current directory for
|
---|
159 | each drive, \function{os.path.join("c:", "foo")} represents a path
|
---|
160 | relative to the current directory on drive \file{C:} (\file{c:foo}), not
|
---|
161 | \file{c:\textbackslash\textbackslash foo}.
|
---|
162 | \end{funcdesc}
|
---|
163 |
|
---|
164 | \begin{funcdesc}{normcase}{path}
|
---|
165 | Normalize the case of a pathname. On \UNIX, this returns the path
|
---|
166 | unchanged; on case-insensitive filesystems, it converts the path to
|
---|
167 | lowercase. On Windows, it also converts forward slashes to backward
|
---|
168 | slashes.
|
---|
169 | \end{funcdesc}
|
---|
170 |
|
---|
171 | \begin{funcdesc}{normpath}{path}
|
---|
172 | Normalize a pathname. This collapses redundant separators and
|
---|
173 | up-level references so that \code{A//B}, \code{A/./B} and
|
---|
174 | \code{A/foo/../B} all become \code{A/B}. It does not normalize the
|
---|
175 | case (use \function{normcase()} for that). On Windows, it converts
|
---|
176 | forward slashes to backward slashes. It should be understood that this may
|
---|
177 | change the meaning of the path if it contains symbolic links!
|
---|
178 | \end{funcdesc}
|
---|
179 |
|
---|
180 | \begin{funcdesc}{realpath}{path}
|
---|
181 | Return the canonical path of the specified filename, eliminating any
|
---|
182 | symbolic links encountered in the path (if they are supported by the
|
---|
183 | operating system).
|
---|
184 | \versionadded{2.2}
|
---|
185 | \end{funcdesc}
|
---|
186 |
|
---|
187 | \begin{funcdesc}{samefile}{path1, path2}
|
---|
188 | Return \code{True} if both pathname arguments refer to the same file or
|
---|
189 | directory (as indicated by device number and i-node number).
|
---|
190 | Raise an exception if a \function{os.stat()} call on either pathname
|
---|
191 | fails.
|
---|
192 | Availability: Macintosh, \UNIX.
|
---|
193 | \end{funcdesc}
|
---|
194 |
|
---|
195 | \begin{funcdesc}{sameopenfile}{fp1, fp2}
|
---|
196 | Return \code{True} if the file descriptors \var{fp1} and \var{fp2} refer
|
---|
197 | to the same file.
|
---|
198 | Availability: Macintosh, \UNIX.
|
---|
199 | \end{funcdesc}
|
---|
200 |
|
---|
201 | \begin{funcdesc}{samestat}{stat1, stat2}
|
---|
202 | Return \code{True} if the stat tuples \var{stat1} and \var{stat2} refer to
|
---|
203 | the same file. These structures may have been returned by
|
---|
204 | \function{fstat()}, \function{lstat()}, or \function{stat()}. This
|
---|
205 | function implements the underlying comparison used by
|
---|
206 | \function{samefile()} and \function{sameopenfile()}.
|
---|
207 | Availability: Macintosh, \UNIX.
|
---|
208 | \end{funcdesc}
|
---|
209 |
|
---|
210 | \begin{funcdesc}{split}{path}
|
---|
211 | Split the pathname \var{path} into a pair, \code{(\var{head},
|
---|
212 | \var{tail})} where \var{tail} is the last pathname component and
|
---|
213 | \var{head} is everything leading up to that. The \var{tail} part will
|
---|
214 | never contain a slash; if \var{path} ends in a slash, \var{tail} will
|
---|
215 | be empty. If there is no slash in \var{path}, \var{head} will be
|
---|
216 | empty. If \var{path} is empty, both \var{head} and \var{tail} are
|
---|
217 | empty. Trailing slashes are stripped from \var{head} unless it is the
|
---|
218 | root (one or more slashes only). In nearly all cases,
|
---|
219 | \code{join(\var{head}, \var{tail})} equals \var{path} (the only
|
---|
220 | exception being when there were multiple slashes separating \var{head}
|
---|
221 | from \var{tail}).
|
---|
222 | \end{funcdesc}
|
---|
223 |
|
---|
224 | \begin{funcdesc}{splitdrive}{path}
|
---|
225 | Split the pathname \var{path} into a pair \code{(\var{drive},
|
---|
226 | \var{tail})} where \var{drive} is either a drive specification or the
|
---|
227 | empty string. On systems which do not use drive specifications,
|
---|
228 | \var{drive} will always be the empty string. In all cases,
|
---|
229 | \code{\var{drive} + \var{tail}} will be the same as \var{path}.
|
---|
230 | \versionadded{1.3}
|
---|
231 | \end{funcdesc}
|
---|
232 |
|
---|
233 | \begin{funcdesc}{splitext}{path}
|
---|
234 | Split the pathname \var{path} into a pair \code{(\var{root}, \var{ext})}
|
---|
235 | such that \code{\var{root} + \var{ext} == \var{path}},
|
---|
236 | and \var{ext} is empty or begins with a period and contains
|
---|
237 | at most one period.
|
---|
238 | \end{funcdesc}
|
---|
239 |
|
---|
240 | \begin{funcdesc}{splitunc}{path}
|
---|
241 | Split the pathname \var{path} into a pair \code{(\var{unc}, \var{rest})}
|
---|
242 | so that \var{unc} is the UNC mount point (such as \code{r'\e\e host\e mount'}),
|
---|
243 | if present, and \var{rest} the rest of the path (such as
|
---|
244 | \code{r'\e path\e file.ext'}). For paths containing drive letters, \var{unc}
|
---|
245 | will always be the empty string.
|
---|
246 | Availability: Windows.
|
---|
247 | \end{funcdesc}
|
---|
248 |
|
---|
249 | \begin{funcdesc}{walk}{path, visit, arg}
|
---|
250 | Calls the function \var{visit} with arguments
|
---|
251 | \code{(\var{arg}, \var{dirname}, \var{names})} for each directory in the
|
---|
252 | directory tree rooted at \var{path} (including \var{path} itself, if it
|
---|
253 | is a directory). The argument \var{dirname} specifies the visited
|
---|
254 | directory, the argument \var{names} lists the files in the directory
|
---|
255 | (gotten from \code{os.listdir(\var{dirname})}).
|
---|
256 | The \var{visit} function may modify \var{names} to
|
---|
257 | influence the set of directories visited below \var{dirname}, e.g. to
|
---|
258 | avoid visiting certain parts of the tree. (The object referred to by
|
---|
259 | \var{names} must be modified in place, using \keyword{del} or slice
|
---|
260 | assignment.)
|
---|
261 |
|
---|
262 | \begin{notice}
|
---|
263 | Symbolic links to directories are not treated as subdirectories, and
|
---|
264 | that \function{walk()} therefore will not visit them. To visit linked
|
---|
265 | directories you must identify them with
|
---|
266 | \code{os.path.islink(\var{file})} and
|
---|
267 | \code{os.path.isdir(\var{file})}, and invoke \function{walk()} as
|
---|
268 | necessary.
|
---|
269 | \end{notice}
|
---|
270 |
|
---|
271 | \note{The newer \function{\refmodule{os}.walk()} generator supplies
|
---|
272 | similar functionality and can be easier to use.}
|
---|
273 | \end{funcdesc}
|
---|
274 |
|
---|
275 | \begin{datadesc}{supports_unicode_filenames}
|
---|
276 | True if arbitrary Unicode strings can be used as file names (within
|
---|
277 | limitations imposed by the file system), and if
|
---|
278 | \function{os.listdir()} returns Unicode strings for a Unicode
|
---|
279 | argument.
|
---|
280 | \versionadded{2.3}
|
---|
281 | \end{datadesc}
|
---|