blob: e4fe44ed94fd8b6cf44dc33f03c49344343bdd04 [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
R David Murraya4e700c2013-01-06 16:13:10 -050020Unlike a unix shell, Python does not do any *automatic* path expansions.
21Functions such as :func:`expanduser` and :func:`expandvars` can be invoked
22explicitly when an application desires shell-like path expansion. (See also
23the :mod:`glob` module.)
24
Antoine Pitrou31119e42013-11-22 17:38:12 +010025
26.. seealso::
27 The :mod:`pathlib` module offers high-level path objects.
28
29
Georg Brandl76e55382008-10-08 16:34:57 +000030.. note::
31
32 All of these functions accept either only bytes or only string objects as
33 their parameters. The result is an object of the same type, if a path or
34 file name is returned.
35
Georg Brandl116aa622007-08-15 14:28:22 +000036
Benjamin Petersond23f8222009-04-05 19:13:16 +000037.. note::
38
39 Since different operating systems have different path name conventions, there
40 are several versions of this module in the standard library. The
41 :mod:`os.path` module is always the path module suitable for the operating
42 system Python is running on, and therefore usable for local paths. However,
43 you can also import and use the individual modules if you want to manipulate
44 a path that is *always* in one of the different formats. They all have the
45 same interface:
46
47 * :mod:`posixpath` for UNIX-style paths
48 * :mod:`ntpath` for Windows paths
49 * :mod:`macpath` for old-style MacOS paths
Benjamin Petersond23f8222009-04-05 19:13:16 +000050
51
Georg Brandl116aa622007-08-15 14:28:22 +000052.. function:: abspath(path)
53
54 Return a normalized absolutized version of the pathname *path*. On most
Chris Jerdonek0b502ff2012-11-25 20:38:01 -080055 platforms, this is equivalent to calling the function :func:`normpath` as
56 follows: ``normpath(join(os.getcwd(), path))``.
Georg Brandl116aa622007-08-15 14:28:22 +000057
Georg Brandl116aa622007-08-15 14:28:22 +000058
59.. function:: basename(path)
60
Chris Jerdonek0b502ff2012-11-25 20:38:01 -080061 Return the base name of pathname *path*. This is the second element of the
62 pair returned by passing *path* to the function :func:`split`. Note that
63 the result of this function is different
Georg Brandl116aa622007-08-15 14:28:22 +000064 from the Unix :program:`basename` program; where :program:`basename` for
65 ``'/foo/bar/'`` returns ``'bar'``, the :func:`basename` function returns an
66 empty string (``''``).
67
68
Serhiy Storchaka38220932015-03-31 15:31:53 +030069.. function:: commonpath(paths)
70
71 Return the longest common sub-path of each pathname in the sequence
72 *paths*. Raise ValueError if *paths* contains both absolute and relative
73 pathnames, or if *paths* is empty. Unlike :func:`commonprefix`, this
74 returns a valid path.
75
76 Availability: Unix, Windows
77
78 .. versionadded:: 3.5
79
80
Georg Brandl116aa622007-08-15 14:28:22 +000081.. function:: commonprefix(list)
82
Serhiy Storchaka38220932015-03-31 15:31:53 +030083 Return the longest path prefix (taken character-by-character) that is a
84 prefix of all paths in *list*. If *list* is empty, return the empty string
85 (``''``). Note that this may return invalid paths because it works a
86 character at a time. To obtain a valid path, see :func:`commonpath`.
Georg Brandl116aa622007-08-15 14:28:22 +000087
88
89.. function:: dirname(path)
90
Chris Jerdonek0b502ff2012-11-25 20:38:01 -080091 Return the directory name of pathname *path*. This is the first element of
92 the pair returned by passing *path* to the function :func:`split`.
Georg Brandl116aa622007-08-15 14:28:22 +000093
94
95.. function:: exists(path)
96
Richard Oudkerk2240ac12012-07-06 12:05:32 +010097 Return ``True`` if *path* refers to an existing path or an open
98 file descriptor. Returns ``False`` for broken symbolic links. On
99 some platforms, this function may return ``False`` if permission is
100 not granted to execute :func:`os.stat` on the requested file, even
Georg Brandl116aa622007-08-15 14:28:22 +0000101 if the *path* physically exists.
102
Richard Oudkerk2240ac12012-07-06 12:05:32 +0100103 .. versionchanged:: 3.3
104 *path* can now be an integer: ``True`` is returned if it is an
105 open file descriptor, ``False`` otherwise.
106
Georg Brandl116aa622007-08-15 14:28:22 +0000107
108.. function:: lexists(path)
109
110 Return ``True`` if *path* refers to an existing path. Returns ``True`` for
111 broken symbolic links. Equivalent to :func:`exists` on platforms lacking
112 :func:`os.lstat`.
113
Georg Brandl116aa622007-08-15 14:28:22 +0000114
115.. function:: expanduser(path)
116
117 On Unix and Windows, return the argument with an initial component of ``~`` or
118 ``~user`` replaced by that *user*'s home directory.
119
120 .. index:: module: pwd
121
122 On Unix, an initial ``~`` is replaced by the environment variable :envvar:`HOME`
123 if it is set; otherwise the current user's home directory is looked up in the
124 password directory through the built-in module :mod:`pwd`. An initial ``~user``
125 is looked up directly in the password directory.
126
127 On Windows, :envvar:`HOME` and :envvar:`USERPROFILE` will be used if set,
128 otherwise a combination of :envvar:`HOMEPATH` and :envvar:`HOMEDRIVE` will be
129 used. An initial ``~user`` is handled by stripping the last directory component
130 from the created user path derived above.
131
132 If the expansion fails or if the path does not begin with a tilde, the path is
133 returned unchanged.
134
135
136.. function:: expandvars(path)
137
138 Return the argument with environment variables expanded. Substrings of the form
139 ``$name`` or ``${name}`` are replaced by the value of environment variable
140 *name*. Malformed variable names and references to non-existing variables are
141 left unchanged.
142
143 On Windows, ``%name%`` expansions are supported in addition to ``$name`` and
144 ``${name}``.
145
146
147.. function:: getatime(path)
148
149 Return the time of last access of *path*. The return value is a number giving
150 the number of seconds since the epoch (see the :mod:`time` module). Raise
Andrew Svetlov618c2e12012-12-15 22:59:24 +0200151 :exc:`OSError` if the file does not exist or is inaccessible.
Georg Brandl116aa622007-08-15 14:28:22 +0000152
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200153 If :func:`os.stat_float_times` returns ``True``, the result is a floating point
Georg Brandl55ac8f02007-09-01 13:51:09 +0000154 number.
Georg Brandl116aa622007-08-15 14:28:22 +0000155
156
157.. function:: getmtime(path)
158
159 Return the time of last modification of *path*. The return value is a number
160 giving the number of seconds since the epoch (see the :mod:`time` module).
Andrew Svetlov618c2e12012-12-15 22:59:24 +0200161 Raise :exc:`OSError` if the file does not exist or is inaccessible.
Georg Brandl116aa622007-08-15 14:28:22 +0000162
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200163 If :func:`os.stat_float_times` returns ``True``, the result is a floating point
Georg Brandl55ac8f02007-09-01 13:51:09 +0000164 number.
Georg Brandl116aa622007-08-15 14:28:22 +0000165
166
167.. function:: getctime(path)
168
169 Return the system's ctime which, on some systems (like Unix) is the time of the
Georg Brandlf6324942013-10-06 09:52:55 +0200170 last metadata change, and, on others (like Windows), is the creation time for *path*.
Georg Brandl116aa622007-08-15 14:28:22 +0000171 The return value is a number giving the number of seconds since the epoch (see
Andrew Svetlov618c2e12012-12-15 22:59:24 +0200172 the :mod:`time` module). Raise :exc:`OSError` if the file does not exist or
Georg Brandl116aa622007-08-15 14:28:22 +0000173 is inaccessible.
174
Georg Brandl116aa622007-08-15 14:28:22 +0000175
176.. function:: getsize(path)
177
Andrew Svetlov618c2e12012-12-15 22:59:24 +0200178 Return the size, in bytes, of *path*. Raise :exc:`OSError` if the file does
Georg Brandl116aa622007-08-15 14:28:22 +0000179 not exist or is inaccessible.
180
Georg Brandl116aa622007-08-15 14:28:22 +0000181
182.. function:: isabs(path)
183
Christian Heimesaf98da12008-01-27 15:18:18 +0000184 Return ``True`` if *path* is an absolute pathname. On Unix, that means it
185 begins with a slash, on Windows that it begins with a (back)slash after chopping
186 off a potential drive letter.
Georg Brandl116aa622007-08-15 14:28:22 +0000187
188
189.. function:: isfile(path)
190
191 Return ``True`` if *path* is an existing regular file. This follows symbolic
192 links, so both :func:`islink` and :func:`isfile` can be true for the same path.
193
194
195.. function:: isdir(path)
196
197 Return ``True`` if *path* is an existing directory. This follows symbolic
198 links, so both :func:`islink` and :func:`isdir` can be true for the same path.
199
200
201.. function:: islink(path)
202
203 Return ``True`` if *path* refers to a directory entry that is a symbolic link.
Benjamin Petersonf6f78e12014-06-22 17:59:35 -0700204 Always ``False`` if symbolic links are not supported by the python runtime.
Georg Brandl116aa622007-08-15 14:28:22 +0000205
206
207.. function:: ismount(path)
208
Larry Hastings3732ed22014-03-15 21:13:56 -0700209 Return ``True`` if pathname *path* is a :dfn:`mount point`: a point in a
210 file system where a different file system has been mounted. On POSIX, the
211 function checks whether *path*'s parent, :file:`path/..`, is on a different
212 device than *path*, or whether :file:`path/..` and *path* point to the same
213 i-node on the same device --- this should detect mount points for all Unix
214 and POSIX variants. On Windows, a drive letter root and a share UNC are
215 always mount points, and for any other path ``GetVolumePathName`` is called
216 to see if it is different from the input path.
217
218 .. versionadded:: 3.4
219 Support for detecting non-root mount points on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000220
221
Zachary Warea13dab42014-10-10 16:03:14 -0500222.. function:: join(path, *paths)
Georg Brandl116aa622007-08-15 14:28:22 +0000223
Zachary Warea13dab42014-10-10 16:03:14 -0500224 Join one or more path components intelligently. The return value is the
225 concatenation of *path* and any members of *\*paths* with exactly one
226 directory separator (``os.sep``) following each non-empty part except the
227 last, meaning that the result will only end in a separator if the last
228 part is empty. If a component is an absolute path, all previous
229 components are thrown away and joining continues from the absolute path
230 component.
231
232 On Windows, the drive letter is not reset when an absolute path component
233 (e.g., ``r'\foo'``) is encountered. If a component contains a drive
234 letter, all previous components are thrown away and the drive letter is
235 reset. Note that since there is a current directory for each drive,
236 ``os.path.join("c:", "foo")`` represents a path relative to the current
237 directory on drive :file:`C:` (:file:`c:foo`), not :file:`c:\\foo`.
Georg Brandl116aa622007-08-15 14:28:22 +0000238
239
240.. function:: normcase(path)
241
Benjamin Petersond23f8222009-04-05 19:13:16 +0000242 Normalize the case of a pathname. On Unix and Mac OS X, this returns the
243 path unchanged; on case-insensitive filesystems, it converts the path to
244 lowercase. On Windows, it also converts forward slashes to backward slashes.
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000245 Raise a TypeError if the type of *path* is not ``str`` or ``bytes``.
Georg Brandl116aa622007-08-15 14:28:22 +0000246
247
248.. function:: normpath(path)
249
Terry Jan Reedyec6e1322013-03-17 15:21:26 -0400250 Normalize a pathname by collapsing redundant separators and up-level
251 references so that ``A//B``, ``A/B/``, ``A/./B`` and ``A/foo/../B`` all
252 become ``A/B``. This string manipulation may change the meaning of a path
253 that contains symbolic links. On Windows, it converts forward slashes to
Terry Jan Reedyf3460412013-03-17 15:28:10 -0400254 backward slashes. To normalize case, use :func:`normcase`.
Georg Brandl116aa622007-08-15 14:28:22 +0000255
256
257.. function:: realpath(path)
258
259 Return the canonical path of the specified filename, eliminating any symbolic
260 links encountered in the path (if they are supported by the operating system).
261
Georg Brandl116aa622007-08-15 14:28:22 +0000262
Benjamin Peterson409a1be2014-03-20 12:39:53 -0500263.. function:: relpath(path, start=os.curdir)
Georg Brandl116aa622007-08-15 14:28:22 +0000264
R David Murrayce10fab2013-07-12 17:43:11 -0400265 Return a relative filepath to *path* either from the current directory or
266 from an optional *start* directory. This is a path computation: the
267 filesystem is not accessed to confirm the existence or nature of *path* or
268 *start*.
Georg Brandl116aa622007-08-15 14:28:22 +0000269
Benjamin Petersonf650e462010-05-06 23:03:05 +0000270 *start* defaults to :attr:`os.curdir`.
271
Antoine Pitrouf10f1622010-12-12 20:17:29 +0000272 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000273
Georg Brandl116aa622007-08-15 14:28:22 +0000274
275.. function:: samefile(path1, path2)
276
Brian Curtind40e6f72010-07-08 21:39:08 +0000277 Return ``True`` if both pathname arguments refer to the same file or directory.
Larry Hastings3732ed22014-03-15 21:13:56 -0700278 This is determined by the device number and i-node number and raises an
Brian Curtind40e6f72010-07-08 21:39:08 +0000279 exception if a :func:`os.stat` call on either pathname fails.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000280
Antoine Pitrouf10f1622010-12-12 20:17:29 +0000281 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000282
Georg Brandlb3823372010-07-10 08:58:37 +0000283 .. versionchanged:: 3.2
284 Added Windows support.
Brian Curtinc7395692010-07-09 15:15:09 +0000285
Brian Curtin490b32a2012-12-26 07:03:03 -0600286 .. versionchanged:: 3.4
287 Windows now uses the same implementation as all other platforms.
288
Georg Brandl116aa622007-08-15 14:28:22 +0000289
290.. function:: sameopenfile(fp1, fp2)
291
292 Return ``True`` if the file descriptors *fp1* and *fp2* refer to the same file.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000293
Brian Curtin62857742010-09-06 17:07:27 +0000294 Availability: Unix, Windows.
295
Georg Brandl61063cc2012-06-24 22:48:30 +0200296 .. versionchanged:: 3.2
297 Added Windows support.
Georg Brandl116aa622007-08-15 14:28:22 +0000298
299
300.. function:: samestat(stat1, stat2)
301
302 Return ``True`` if the stat tuples *stat1* and *stat2* refer to the same file.
Serhiy Storchakadab83542013-10-13 20:12:43 +0300303 These structures may have been returned by :func:`os.fstat`,
304 :func:`os.lstat`, or :func:`os.stat`. This function implements the
305 underlying comparison used by :func:`samefile` and :func:`sameopenfile`.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000306
Brian Curtin490b32a2012-12-26 07:03:03 -0600307 Availability: Unix, Windows.
308
309 .. versionchanged:: 3.4
310 Added Windows support.
Georg Brandl116aa622007-08-15 14:28:22 +0000311
312
313.. function:: split(path)
314
Georg Brandl539c1652010-10-14 06:46:08 +0000315 Split the pathname *path* into a pair, ``(head, tail)`` where *tail* is the
316 last pathname component and *head* is everything leading up to that. The
317 *tail* part will never contain a slash; if *path* ends in a slash, *tail*
318 will be empty. If there is no slash in *path*, *head* will be empty. If
319 *path* is empty, both *head* and *tail* are empty. Trailing slashes are
320 stripped from *head* unless it is the root (one or more slashes only). In
321 all cases, ``join(head, tail)`` returns a path to the same location as *path*
Chris Jerdonek0b502ff2012-11-25 20:38:01 -0800322 (but the strings may differ). Also see the functions :func:`dirname` and
323 :func:`basename`.
Georg Brandl116aa622007-08-15 14:28:22 +0000324
325
326.. function:: splitdrive(path)
327
328 Split the pathname *path* into a pair ``(drive, tail)`` where *drive* is either
Mark Hammond5a607a32009-05-06 08:04:54 +0000329 a mount point or the empty string. On systems which do not use drive
Georg Brandl116aa622007-08-15 14:28:22 +0000330 specifications, *drive* will always be the empty string. In all cases, ``drive
331 + tail`` will be the same as *path*.
332
Mark Hammond5a607a32009-05-06 08:04:54 +0000333 On Windows, splits a pathname into drive/UNC sharepoint and relative path.
334
335 If the path contains a drive letter, drive will contain everything
336 up to and including the colon.
337 e.g. ``splitdrive("c:/dir")`` returns ``("c:", "/dir")``
338
339 If the path contains a UNC path, drive will contain the host name
340 and share, up to but not including the fourth separator.
341 e.g. ``splitdrive("//host/computer/dir")`` returns ``("//host/computer", "/dir")``
342
Georg Brandl116aa622007-08-15 14:28:22 +0000343
344.. function:: splitext(path)
345
346 Split the pathname *path* into a pair ``(root, ext)`` such that ``root + ext ==
347 path``, and *ext* is empty or begins with a period and contains at most one
348 period. Leading periods on the basename are ignored; ``splitext('.cshrc')``
349 returns ``('.cshrc', '')``.
350
Georg Brandl116aa622007-08-15 14:28:22 +0000351
352.. function:: splitunc(path)
353
Mark Hammond5a607a32009-05-06 08:04:54 +0000354 .. deprecated:: 3.1
355 Use *splitdrive* instead.
356
Georg Brandl116aa622007-08-15 14:28:22 +0000357 Split the pathname *path* into a pair ``(unc, rest)`` so that *unc* is the UNC
358 mount point (such as ``r'\\host\mount'``), if present, and *rest* the rest of
359 the path (such as ``r'\path\file.ext'``). For paths containing drive letters,
Benjamin Petersonf650e462010-05-06 23:03:05 +0000360 *unc* will always be the empty string.
361
362 Availability: Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000363
364
Georg Brandl116aa622007-08-15 14:28:22 +0000365.. data:: supports_unicode_filenames
366
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200367 ``True`` if arbitrary Unicode strings can be used as file names (within limitations
Victor Stinnerb55e4982010-09-11 00:22:12 +0000368 imposed by the file system).