blob: d04fd0958f0334038368f158c86c4fe3aad19b56 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`os.path` --- Common pathname manipulations
2================================================
3
4.. module:: os.path
5 :synopsis: Operations on pathnames.
6
Georg Brandl116aa622007-08-15 14:28:22 +00007.. index:: single: path; operations
8
9This module implements some useful functions on pathnames. To read or
10write files see :func:`open`, and for accessing the filesystem see the
Martin v. Löwis651423c2008-10-07 07:03:04 +000011:mod:`os` module. The path parameters can be passed as either strings,
12or bytes. Applications are encouraged to represent file names as
13(Unicode) character strings. Unfortunately, some file names may not be
14representable as strings on Unix, so applications that need to support
15arbitrary file names on Unix should use bytes objects to represent
16path names. Vice versa, using bytes objects cannot represent all file
17names on Windows (in the standard ``mbcs`` encoding), hence Windows
18applications should use string objects to access all files.
Georg Brandl116aa622007-08-15 14:28:22 +000019
Georg Brandl76e55382008-10-08 16:34:57 +000020.. note::
21
22 All of these functions accept either only bytes or only string objects as
23 their parameters. The result is an object of the same type, if a path or
24 file name is returned.
25
Georg Brandl116aa622007-08-15 14:28:22 +000026.. warning::
27
28 On Windows, many of these functions do not properly support UNC pathnames.
29 :func:`splitunc` and :func:`ismount` do handle them correctly.
30
31
Benjamin Petersond23f8222009-04-05 19:13:16 +000032.. note::
33
34 Since different operating systems have different path name conventions, there
35 are several versions of this module in the standard library. The
36 :mod:`os.path` module is always the path module suitable for the operating
37 system Python is running on, and therefore usable for local paths. However,
38 you can also import and use the individual modules if you want to manipulate
39 a path that is *always* in one of the different formats. They all have the
40 same interface:
41
42 * :mod:`posixpath` for UNIX-style paths
43 * :mod:`ntpath` for Windows paths
44 * :mod:`macpath` for old-style MacOS paths
45 * :mod:`os2emxpath` for OS/2 EMX paths
46
47
Georg Brandl116aa622007-08-15 14:28:22 +000048.. function:: abspath(path)
49
50 Return a normalized absolutized version of the pathname *path*. On most
51 platforms, this is equivalent to ``normpath(join(os.getcwd(), path))``.
52
Georg Brandl116aa622007-08-15 14:28:22 +000053
54.. function:: basename(path)
55
56 Return the base name of pathname *path*. This is the second half of the pair
57 returned by ``split(path)``. Note that the result of this function is different
58 from the Unix :program:`basename` program; where :program:`basename` for
59 ``'/foo/bar/'`` returns ``'bar'``, the :func:`basename` function returns an
60 empty string (``''``).
61
62
63.. function:: commonprefix(list)
64
65 Return the longest path prefix (taken character-by-character) that is a prefix
66 of all paths in *list*. If *list* is empty, return the empty string (``''``).
67 Note that this may return invalid paths because it works a character at a time.
68
69
70.. function:: dirname(path)
71
72 Return the directory name of pathname *path*. This is the first half of the
73 pair returned by ``split(path)``.
74
75
76.. function:: exists(path)
77
78 Return ``True`` if *path* refers to an existing path. Returns ``False`` for
79 broken symbolic links. On some platforms, this function may return ``False`` if
80 permission is not granted to execute :func:`os.stat` on the requested file, even
81 if the *path* physically exists.
82
83
84.. function:: lexists(path)
85
86 Return ``True`` if *path* refers to an existing path. Returns ``True`` for
87 broken symbolic links. Equivalent to :func:`exists` on platforms lacking
88 :func:`os.lstat`.
89
Georg Brandl116aa622007-08-15 14:28:22 +000090
91.. function:: expanduser(path)
92
93 On Unix and Windows, return the argument with an initial component of ``~`` or
94 ``~user`` replaced by that *user*'s home directory.
95
96 .. index:: module: pwd
97
98 On Unix, an initial ``~`` is replaced by the environment variable :envvar:`HOME`
99 if it is set; otherwise the current user's home directory is looked up in the
100 password directory through the built-in module :mod:`pwd`. An initial ``~user``
101 is looked up directly in the password directory.
102
103 On Windows, :envvar:`HOME` and :envvar:`USERPROFILE` will be used if set,
104 otherwise a combination of :envvar:`HOMEPATH` and :envvar:`HOMEDRIVE` will be
105 used. An initial ``~user`` is handled by stripping the last directory component
106 from the created user path derived above.
107
108 If the expansion fails or if the path does not begin with a tilde, the path is
109 returned unchanged.
110
111
112.. function:: expandvars(path)
113
114 Return the argument with environment variables expanded. Substrings of the form
115 ``$name`` or ``${name}`` are replaced by the value of environment variable
116 *name*. Malformed variable names and references to non-existing variables are
117 left unchanged.
118
119 On Windows, ``%name%`` expansions are supported in addition to ``$name`` and
120 ``${name}``.
121
122
123.. function:: getatime(path)
124
125 Return the time of last access of *path*. The return value is a number giving
126 the number of seconds since the epoch (see the :mod:`time` module). Raise
127 :exc:`os.error` if the file does not exist or is inaccessible.
128
Georg Brandl55ac8f02007-09-01 13:51:09 +0000129 If :func:`os.stat_float_times` returns True, the result is a floating point
130 number.
Georg Brandl116aa622007-08-15 14:28:22 +0000131
132
133.. function:: getmtime(path)
134
135 Return the time of last modification of *path*. The return value is a number
136 giving the number of seconds since the epoch (see the :mod:`time` module).
137 Raise :exc:`os.error` if the file does not exist or is inaccessible.
138
Georg Brandl55ac8f02007-09-01 13:51:09 +0000139 If :func:`os.stat_float_times` returns True, the result is a floating point
140 number.
Georg Brandl116aa622007-08-15 14:28:22 +0000141
142
143.. function:: getctime(path)
144
145 Return the system's ctime which, on some systems (like Unix) is the time of the
146 last change, and, on others (like Windows), is the creation time for *path*.
147 The return value is a number giving the number of seconds since the epoch (see
148 the :mod:`time` module). Raise :exc:`os.error` if the file does not exist or
149 is inaccessible.
150
Georg Brandl116aa622007-08-15 14:28:22 +0000151
152.. function:: getsize(path)
153
154 Return the size, in bytes, of *path*. Raise :exc:`os.error` if the file does
155 not exist or is inaccessible.
156
Georg Brandl116aa622007-08-15 14:28:22 +0000157
158.. function:: isabs(path)
159
Christian Heimesaf98da12008-01-27 15:18:18 +0000160 Return ``True`` if *path* is an absolute pathname. On Unix, that means it
161 begins with a slash, on Windows that it begins with a (back)slash after chopping
162 off a potential drive letter.
Georg Brandl116aa622007-08-15 14:28:22 +0000163
164
165.. function:: isfile(path)
166
167 Return ``True`` if *path* is an existing regular file. This follows symbolic
168 links, so both :func:`islink` and :func:`isfile` can be true for the same path.
169
170
171.. function:: isdir(path)
172
173 Return ``True`` if *path* is an existing directory. This follows symbolic
174 links, so both :func:`islink` and :func:`isdir` can be true for the same path.
175
176
177.. function:: islink(path)
178
179 Return ``True`` if *path* refers to a directory entry that is a symbolic link.
180 Always ``False`` if symbolic links are not supported.
181
182
183.. function:: ismount(path)
184
185 Return ``True`` if pathname *path* is a :dfn:`mount point`: a point in a file
186 system where a different file system has been mounted. The function checks
187 whether *path*'s parent, :file:`path/..`, is on a different device than *path*,
188 or whether :file:`path/..` and *path* point to the same i-node on the same
189 device --- this should detect mount points for all Unix and POSIX variants.
190
191
192.. function:: join(path1[, path2[, ...]])
193
194 Join one or more path components intelligently. If any component is an absolute
195 path, all previous components (on Windows, including the previous drive letter,
196 if there was one) are thrown away, and joining continues. The return value is
197 the concatenation of *path1*, and optionally *path2*, etc., with exactly one
198 directory separator (``os.sep``) inserted between components, unless *path2* is
199 empty. Note that on Windows, since there is a current directory for each drive,
200 ``os.path.join("c:", "foo")`` represents a path relative to the current
201 directory on drive :file:`C:` (:file:`c:foo`), not :file:`c:\\foo`.
202
203
204.. function:: normcase(path)
205
Benjamin Petersond23f8222009-04-05 19:13:16 +0000206 Normalize the case of a pathname. On Unix and Mac OS X, this returns the
207 path unchanged; on case-insensitive filesystems, it converts the path to
208 lowercase. On Windows, it also converts forward slashes to backward slashes.
Georg Brandl116aa622007-08-15 14:28:22 +0000209
210
211.. function:: normpath(path)
212
213 Normalize a pathname. This collapses redundant separators and up-level
214 references so that ``A//B``, ``A/./B`` and ``A/foo/../B`` all become ``A/B``.
215 It does not normalize the case (use :func:`normcase` for that). On Windows, it
216 converts forward slashes to backward slashes. It should be understood that this
217 may change the meaning of the path if it contains symbolic links!
218
219
220.. function:: realpath(path)
221
222 Return the canonical path of the specified filename, eliminating any symbolic
223 links encountered in the path (if they are supported by the operating system).
224
Georg Brandl116aa622007-08-15 14:28:22 +0000225
226.. function:: relpath(path[, start])
227
228 Return a relative filepath to *path* either from the current directory or from
229 an optional *start* point.
230
231 *start* defaults to :attr:`os.curdir`. Availability: Windows, Unix.
232
Georg Brandl116aa622007-08-15 14:28:22 +0000233
234.. function:: samefile(path1, path2)
235
236 Return ``True`` if both pathname arguments refer to the same file or directory
237 (as indicated by device number and i-node number). Raise an exception if a
Georg Brandlc575c902008-09-13 17:46:05 +0000238 :func:`os.stat` call on either pathname fails. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000239
240
241.. function:: sameopenfile(fp1, fp2)
242
243 Return ``True`` if the file descriptors *fp1* and *fp2* refer to the same file.
Georg Brandlc575c902008-09-13 17:46:05 +0000244 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000245
246
247.. function:: samestat(stat1, stat2)
248
249 Return ``True`` if the stat tuples *stat1* and *stat2* refer to the same file.
250 These structures may have been returned by :func:`fstat`, :func:`lstat`, or
251 :func:`stat`. This function implements the underlying comparison used by
Georg Brandlc575c902008-09-13 17:46:05 +0000252 :func:`samefile` and :func:`sameopenfile`. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000253
254
255.. function:: split(path)
256
257 Split the pathname *path* into a pair, ``(head, tail)`` where *tail* is the last
258 pathname component and *head* is everything leading up to that. The *tail* part
259 will never contain a slash; if *path* ends in a slash, *tail* will be empty. If
260 there is no slash in *path*, *head* will be empty. If *path* is empty, both
261 *head* and *tail* are empty. Trailing slashes are stripped from *head* unless
262 it is the root (one or more slashes only). In nearly all cases, ``join(head,
263 tail)`` equals *path* (the only exception being when there were multiple slashes
264 separating *head* from *tail*).
265
266
267.. function:: splitdrive(path)
268
269 Split the pathname *path* into a pair ``(drive, tail)`` where *drive* is either
270 a drive specification or the empty string. On systems which do not use drive
271 specifications, *drive* will always be the empty string. In all cases, ``drive
272 + tail`` will be the same as *path*.
273
Georg Brandl116aa622007-08-15 14:28:22 +0000274
275.. function:: splitext(path)
276
277 Split the pathname *path* into a pair ``(root, ext)`` such that ``root + ext ==
278 path``, and *ext* is empty or begins with a period and contains at most one
279 period. Leading periods on the basename are ignored; ``splitext('.cshrc')``
280 returns ``('.cshrc', '')``.
281
Georg Brandl116aa622007-08-15 14:28:22 +0000282
283.. function:: splitunc(path)
284
285 Split the pathname *path* into a pair ``(unc, rest)`` so that *unc* is the UNC
286 mount point (such as ``r'\\host\mount'``), if present, and *rest* the rest of
287 the path (such as ``r'\path\file.ext'``). For paths containing drive letters,
288 *unc* will always be the empty string. Availability: Windows.
289
290
Georg Brandl116aa622007-08-15 14:28:22 +0000291.. data:: supports_unicode_filenames
292
293 True if arbitrary Unicode strings can be used as file names (within limitations
Georg Brandlf6945182008-02-01 11:56:49 +0000294 imposed by the file system), and if :func:`os.listdir` returns strings that
295 contain characters that cannot be represented by ASCII.