source: vendor/python/2.5/Doc/lib/libposixpath.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

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