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