blob: 3e84de0ce8b498a9fe4d0acd3fd1122a0c4e6169 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`os.path` --- Common pathname manipulations
2================================================
3
4.. module:: os.path
5 :synopsis: Operations on pathnames.
6
Georg Brandl8ec7f652007-08-15 14:28:01 +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
11:mod:`os` module.
12
Georg Brandl16a57f62009-04-27 15:29:09 +000013.. note::
Georg Brandl8ec7f652007-08-15 14:28:01 +000014
15 On Windows, many of these functions do not properly support UNC pathnames.
16 :func:`splitunc` and :func:`ismount` do handle them correctly.
17
18
Georg Brandl5d196102009-04-05 10:41:02 +000019.. note::
20
21 Since different operating systems have different path name conventions, there
22 are several versions of this module in the standard library. The
23 :mod:`os.path` module is always the path module suitable for the operating
24 system Python is running on, and therefore usable for local paths. However,
25 you can also import and use the individual modules if you want to manipulate
26 a path that is *always* in one of the different formats. They all have the
27 same interface:
28
29 * :mod:`posixpath` for UNIX-style paths
30 * :mod:`ntpath` for Windows paths
31 * :mod:`macpath` for old-style MacOS paths
32 * :mod:`os2emxpath` for OS/2 EMX paths
33
34
Georg Brandl8ec7f652007-08-15 14:28:01 +000035.. function:: abspath(path)
36
37 Return a normalized absolutized version of the pathname *path*. On most
Chris Jerdonek55b4cfb2012-11-25 20:35:23 -080038 platforms, this is equivalent to calling the function :func:`normpath` as
39 follows: ``normpath(join(os.getcwd(), path))``.
Georg Brandl8ec7f652007-08-15 14:28:01 +000040
41 .. versionadded:: 1.5.2
42
43
44.. function:: basename(path)
45
Chris Jerdonek55b4cfb2012-11-25 20:35:23 -080046 Return the base name of pathname *path*. This is the second element of the
47 pair returned by passing *path* to the function :func:`split`. Note that
48 the result of this function is different
Georg Brandl8ec7f652007-08-15 14:28:01 +000049 from the Unix :program:`basename` program; where :program:`basename` for
50 ``'/foo/bar/'`` returns ``'bar'``, the :func:`basename` function returns an
51 empty string (``''``).
52
53
54.. function:: commonprefix(list)
55
56 Return the longest path prefix (taken character-by-character) that is a prefix
57 of all paths in *list*. If *list* is empty, return the empty string (``''``).
58 Note that this may return invalid paths because it works a character at a time.
59
60
61.. function:: dirname(path)
62
Chris Jerdonek55b4cfb2012-11-25 20:35:23 -080063 Return the directory name of pathname *path*. This is the first element of
64 the pair returned by passing *path* to the function :func:`split`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000065
66
67.. function:: exists(path)
68
69 Return ``True`` if *path* refers to an existing path. Returns ``False`` for
70 broken symbolic links. On some platforms, this function may return ``False`` if
71 permission is not granted to execute :func:`os.stat` on the requested file, even
72 if the *path* physically exists.
73
74
75.. function:: lexists(path)
76
77 Return ``True`` if *path* refers to an existing path. Returns ``True`` for
78 broken symbolic links. Equivalent to :func:`exists` on platforms lacking
79 :func:`os.lstat`.
80
81 .. versionadded:: 2.4
82
83
84.. function:: expanduser(path)
85
86 On Unix and Windows, return the argument with an initial component of ``~`` or
87 ``~user`` replaced by that *user*'s home directory.
88
89 .. index:: module: pwd
90
91 On Unix, an initial ``~`` is replaced by the environment variable :envvar:`HOME`
92 if it is set; otherwise the current user's home directory is looked up in the
93 password directory through the built-in module :mod:`pwd`. An initial ``~user``
94 is looked up directly in the password directory.
95
96 On Windows, :envvar:`HOME` and :envvar:`USERPROFILE` will be used if set,
97 otherwise a combination of :envvar:`HOMEPATH` and :envvar:`HOMEDRIVE` will be
98 used. An initial ``~user`` is handled by stripping the last directory component
99 from the created user path derived above.
100
101 If the expansion fails or if the path does not begin with a tilde, the path is
102 returned unchanged.
103
104
105.. function:: expandvars(path)
106
107 Return the argument with environment variables expanded. Substrings of the form
108 ``$name`` or ``${name}`` are replaced by the value of environment variable
109 *name*. Malformed variable names and references to non-existing variables are
110 left unchanged.
111
112 On Windows, ``%name%`` expansions are supported in addition to ``$name`` and
113 ``${name}``.
114
115
116.. function:: getatime(path)
117
118 Return the time of last access of *path*. The return value is a number giving
119 the number of seconds since the epoch (see the :mod:`time` module). Raise
120 :exc:`os.error` if the file does not exist or is inaccessible.
121
122 .. versionadded:: 1.5.2
123
124 .. versionchanged:: 2.3
125 If :func:`os.stat_float_times` returns True, the result is a floating point
126 number.
127
128
129.. function:: getmtime(path)
130
131 Return the time of last modification of *path*. The return value is a number
132 giving the number of seconds since the epoch (see the :mod:`time` module).
133 Raise :exc:`os.error` if the file does not exist or is inaccessible.
134
135 .. versionadded:: 1.5.2
136
137 .. versionchanged:: 2.3
138 If :func:`os.stat_float_times` returns True, the result is a floating point
139 number.
140
141
142.. function:: getctime(path)
143
144 Return the system's ctime which, on some systems (like Unix) is the time of the
145 last change, and, on others (like Windows), is the creation time for *path*.
146 The return value is a number giving the number of seconds since the epoch (see
147 the :mod:`time` module). Raise :exc:`os.error` if the file does not exist or
148 is inaccessible.
149
150 .. versionadded:: 2.3
151
152
153.. function:: getsize(path)
154
155 Return the size, in bytes, of *path*. Raise :exc:`os.error` if the file does
156 not exist or is inaccessible.
157
158 .. versionadded:: 1.5.2
159
160
161.. function:: isabs(path)
162
Georg Brandlfe7dd502008-01-26 09:43:35 +0000163 Return ``True`` if *path* is an absolute pathname. On Unix, that means it
Georg Brandl05225482008-01-26 11:02:22 +0000164 begins with a slash, on Windows that it begins with a (back)slash after chopping
Georg Brandlfe7dd502008-01-26 09:43:35 +0000165 off a potential drive letter.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000166
167
168.. function:: isfile(path)
169
170 Return ``True`` if *path* is an existing regular file. This follows symbolic
171 links, so both :func:`islink` and :func:`isfile` can be true for the same path.
172
173
174.. function:: isdir(path)
175
176 Return ``True`` if *path* is an existing directory. This follows symbolic
177 links, so both :func:`islink` and :func:`isdir` can be true for the same path.
178
179
180.. function:: islink(path)
181
182 Return ``True`` if *path* refers to a directory entry that is a symbolic link.
183 Always ``False`` if symbolic links are not supported.
184
185
186.. function:: ismount(path)
187
188 Return ``True`` if pathname *path* is a :dfn:`mount point`: a point in a file
189 system where a different file system has been mounted. The function checks
190 whether *path*'s parent, :file:`path/..`, is on a different device than *path*,
191 or whether :file:`path/..` and *path* point to the same i-node on the same
192 device --- this should detect mount points for all Unix and POSIX variants.
193
194
195.. function:: join(path1[, path2[, ...]])
196
197 Join one or more path components intelligently. If any component is an absolute
198 path, all previous components (on Windows, including the previous drive letter,
199 if there was one) are thrown away, and joining continues. The return value is
200 the concatenation of *path1*, and optionally *path2*, etc., with exactly one
R David Murray17e2b402011-06-23 21:19:25 -0400201 directory separator (``os.sep``) following each non-empty part except the last.
202 (This means that an empty last part will result in a path that ends with a
203 separator.) Note that on Windows, since there is a current directory for
204 each drive, ``os.path.join("c:", "foo")`` represents a path relative to the
205 current directory on drive :file:`C:` (:file:`c:foo`), not :file:`c:\\foo`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000206
207
208.. function:: normcase(path)
209
Georg Brandl89b12962009-04-05 10:29:57 +0000210 Normalize the case of a pathname. On Unix and Mac OS X, this returns the
211 path unchanged; on case-insensitive filesystems, it converts the path to
212 lowercase. On Windows, it also converts forward slashes to backward slashes.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000213
214
215.. function:: normpath(path)
216
217 Normalize a pathname. This collapses redundant separators and up-level
Georg Brandl7d4bfb32010-08-02 21:44:25 +0000218 references so that ``A//B``, ``A/B/``, ``A/./B`` and ``A/foo/../B`` all become
219 ``A/B``.
220
Georg Brandl8ec7f652007-08-15 14:28:01 +0000221 It does not normalize the case (use :func:`normcase` for that). On Windows, it
222 converts forward slashes to backward slashes. It should be understood that this
223 may change the meaning of the path if it contains symbolic links!
224
225
226.. function:: realpath(path)
227
228 Return the canonical path of the specified filename, eliminating any symbolic
229 links encountered in the path (if they are supported by the operating system).
230
231 .. versionadded:: 2.2
232
233
234.. function:: relpath(path[, start])
235
236 Return a relative filepath to *path* either from the current directory or from
237 an optional *start* point.
238
Benjamin Peterson7aaef842010-05-06 22:33:46 +0000239 *start* defaults to :attr:`os.curdir`.
240
241 Availability: Windows, Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000242
243 .. versionadded:: 2.6
244
245
246.. function:: samefile(path1, path2)
247
248 Return ``True`` if both pathname arguments refer to the same file or directory
249 (as indicated by device number and i-node number). Raise an exception if a
Benjamin Peterson7aaef842010-05-06 22:33:46 +0000250 :func:`os.stat` call on either pathname fails.
251
252 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000253
254
255.. function:: sameopenfile(fp1, fp2)
256
257 Return ``True`` if the file descriptors *fp1* and *fp2* refer to the same file.
Benjamin Peterson7aaef842010-05-06 22:33:46 +0000258
Georg Brandl9af94982008-09-13 17:41:16 +0000259 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000260
261
262.. function:: samestat(stat1, stat2)
263
264 Return ``True`` if the stat tuples *stat1* and *stat2* refer to the same file.
265 These structures may have been returned by :func:`fstat`, :func:`lstat`, or
266 :func:`stat`. This function implements the underlying comparison used by
Benjamin Peterson7aaef842010-05-06 22:33:46 +0000267 :func:`samefile` and :func:`sameopenfile`.
268
269 Availability: Unix.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000270
271
272.. function:: split(path)
273
Georg Brandl420cca92010-11-26 07:21:01 +0000274 Split the pathname *path* into a pair, ``(head, tail)`` where *tail* is the
275 last pathname component and *head* is everything leading up to that. The
276 *tail* part will never contain a slash; if *path* ends in a slash, *tail*
277 will be empty. If there is no slash in *path*, *head* will be empty. If
278 *path* is empty, both *head* and *tail* are empty. Trailing slashes are
279 stripped from *head* unless it is the root (one or more slashes only). In
280 all cases, ``join(head, tail)`` returns a path to the same location as *path*
Chris Jerdonek55b4cfb2012-11-25 20:35:23 -0800281 (but the strings may differ). Also see the functions :func:`dirname` and
282 :func:`basename`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000283
284
285.. function:: splitdrive(path)
286
287 Split the pathname *path* into a pair ``(drive, tail)`` where *drive* is either
288 a drive specification or the empty string. On systems which do not use drive
289 specifications, *drive* will always be the empty string. In all cases, ``drive
290 + tail`` will be the same as *path*.
291
292 .. versionadded:: 1.3
293
294
295.. function:: splitext(path)
296
297 Split the pathname *path* into a pair ``(root, ext)`` such that ``root + ext ==
298 path``, and *ext* is empty or begins with a period and contains at most one
299 period. Leading periods on the basename are ignored; ``splitext('.cshrc')``
300 returns ``('.cshrc', '')``.
301
302 .. versionchanged:: 2.6
303 Earlier versions could produce an empty root when the only period was the
304 first character.
305
306
307.. function:: splitunc(path)
308
309 Split the pathname *path* into a pair ``(unc, rest)`` so that *unc* is the UNC
310 mount point (such as ``r'\\host\mount'``), if present, and *rest* the rest of
311 the path (such as ``r'\path\file.ext'``). For paths containing drive letters,
Benjamin Peterson7aaef842010-05-06 22:33:46 +0000312 *unc* will always be the empty string.
313
314 Availability: Windows.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000315
316
317.. function:: walk(path, visit, arg)
318
319 Calls the function *visit* with arguments ``(arg, dirname, names)`` for each
320 directory in the directory tree rooted at *path* (including *path* itself, if it
321 is a directory). The argument *dirname* specifies the visited directory, the
322 argument *names* lists the files in the directory (gotten from
323 ``os.listdir(dirname)``). The *visit* function may modify *names* to influence
324 the set of directories visited below *dirname*, e.g. to avoid visiting certain
325 parts of the tree. (The object referred to by *names* must be modified in
326 place, using :keyword:`del` or slice assignment.)
327
328 .. note::
329
330 Symbolic links to directories are not treated as subdirectories, and that
331 :func:`walk` therefore will not visit them. To visit linked directories you must
332 identify them with ``os.path.islink(file)`` and ``os.path.isdir(file)``, and
333 invoke :func:`walk` as necessary.
334
Georg Brandl16a57f62009-04-27 15:29:09 +0000335 .. note::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000336
Ezio Melotti510ff542012-05-03 19:21:40 +0300337 This function is deprecated and has been removed in Python 3 in favor of
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000338 :func:`os.walk`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000339
340
341.. data:: supports_unicode_filenames
342
343 True if arbitrary Unicode strings can be used as file names (within limitations
Victor Stinner46287f52010-09-13 20:31:34 +0000344 imposed by the file system).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000345
346 .. versionadded:: 2.3
347