blob: b1ad2141f89730179fe61c413ce6f2da0e34cb9e [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
69.. function:: commonprefix(list)
70
71 Return the longest path prefix (taken character-by-character) that is a prefix
72 of all paths in *list*. If *list* is empty, return the empty string (``''``).
73 Note that this may return invalid paths because it works a character at a time.
74
75
76.. function:: dirname(path)
77
Chris Jerdonek0b502ff2012-11-25 20:38:01 -080078 Return the directory name of pathname *path*. This is the first element of
79 the pair returned by passing *path* to the function :func:`split`.
Georg Brandl116aa622007-08-15 14:28:22 +000080
81
82.. function:: exists(path)
83
Richard Oudkerk2240ac12012-07-06 12:05:32 +010084 Return ``True`` if *path* refers to an existing path or an open
85 file descriptor. Returns ``False`` for broken symbolic links. On
86 some platforms, this function may return ``False`` if permission is
87 not granted to execute :func:`os.stat` on the requested file, even
Georg Brandl116aa622007-08-15 14:28:22 +000088 if the *path* physically exists.
89
Richard Oudkerk2240ac12012-07-06 12:05:32 +010090 .. versionchanged:: 3.3
91 *path* can now be an integer: ``True`` is returned if it is an
92 open file descriptor, ``False`` otherwise.
93
Georg Brandl116aa622007-08-15 14:28:22 +000094
95.. function:: lexists(path)
96
97 Return ``True`` if *path* refers to an existing path. Returns ``True`` for
98 broken symbolic links. Equivalent to :func:`exists` on platforms lacking
99 :func:`os.lstat`.
100
Georg Brandl116aa622007-08-15 14:28:22 +0000101
102.. function:: expanduser(path)
103
104 On Unix and Windows, return the argument with an initial component of ``~`` or
105 ``~user`` replaced by that *user*'s home directory.
106
107 .. index:: module: pwd
108
109 On Unix, an initial ``~`` is replaced by the environment variable :envvar:`HOME`
110 if it is set; otherwise the current user's home directory is looked up in the
111 password directory through the built-in module :mod:`pwd`. An initial ``~user``
112 is looked up directly in the password directory.
113
114 On Windows, :envvar:`HOME` and :envvar:`USERPROFILE` will be used if set,
115 otherwise a combination of :envvar:`HOMEPATH` and :envvar:`HOMEDRIVE` will be
116 used. An initial ``~user`` is handled by stripping the last directory component
117 from the created user path derived above.
118
119 If the expansion fails or if the path does not begin with a tilde, the path is
120 returned unchanged.
121
122
123.. function:: expandvars(path)
124
125 Return the argument with environment variables expanded. Substrings of the form
126 ``$name`` or ``${name}`` are replaced by the value of environment variable
127 *name*. Malformed variable names and references to non-existing variables are
128 left unchanged.
129
130 On Windows, ``%name%`` expansions are supported in addition to ``$name`` and
131 ``${name}``.
132
133
134.. function:: getatime(path)
135
136 Return the time of last access of *path*. The return value is a number giving
137 the number of seconds since the epoch (see the :mod:`time` module). Raise
Andrew Svetlov618c2e12012-12-15 22:59:24 +0200138 :exc:`OSError` if the file does not exist or is inaccessible.
Georg Brandl116aa622007-08-15 14:28:22 +0000139
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200140 If :func:`os.stat_float_times` returns ``True``, the result is a floating point
Georg Brandl55ac8f02007-09-01 13:51:09 +0000141 number.
Georg Brandl116aa622007-08-15 14:28:22 +0000142
143
144.. function:: getmtime(path)
145
146 Return the time of last modification of *path*. The return value is a number
147 giving the number of seconds since the epoch (see the :mod:`time` module).
Andrew Svetlov618c2e12012-12-15 22:59:24 +0200148 Raise :exc:`OSError` if the file does not exist or is inaccessible.
Georg Brandl116aa622007-08-15 14:28:22 +0000149
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200150 If :func:`os.stat_float_times` returns ``True``, the result is a floating point
Georg Brandl55ac8f02007-09-01 13:51:09 +0000151 number.
Georg Brandl116aa622007-08-15 14:28:22 +0000152
153
154.. function:: getctime(path)
155
156 Return the system's ctime which, on some systems (like Unix) is the time of the
Georg Brandlf6324942013-10-06 09:52:55 +0200157 last metadata change, and, on others (like Windows), is the creation time for *path*.
Georg Brandl116aa622007-08-15 14:28:22 +0000158 The return value is a number giving the number of seconds since the epoch (see
Andrew Svetlov618c2e12012-12-15 22:59:24 +0200159 the :mod:`time` module). Raise :exc:`OSError` if the file does not exist or
Georg Brandl116aa622007-08-15 14:28:22 +0000160 is inaccessible.
161
Georg Brandl116aa622007-08-15 14:28:22 +0000162
163.. function:: getsize(path)
164
Andrew Svetlov618c2e12012-12-15 22:59:24 +0200165 Return the size, in bytes, of *path*. Raise :exc:`OSError` if the file does
Georg Brandl116aa622007-08-15 14:28:22 +0000166 not exist or is inaccessible.
167
Georg Brandl116aa622007-08-15 14:28:22 +0000168
169.. function:: isabs(path)
170
Christian Heimesaf98da12008-01-27 15:18:18 +0000171 Return ``True`` if *path* is an absolute pathname. On Unix, that means it
172 begins with a slash, on Windows that it begins with a (back)slash after chopping
173 off a potential drive letter.
Georg Brandl116aa622007-08-15 14:28:22 +0000174
175
176.. function:: isfile(path)
177
178 Return ``True`` if *path* is an existing regular file. This follows symbolic
179 links, so both :func:`islink` and :func:`isfile` can be true for the same path.
180
181
182.. function:: isdir(path)
183
184 Return ``True`` if *path* is an existing directory. This follows symbolic
185 links, so both :func:`islink` and :func:`isdir` can be true for the same path.
186
187
188.. function:: islink(path)
189
190 Return ``True`` if *path* refers to a directory entry that is a symbolic link.
191 Always ``False`` if symbolic links are not supported.
192
193
194.. function:: ismount(path)
195
196 Return ``True`` if pathname *path* is a :dfn:`mount point`: a point in a file
197 system where a different file system has been mounted. The function checks
198 whether *path*'s parent, :file:`path/..`, is on a different device than *path*,
199 or whether :file:`path/..` and *path* point to the same i-node on the same
200 device --- this should detect mount points for all Unix and POSIX variants.
201
202
203.. function:: join(path1[, path2[, ...]])
204
205 Join one or more path components intelligently. If any component is an absolute
206 path, all previous components (on Windows, including the previous drive letter,
207 if there was one) are thrown away, and joining continues. The return value is
208 the concatenation of *path1*, and optionally *path2*, etc., with exactly one
R David Murray24eb4bc2011-06-23 21:26:13 -0400209 directory separator (``os.sep``) following each non-empty part except the last.
210 (This means that an empty last part will result in a path that ends with a
211 separator.) Note that on Windows, since there is a current directory for
212 each drive, ``os.path.join("c:", "foo")`` represents a path relative to the
213 current directory on drive :file:`C:` (:file:`c:foo`), not :file:`c:\\foo`.
Georg Brandl116aa622007-08-15 14:28:22 +0000214
215
216.. function:: normcase(path)
217
Benjamin Petersond23f8222009-04-05 19:13:16 +0000218 Normalize the case of a pathname. On Unix and Mac OS X, this returns the
219 path unchanged; on case-insensitive filesystems, it converts the path to
220 lowercase. On Windows, it also converts forward slashes to backward slashes.
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000221 Raise a TypeError if the type of *path* is not ``str`` or ``bytes``.
Georg Brandl116aa622007-08-15 14:28:22 +0000222
223
224.. function:: normpath(path)
225
Terry Jan Reedyec6e1322013-03-17 15:21:26 -0400226 Normalize a pathname by collapsing redundant separators and up-level
227 references so that ``A//B``, ``A/B/``, ``A/./B`` and ``A/foo/../B`` all
228 become ``A/B``. This string manipulation may change the meaning of a path
229 that contains symbolic links. On Windows, it converts forward slashes to
Terry Jan Reedyf3460412013-03-17 15:28:10 -0400230 backward slashes. To normalize case, use :func:`normcase`.
Georg Brandl116aa622007-08-15 14:28:22 +0000231
232
233.. function:: realpath(path)
234
235 Return the canonical path of the specified filename, eliminating any symbolic
236 links encountered in the path (if they are supported by the operating system).
237
Georg Brandl116aa622007-08-15 14:28:22 +0000238
Georg Brandl18244152009-09-02 20:34:52 +0000239.. function:: relpath(path, start=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000240
R David Murrayce10fab2013-07-12 17:43:11 -0400241 Return a relative filepath to *path* either from the current directory or
242 from an optional *start* directory. This is a path computation: the
243 filesystem is not accessed to confirm the existence or nature of *path* or
244 *start*.
Georg Brandl116aa622007-08-15 14:28:22 +0000245
Benjamin Petersonf650e462010-05-06 23:03:05 +0000246 *start* defaults to :attr:`os.curdir`.
247
Antoine Pitrouf10f1622010-12-12 20:17:29 +0000248 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000249
Georg Brandl116aa622007-08-15 14:28:22 +0000250
251.. function:: samefile(path1, path2)
252
Brian Curtind40e6f72010-07-08 21:39:08 +0000253 Return ``True`` if both pathname arguments refer to the same file or directory.
R David Murray2167e292014-02-12 00:02:34 -0500254 This is determined by the device number and i-node number and raises an
Brian Curtind40e6f72010-07-08 21:39:08 +0000255 exception if a :func:`os.stat` call on either pathname fails.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000256
Antoine Pitrouf10f1622010-12-12 20:17:29 +0000257 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000258
Georg Brandlb3823372010-07-10 08:58:37 +0000259 .. versionchanged:: 3.2
260 Added Windows support.
Brian Curtinc7395692010-07-09 15:15:09 +0000261
Brian Curtin490b32a2012-12-26 07:03:03 -0600262 .. versionchanged:: 3.4
263 Windows now uses the same implementation as all other platforms.
264
Georg Brandl116aa622007-08-15 14:28:22 +0000265
266.. function:: sameopenfile(fp1, fp2)
267
268 Return ``True`` if the file descriptors *fp1* and *fp2* refer to the same file.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000269
Brian Curtin62857742010-09-06 17:07:27 +0000270 Availability: Unix, Windows.
271
Georg Brandl61063cc2012-06-24 22:48:30 +0200272 .. versionchanged:: 3.2
273 Added Windows support.
Georg Brandl116aa622007-08-15 14:28:22 +0000274
275
276.. function:: samestat(stat1, stat2)
277
278 Return ``True`` if the stat tuples *stat1* and *stat2* refer to the same file.
Serhiy Storchakadab83542013-10-13 20:12:43 +0300279 These structures may have been returned by :func:`os.fstat`,
280 :func:`os.lstat`, or :func:`os.stat`. This function implements the
281 underlying comparison used by :func:`samefile` and :func:`sameopenfile`.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000282
Brian Curtin490b32a2012-12-26 07:03:03 -0600283 Availability: Unix, Windows.
284
285 .. versionchanged:: 3.4
286 Added Windows support.
Georg Brandl116aa622007-08-15 14:28:22 +0000287
288
289.. function:: split(path)
290
Georg Brandl539c1652010-10-14 06:46:08 +0000291 Split the pathname *path* into a pair, ``(head, tail)`` where *tail* is the
292 last pathname component and *head* is everything leading up to that. The
293 *tail* part will never contain a slash; if *path* ends in a slash, *tail*
294 will be empty. If there is no slash in *path*, *head* will be empty. If
295 *path* is empty, both *head* and *tail* are empty. Trailing slashes are
296 stripped from *head* unless it is the root (one or more slashes only). In
297 all cases, ``join(head, tail)`` returns a path to the same location as *path*
Chris Jerdonek0b502ff2012-11-25 20:38:01 -0800298 (but the strings may differ). Also see the functions :func:`dirname` and
299 :func:`basename`.
Georg Brandl116aa622007-08-15 14:28:22 +0000300
301
302.. function:: splitdrive(path)
303
304 Split the pathname *path* into a pair ``(drive, tail)`` where *drive* is either
Mark Hammond5a607a32009-05-06 08:04:54 +0000305 a mount point or the empty string. On systems which do not use drive
Georg Brandl116aa622007-08-15 14:28:22 +0000306 specifications, *drive* will always be the empty string. In all cases, ``drive
307 + tail`` will be the same as *path*.
308
Mark Hammond5a607a32009-05-06 08:04:54 +0000309 On Windows, splits a pathname into drive/UNC sharepoint and relative path.
310
311 If the path contains a drive letter, drive will contain everything
312 up to and including the colon.
313 e.g. ``splitdrive("c:/dir")`` returns ``("c:", "/dir")``
314
315 If the path contains a UNC path, drive will contain the host name
316 and share, up to but not including the fourth separator.
317 e.g. ``splitdrive("//host/computer/dir")`` returns ``("//host/computer", "/dir")``
318
Georg Brandl116aa622007-08-15 14:28:22 +0000319
320.. function:: splitext(path)
321
322 Split the pathname *path* into a pair ``(root, ext)`` such that ``root + ext ==
323 path``, and *ext* is empty or begins with a period and contains at most one
324 period. Leading periods on the basename are ignored; ``splitext('.cshrc')``
325 returns ``('.cshrc', '')``.
326
Georg Brandl116aa622007-08-15 14:28:22 +0000327
328.. function:: splitunc(path)
329
Mark Hammond5a607a32009-05-06 08:04:54 +0000330 .. deprecated:: 3.1
331 Use *splitdrive* instead.
332
Georg Brandl116aa622007-08-15 14:28:22 +0000333 Split the pathname *path* into a pair ``(unc, rest)`` so that *unc* is the UNC
334 mount point (such as ``r'\\host\mount'``), if present, and *rest* the rest of
335 the path (such as ``r'\path\file.ext'``). For paths containing drive letters,
Benjamin Petersonf650e462010-05-06 23:03:05 +0000336 *unc* will always be the empty string.
337
338 Availability: Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000339
340
Georg Brandl116aa622007-08-15 14:28:22 +0000341.. data:: supports_unicode_filenames
342
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200343 ``True`` if arbitrary Unicode strings can be used as file names (within limitations
Victor Stinnerb55e4982010-09-11 00:22:12 +0000344 imposed by the file system).