blob: 71eeb533bc98638b957d42123b31e33501218816 [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
Martin v. Löwis651423c2008-10-07 07:03:04 +000013:mod:`os` module. The path parameters can be passed as either strings,
14or bytes. Applications are encouraged to represent file names as
15(Unicode) character strings. Unfortunately, some file names may not be
16representable as strings on Unix, so applications that need to support
17arbitrary file names on Unix should use bytes objects to represent
18path names. Vice versa, using bytes objects cannot represent all file
19names on Windows (in the standard ``mbcs`` encoding), hence Windows
20applications should use string objects to access all files.
Georg Brandl116aa622007-08-15 14:28:22 +000021
22.. warning::
23
24 On Windows, many of these functions do not properly support UNC pathnames.
25 :func:`splitunc` and :func:`ismount` do handle them correctly.
26
27
28.. function:: abspath(path)
29
30 Return a normalized absolutized version of the pathname *path*. On most
31 platforms, this is equivalent to ``normpath(join(os.getcwd(), path))``.
32
Georg Brandl116aa622007-08-15 14:28:22 +000033
34.. function:: basename(path)
35
36 Return the base name of pathname *path*. This is the second half of the pair
37 returned by ``split(path)``. Note that the result of this function is different
38 from the Unix :program:`basename` program; where :program:`basename` for
39 ``'/foo/bar/'`` returns ``'bar'``, the :func:`basename` function returns an
40 empty string (``''``).
41
42
43.. function:: commonprefix(list)
44
45 Return the longest path prefix (taken character-by-character) that is a prefix
46 of all paths in *list*. If *list* is empty, return the empty string (``''``).
47 Note that this may return invalid paths because it works a character at a time.
48
49
50.. function:: dirname(path)
51
52 Return the directory name of pathname *path*. This is the first half of the
53 pair returned by ``split(path)``.
54
55
56.. function:: exists(path)
57
58 Return ``True`` if *path* refers to an existing path. Returns ``False`` for
59 broken symbolic links. On some platforms, this function may return ``False`` if
60 permission is not granted to execute :func:`os.stat` on the requested file, even
61 if the *path* physically exists.
62
63
64.. function:: lexists(path)
65
66 Return ``True`` if *path* refers to an existing path. Returns ``True`` for
67 broken symbolic links. Equivalent to :func:`exists` on platforms lacking
68 :func:`os.lstat`.
69
Georg Brandl116aa622007-08-15 14:28:22 +000070
71.. function:: expanduser(path)
72
73 On Unix and Windows, return the argument with an initial component of ``~`` or
74 ``~user`` replaced by that *user*'s home directory.
75
76 .. index:: module: pwd
77
78 On Unix, an initial ``~`` is replaced by the environment variable :envvar:`HOME`
79 if it is set; otherwise the current user's home directory is looked up in the
80 password directory through the built-in module :mod:`pwd`. An initial ``~user``
81 is looked up directly in the password directory.
82
83 On Windows, :envvar:`HOME` and :envvar:`USERPROFILE` will be used if set,
84 otherwise a combination of :envvar:`HOMEPATH` and :envvar:`HOMEDRIVE` will be
85 used. An initial ``~user`` is handled by stripping the last directory component
86 from the created user path derived above.
87
88 If the expansion fails or if the path does not begin with a tilde, the path is
89 returned unchanged.
90
91
92.. function:: expandvars(path)
93
94 Return the argument with environment variables expanded. Substrings of the form
95 ``$name`` or ``${name}`` are replaced by the value of environment variable
96 *name*. Malformed variable names and references to non-existing variables are
97 left unchanged.
98
99 On Windows, ``%name%`` expansions are supported in addition to ``$name`` and
100 ``${name}``.
101
102
103.. function:: getatime(path)
104
105 Return the time of last access of *path*. The return value is a number giving
106 the number of seconds since the epoch (see the :mod:`time` module). Raise
107 :exc:`os.error` if the file does not exist or is inaccessible.
108
Georg Brandl55ac8f02007-09-01 13:51:09 +0000109 If :func:`os.stat_float_times` returns True, the result is a floating point
110 number.
Georg Brandl116aa622007-08-15 14:28:22 +0000111
112
113.. function:: getmtime(path)
114
115 Return the time of last modification of *path*. The return value is a number
116 giving the number of seconds since the epoch (see the :mod:`time` module).
117 Raise :exc:`os.error` if the file does not exist or is inaccessible.
118
Georg Brandl55ac8f02007-09-01 13:51:09 +0000119 If :func:`os.stat_float_times` returns True, the result is a floating point
120 number.
Georg Brandl116aa622007-08-15 14:28:22 +0000121
122
123.. function:: getctime(path)
124
125 Return the system's ctime which, on some systems (like Unix) is the time of the
126 last change, and, on others (like Windows), is the creation time for *path*.
127 The return value is a number giving the number of seconds since the epoch (see
128 the :mod:`time` module). Raise :exc:`os.error` if the file does not exist or
129 is inaccessible.
130
Georg Brandl116aa622007-08-15 14:28:22 +0000131
132.. function:: getsize(path)
133
134 Return the size, in bytes, of *path*. Raise :exc:`os.error` if the file does
135 not exist or is inaccessible.
136
Georg Brandl116aa622007-08-15 14:28:22 +0000137
138.. function:: isabs(path)
139
Christian Heimesaf98da12008-01-27 15:18:18 +0000140 Return ``True`` if *path* is an absolute pathname. On Unix, that means it
141 begins with a slash, on Windows that it begins with a (back)slash after chopping
142 off a potential drive letter.
Georg Brandl116aa622007-08-15 14:28:22 +0000143
144
145.. function:: isfile(path)
146
147 Return ``True`` if *path* is an existing regular file. This follows symbolic
148 links, so both :func:`islink` and :func:`isfile` can be true for the same path.
149
150
151.. function:: isdir(path)
152
153 Return ``True`` if *path* is an existing directory. This follows symbolic
154 links, so both :func:`islink` and :func:`isdir` can be true for the same path.
155
156
157.. function:: islink(path)
158
159 Return ``True`` if *path* refers to a directory entry that is a symbolic link.
160 Always ``False`` if symbolic links are not supported.
161
162
163.. function:: ismount(path)
164
165 Return ``True`` if pathname *path* is a :dfn:`mount point`: a point in a file
166 system where a different file system has been mounted. The function checks
167 whether *path*'s parent, :file:`path/..`, is on a different device than *path*,
168 or whether :file:`path/..` and *path* point to the same i-node on the same
169 device --- this should detect mount points for all Unix and POSIX variants.
170
171
172.. function:: join(path1[, path2[, ...]])
173
174 Join one or more path components intelligently. If any component is an absolute
175 path, all previous components (on Windows, including the previous drive letter,
176 if there was one) are thrown away, and joining continues. The return value is
177 the concatenation of *path1*, and optionally *path2*, etc., with exactly one
178 directory separator (``os.sep``) inserted between components, unless *path2* is
179 empty. Note that on Windows, since there is a current directory for each drive,
180 ``os.path.join("c:", "foo")`` represents a path relative to the current
181 directory on drive :file:`C:` (:file:`c:foo`), not :file:`c:\\foo`.
182
183
184.. function:: normcase(path)
185
186 Normalize the case of a pathname. On Unix, this returns the path unchanged; on
187 case-insensitive filesystems, it converts the path to lowercase. On Windows, it
188 also converts forward slashes to backward slashes.
189
190
191.. function:: normpath(path)
192
193 Normalize a pathname. This collapses redundant separators and up-level
194 references so that ``A//B``, ``A/./B`` and ``A/foo/../B`` all become ``A/B``.
195 It does not normalize the case (use :func:`normcase` for that). On Windows, it
196 converts forward slashes to backward slashes. It should be understood that this
197 may change the meaning of the path if it contains symbolic links!
198
199
200.. function:: realpath(path)
201
202 Return the canonical path of the specified filename, eliminating any symbolic
203 links encountered in the path (if they are supported by the operating system).
204
Georg Brandl116aa622007-08-15 14:28:22 +0000205
206.. function:: relpath(path[, start])
207
208 Return a relative filepath to *path* either from the current directory or from
209 an optional *start* point.
210
211 *start* defaults to :attr:`os.curdir`. Availability: Windows, Unix.
212
Georg Brandl116aa622007-08-15 14:28:22 +0000213
214.. function:: samefile(path1, path2)
215
216 Return ``True`` if both pathname arguments refer to the same file or directory
217 (as indicated by device number and i-node number). Raise an exception if a
Georg Brandlc575c902008-09-13 17:46:05 +0000218 :func:`os.stat` call on either pathname fails. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000219
220
221.. function:: sameopenfile(fp1, fp2)
222
223 Return ``True`` if the file descriptors *fp1* and *fp2* refer to the same file.
Georg Brandlc575c902008-09-13 17:46:05 +0000224 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000225
226
227.. function:: samestat(stat1, stat2)
228
229 Return ``True`` if the stat tuples *stat1* and *stat2* refer to the same file.
230 These structures may have been returned by :func:`fstat`, :func:`lstat`, or
231 :func:`stat`. This function implements the underlying comparison used by
Georg Brandlc575c902008-09-13 17:46:05 +0000232 :func:`samefile` and :func:`sameopenfile`. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000233
234
235.. function:: split(path)
236
237 Split the pathname *path* into a pair, ``(head, tail)`` where *tail* is the last
238 pathname component and *head* is everything leading up to that. The *tail* part
239 will never contain a slash; if *path* ends in a slash, *tail* will be empty. If
240 there is no slash in *path*, *head* will be empty. If *path* is empty, both
241 *head* and *tail* are empty. Trailing slashes are stripped from *head* unless
242 it is the root (one or more slashes only). In nearly all cases, ``join(head,
243 tail)`` equals *path* (the only exception being when there were multiple slashes
244 separating *head* from *tail*).
245
246
247.. function:: splitdrive(path)
248
249 Split the pathname *path* into a pair ``(drive, tail)`` where *drive* is either
250 a drive specification or the empty string. On systems which do not use drive
251 specifications, *drive* will always be the empty string. In all cases, ``drive
252 + tail`` will be the same as *path*.
253
Georg Brandl116aa622007-08-15 14:28:22 +0000254
255.. function:: splitext(path)
256
257 Split the pathname *path* into a pair ``(root, ext)`` such that ``root + ext ==
258 path``, and *ext* is empty or begins with a period and contains at most one
259 period. Leading periods on the basename are ignored; ``splitext('.cshrc')``
260 returns ``('.cshrc', '')``.
261
Georg Brandl116aa622007-08-15 14:28:22 +0000262
263.. function:: splitunc(path)
264
265 Split the pathname *path* into a pair ``(unc, rest)`` so that *unc* is the UNC
266 mount point (such as ``r'\\host\mount'``), if present, and *rest* the rest of
267 the path (such as ``r'\path\file.ext'``). For paths containing drive letters,
268 *unc* will always be the empty string. Availability: Windows.
269
270
Georg Brandl116aa622007-08-15 14:28:22 +0000271.. data:: supports_unicode_filenames
272
273 True if arbitrary Unicode strings can be used as file names (within limitations
Georg Brandlf6945182008-02-01 11:56:49 +0000274 imposed by the file system), and if :func:`os.listdir` returns strings that
275 contain characters that cannot be represented by ASCII.