blob: a167e3b885fdfcc564510768aa5057b9f64b9fc4 [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
Victor Stinnerd7538dd2018-12-14 13:37:26 +01007**Source code:** :source:`Lib/posixpath.py` (for POSIX) and
8:source:`Lib/ntpath.py` (for Windows NT).
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04009
Georg Brandl116aa622007-08-15 14:28:22 +000010.. index:: single: path; operations
11
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040012--------------
13
Georg Brandl116aa622007-08-15 14:28:22 +000014This module implements some useful functions on pathnames. To read or
15write files see :func:`open`, and for accessing the filesystem see the
Martin v. Löwis651423c2008-10-07 07:03:04 +000016:mod:`os` module. The path parameters can be passed as either strings,
17or bytes. Applications are encouraged to represent file names as
18(Unicode) character strings. Unfortunately, some file names may not be
19representable as strings on Unix, so applications that need to support
20arbitrary file names on Unix should use bytes objects to represent
21path names. Vice versa, using bytes objects cannot represent all file
22names on Windows (in the standard ``mbcs`` encoding), hence Windows
23applications should use string objects to access all files.
Georg Brandl116aa622007-08-15 14:28:22 +000024
R David Murraya4e700c2013-01-06 16:13:10 -050025Unlike a unix shell, Python does not do any *automatic* path expansions.
26Functions such as :func:`expanduser` and :func:`expandvars` can be invoked
27explicitly when an application desires shell-like path expansion. (See also
28the :mod:`glob` module.)
29
Antoine Pitrou31119e42013-11-22 17:38:12 +010030
31.. seealso::
32 The :mod:`pathlib` module offers high-level path objects.
33
34
Georg Brandl76e55382008-10-08 16:34:57 +000035.. note::
36
37 All of these functions accept either only bytes or only string objects as
38 their parameters. The result is an object of the same type, if a path or
39 file name is returned.
40
Georg Brandl116aa622007-08-15 14:28:22 +000041
Benjamin Petersond23f8222009-04-05 19:13:16 +000042.. note::
43
44 Since different operating systems have different path name conventions, there
45 are several versions of this module in the standard library. The
46 :mod:`os.path` module is always the path module suitable for the operating
47 system Python is running on, and therefore usable for local paths. However,
48 you can also import and use the individual modules if you want to manipulate
49 a path that is *always* in one of the different formats. They all have the
50 same interface:
51
52 * :mod:`posixpath` for UNIX-style paths
53 * :mod:`ntpath` for Windows paths
Benjamin Petersond23f8222009-04-05 19:13:16 +000054
55
Serhiy Storchaka0185f342018-09-18 11:28:51 +030056.. versionchanged:: 3.8
57
58 :func:`exists`, :func:`lexists`, :func:`isdir`, :func:`isfile`,
59 :func:`islink`, and :func:`ismount` now return ``False`` instead of
60 raising an exception for paths that contain characters or bytes
61 unrepresentable at the OS level.
62
63
Georg Brandl116aa622007-08-15 14:28:22 +000064.. function:: abspath(path)
65
66 Return a normalized absolutized version of the pathname *path*. On most
Chris Jerdonek0b502ff2012-11-25 20:38:01 -080067 platforms, this is equivalent to calling the function :func:`normpath` as
68 follows: ``normpath(join(os.getcwd(), path))``.
Georg Brandl116aa622007-08-15 14:28:22 +000069
Brett Cannon6fa7aad2016-09-06 15:55:02 -070070 .. versionchanged:: 3.6
71 Accepts a :term:`path-like object`.
72
Georg Brandl116aa622007-08-15 14:28:22 +000073
74.. function:: basename(path)
75
Chris Jerdonek0b502ff2012-11-25 20:38:01 -080076 Return the base name of pathname *path*. This is the second element of the
77 pair returned by passing *path* to the function :func:`split`. Note that
78 the result of this function is different
Georg Brandl116aa622007-08-15 14:28:22 +000079 from the Unix :program:`basename` program; where :program:`basename` for
80 ``'/foo/bar/'`` returns ``'bar'``, the :func:`basename` function returns an
81 empty string (``''``).
82
Brett Cannon6fa7aad2016-09-06 15:55:02 -070083 .. versionchanged:: 3.6
84 Accepts a :term:`path-like object`.
85
Georg Brandl116aa622007-08-15 14:28:22 +000086
Serhiy Storchaka38220932015-03-31 15:31:53 +030087.. function:: commonpath(paths)
88
89 Return the longest common sub-path of each pathname in the sequence
90 *paths*. Raise ValueError if *paths* contains both absolute and relative
91 pathnames, or if *paths* is empty. Unlike :func:`commonprefix`, this
92 returns a valid path.
93
Cheryl Sabella2d6097d2018-10-12 10:55:20 -040094 .. availability:: Unix, Windows.
Serhiy Storchaka38220932015-03-31 15:31:53 +030095
96 .. versionadded:: 3.5
97
Brett Cannon6fa7aad2016-09-06 15:55:02 -070098 .. versionchanged:: 3.6
99 Accepts a sequence of :term:`path-like objects <path-like object>`.
100
Serhiy Storchaka38220932015-03-31 15:31:53 +0300101
Georg Brandl116aa622007-08-15 14:28:22 +0000102.. function:: commonprefix(list)
103
Serhiy Storchaka38220932015-03-31 15:31:53 +0300104 Return the longest path prefix (taken character-by-character) that is a
105 prefix of all paths in *list*. If *list* is empty, return the empty string
Yury Selivanov80ac11f2015-08-17 23:43:43 -0400106 (``''``).
107
108 .. note::
109
110 This function may return invalid paths because it works a
111 character at a time. To obtain a valid path, see
112 :func:`commonpath`.
113
114 ::
115
Yury Selivanovde115612015-08-19 09:53:28 -0400116 >>> os.path.commonprefix(['/usr/lib', '/usr/local/lib'])
117 '/usr/l'
Yury Selivanov80ac11f2015-08-17 23:43:43 -0400118
Yury Selivanovde115612015-08-19 09:53:28 -0400119 >>> os.path.commonpath(['/usr/lib', '/usr/local/lib'])
120 '/usr'
Georg Brandl116aa622007-08-15 14:28:22 +0000121
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700122 .. versionchanged:: 3.6
123 Accepts a :term:`path-like object`.
124
Georg Brandl116aa622007-08-15 14:28:22 +0000125
126.. function:: dirname(path)
127
Chris Jerdonek0b502ff2012-11-25 20:38:01 -0800128 Return the directory name of pathname *path*. This is the first element of
129 the pair returned by passing *path* to the function :func:`split`.
Georg Brandl116aa622007-08-15 14:28:22 +0000130
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700131 .. versionchanged:: 3.6
132 Accepts a :term:`path-like object`.
133
Georg Brandl116aa622007-08-15 14:28:22 +0000134
135.. function:: exists(path)
136
Richard Oudkerk2240ac12012-07-06 12:05:32 +0100137 Return ``True`` if *path* refers to an existing path or an open
138 file descriptor. Returns ``False`` for broken symbolic links. On
139 some platforms, this function may return ``False`` if permission is
140 not granted to execute :func:`os.stat` on the requested file, even
Georg Brandl116aa622007-08-15 14:28:22 +0000141 if the *path* physically exists.
142
Richard Oudkerk2240ac12012-07-06 12:05:32 +0100143 .. versionchanged:: 3.3
144 *path* can now be an integer: ``True`` is returned if it is an
145 open file descriptor, ``False`` otherwise.
146
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700147 .. versionchanged:: 3.6
148 Accepts a :term:`path-like object`.
149
Georg Brandl116aa622007-08-15 14:28:22 +0000150
151.. function:: lexists(path)
152
153 Return ``True`` if *path* refers to an existing path. Returns ``True`` for
154 broken symbolic links. Equivalent to :func:`exists` on platforms lacking
155 :func:`os.lstat`.
156
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700157 .. versionchanged:: 3.6
158 Accepts a :term:`path-like object`.
159
Georg Brandl116aa622007-08-15 14:28:22 +0000160
Serhiy Storchaka913876d2018-10-28 13:41:26 +0200161.. index:: single: ~ (tilde); home directory expansion
Serhiy Storchakaddb961d2018-10-26 09:00:49 +0300162
Georg Brandl116aa622007-08-15 14:28:22 +0000163.. function:: expanduser(path)
164
165 On Unix and Windows, return the argument with an initial component of ``~`` or
166 ``~user`` replaced by that *user*'s home directory.
167
168 .. index:: module: pwd
169
170 On Unix, an initial ``~`` is replaced by the environment variable :envvar:`HOME`
171 if it is set; otherwise the current user's home directory is looked up in the
172 password directory through the built-in module :mod:`pwd`. An initial ``~user``
173 is looked up directly in the password directory.
174
Steve Dower8ef864d2019-03-12 15:15:26 -0700175 On Windows, :envvar:`USERPROFILE` will be used if set, otherwise a combination
176 of :envvar:`HOMEPATH` and :envvar:`HOMEDRIVE` will be used. An initial
177 ``~user`` is handled by stripping the last directory component from the created
178 user path derived above.
Georg Brandl116aa622007-08-15 14:28:22 +0000179
180 If the expansion fails or if the path does not begin with a tilde, the path is
181 returned unchanged.
182
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700183 .. versionchanged:: 3.6
184 Accepts a :term:`path-like object`.
185
Steve Dower8ef864d2019-03-12 15:15:26 -0700186 .. versionchanged:: 3.8
187 No longer uses :envvar:`HOME` on Windows.
188
Serhiy Storchakaddb961d2018-10-26 09:00:49 +0300189.. index::
Serhiy Storchaka913876d2018-10-28 13:41:26 +0200190 single: $ (dollar); environment variables expansion
191 single: % (percent); environment variables expansion (Windows)
Georg Brandl116aa622007-08-15 14:28:22 +0000192
193.. function:: expandvars(path)
194
195 Return the argument with environment variables expanded. Substrings of the form
196 ``$name`` or ``${name}`` are replaced by the value of environment variable
197 *name*. Malformed variable names and references to non-existing variables are
198 left unchanged.
199
200 On Windows, ``%name%`` expansions are supported in addition to ``$name`` and
201 ``${name}``.
202
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700203 .. versionchanged:: 3.6
204 Accepts a :term:`path-like object`.
205
Georg Brandl116aa622007-08-15 14:28:22 +0000206
207.. function:: getatime(path)
208
Victor Stinner01b5aab2017-10-24 02:02:00 -0700209 Return the time of last access of *path*. The return value is a floating point number giving
Georg Brandl116aa622007-08-15 14:28:22 +0000210 the number of seconds since the epoch (see the :mod:`time` module). Raise
Andrew Svetlov618c2e12012-12-15 22:59:24 +0200211 :exc:`OSError` if the file does not exist or is inaccessible.
Georg Brandl116aa622007-08-15 14:28:22 +0000212
Georg Brandl116aa622007-08-15 14:28:22 +0000213
214.. function:: getmtime(path)
215
Victor Stinner01b5aab2017-10-24 02:02:00 -0700216 Return the time of last modification of *path*. The return value is a floating point number
Georg Brandl116aa622007-08-15 14:28:22 +0000217 giving the number of seconds since the epoch (see the :mod:`time` module).
Andrew Svetlov618c2e12012-12-15 22:59:24 +0200218 Raise :exc:`OSError` if the file does not exist or is inaccessible.
Georg Brandl116aa622007-08-15 14:28:22 +0000219
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700220 .. versionchanged:: 3.6
221 Accepts a :term:`path-like object`.
222
Georg Brandl116aa622007-08-15 14:28:22 +0000223
224.. function:: getctime(path)
225
226 Return the system's ctime which, on some systems (like Unix) is the time of the
Georg Brandlf6324942013-10-06 09:52:55 +0200227 last metadata change, and, on others (like Windows), is the creation time for *path*.
Georg Brandl116aa622007-08-15 14:28:22 +0000228 The return value is a number giving the number of seconds since the epoch (see
Andrew Svetlov618c2e12012-12-15 22:59:24 +0200229 the :mod:`time` module). Raise :exc:`OSError` if the file does not exist or
Georg Brandl116aa622007-08-15 14:28:22 +0000230 is inaccessible.
231
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700232 .. versionchanged:: 3.6
233 Accepts a :term:`path-like object`.
234
Georg Brandl116aa622007-08-15 14:28:22 +0000235
236.. function:: getsize(path)
237
Andrew Svetlov618c2e12012-12-15 22:59:24 +0200238 Return the size, in bytes, of *path*. Raise :exc:`OSError` if the file does
Georg Brandl116aa622007-08-15 14:28:22 +0000239 not exist or is inaccessible.
240
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700241 .. versionchanged:: 3.6
242 Accepts a :term:`path-like object`.
243
Georg Brandl116aa622007-08-15 14:28:22 +0000244
245.. function:: isabs(path)
246
Christian Heimesaf98da12008-01-27 15:18:18 +0000247 Return ``True`` if *path* is an absolute pathname. On Unix, that means it
248 begins with a slash, on Windows that it begins with a (back)slash after chopping
249 off a potential drive letter.
Georg Brandl116aa622007-08-15 14:28:22 +0000250
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700251 .. versionchanged:: 3.6
252 Accepts a :term:`path-like object`.
253
Georg Brandl116aa622007-08-15 14:28:22 +0000254
255.. function:: isfile(path)
256
Cheryl Sabellab3dd18d2018-01-14 23:57:51 -0500257 Return ``True`` if *path* is an :func:`existing <exists>` regular file.
258 This follows symbolic links, so both :func:`islink` and :func:`isfile` can
259 be true for the same path.
Georg Brandl116aa622007-08-15 14:28:22 +0000260
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700261 .. versionchanged:: 3.6
262 Accepts a :term:`path-like object`.
263
Georg Brandl116aa622007-08-15 14:28:22 +0000264
265.. function:: isdir(path)
266
Cheryl Sabellab3dd18d2018-01-14 23:57:51 -0500267 Return ``True`` if *path* is an :func:`existing <exists>` directory. This
268 follows symbolic links, so both :func:`islink` and :func:`isdir` can be true
269 for the same path.
Georg Brandl116aa622007-08-15 14:28:22 +0000270
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700271 .. versionchanged:: 3.6
272 Accepts a :term:`path-like object`.
273
Georg Brandl116aa622007-08-15 14:28:22 +0000274
275.. function:: islink(path)
276
Cheryl Sabellab3dd18d2018-01-14 23:57:51 -0500277 Return ``True`` if *path* refers to an :func:`existing <exists>` directory
278 entry that is a symbolic link. Always ``False`` if symbolic links are not
279 supported by the Python runtime.
Georg Brandl116aa622007-08-15 14:28:22 +0000280
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700281 .. versionchanged:: 3.6
282 Accepts a :term:`path-like object`.
283
Georg Brandl116aa622007-08-15 14:28:22 +0000284
285.. function:: ismount(path)
286
Larry Hastings3732ed22014-03-15 21:13:56 -0700287 Return ``True`` if pathname *path* is a :dfn:`mount point`: a point in a
288 file system where a different file system has been mounted. On POSIX, the
Serhiy Storchaka32ebd852019-01-15 10:55:40 +0200289 function checks whether *path*'s parent, :file:`{path}/..`, is on a different
290 device than *path*, or whether :file:`{path}/..` and *path* point to the same
Larry Hastings3732ed22014-03-15 21:13:56 -0700291 i-node on the same device --- this should detect mount points for all Unix
Serhiy Storchaka32ebd852019-01-15 10:55:40 +0200292 and POSIX variants. It is not able to reliably detect bind mounts on the
293 same filesystem. On Windows, a drive letter root and a share UNC are
Larry Hastings3732ed22014-03-15 21:13:56 -0700294 always mount points, and for any other path ``GetVolumePathName`` is called
295 to see if it is different from the input path.
296
297 .. versionadded:: 3.4
298 Support for detecting non-root mount points on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000299
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700300 .. versionchanged:: 3.6
301 Accepts a :term:`path-like object`.
302
Georg Brandl116aa622007-08-15 14:28:22 +0000303
Zachary Warea13dab42014-10-10 16:03:14 -0500304.. function:: join(path, *paths)
Georg Brandl116aa622007-08-15 14:28:22 +0000305
Zachary Warea13dab42014-10-10 16:03:14 -0500306 Join one or more path components intelligently. The return value is the
307 concatenation of *path* and any members of *\*paths* with exactly one
308 directory separator (``os.sep``) following each non-empty part except the
309 last, meaning that the result will only end in a separator if the last
310 part is empty. If a component is an absolute path, all previous
311 components are thrown away and joining continues from the absolute path
312 component.
313
314 On Windows, the drive letter is not reset when an absolute path component
315 (e.g., ``r'\foo'``) is encountered. If a component contains a drive
316 letter, all previous components are thrown away and the drive letter is
317 reset. Note that since there is a current directory for each drive,
318 ``os.path.join("c:", "foo")`` represents a path relative to the current
319 directory on drive :file:`C:` (:file:`c:foo`), not :file:`c:\\foo`.
Georg Brandl116aa622007-08-15 14:28:22 +0000320
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700321 .. versionchanged:: 3.6
322 Accepts a :term:`path-like object` for *path* and *paths*.
323
Georg Brandl116aa622007-08-15 14:28:22 +0000324
325.. function:: normcase(path)
326
Benjamin Petersond23f8222009-04-05 19:13:16 +0000327 Normalize the case of a pathname. On Unix and Mac OS X, this returns the
328 path unchanged; on case-insensitive filesystems, it converts the path to
329 lowercase. On Windows, it also converts forward slashes to backward slashes.
Stéphane Wirtele483f022018-10-26 12:52:11 +0200330 Raise a :exc:`TypeError` if the type of *path* is not ``str`` or ``bytes`` (directly
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700331 or indirectly through the :class:`os.PathLike` interface).
332
333 .. versionchanged:: 3.6
334 Accepts a :term:`path-like object`.
Georg Brandl116aa622007-08-15 14:28:22 +0000335
336
337.. function:: normpath(path)
338
Terry Jan Reedyec6e1322013-03-17 15:21:26 -0400339 Normalize a pathname by collapsing redundant separators and up-level
340 references so that ``A//B``, ``A/B/``, ``A/./B`` and ``A/foo/../B`` all
341 become ``A/B``. This string manipulation may change the meaning of a path
342 that contains symbolic links. On Windows, it converts forward slashes to
Terry Jan Reedyf3460412013-03-17 15:28:10 -0400343 backward slashes. To normalize case, use :func:`normcase`.
Georg Brandl116aa622007-08-15 14:28:22 +0000344
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700345 .. versionchanged:: 3.6
346 Accepts a :term:`path-like object`.
347
Georg Brandl116aa622007-08-15 14:28:22 +0000348
349.. function:: realpath(path)
350
351 Return the canonical path of the specified filename, eliminating any symbolic
352 links encountered in the path (if they are supported by the operating system).
353
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700354 .. versionchanged:: 3.6
355 Accepts a :term:`path-like object`.
356
Georg Brandl116aa622007-08-15 14:28:22 +0000357
Benjamin Peterson409a1be2014-03-20 12:39:53 -0500358.. function:: relpath(path, start=os.curdir)
Georg Brandl116aa622007-08-15 14:28:22 +0000359
R David Murrayce10fab2013-07-12 17:43:11 -0400360 Return a relative filepath to *path* either from the current directory or
361 from an optional *start* directory. This is a path computation: the
362 filesystem is not accessed to confirm the existence or nature of *path* or
363 *start*.
Georg Brandl116aa622007-08-15 14:28:22 +0000364
Benjamin Petersonf650e462010-05-06 23:03:05 +0000365 *start* defaults to :attr:`os.curdir`.
366
Cheryl Sabella2d6097d2018-10-12 10:55:20 -0400367 .. availability:: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000368
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700369 .. versionchanged:: 3.6
370 Accepts a :term:`path-like object`.
371
Georg Brandl116aa622007-08-15 14:28:22 +0000372
373.. function:: samefile(path1, path2)
374
Brian Curtind40e6f72010-07-08 21:39:08 +0000375 Return ``True`` if both pathname arguments refer to the same file or directory.
Larry Hastings3732ed22014-03-15 21:13:56 -0700376 This is determined by the device number and i-node number and raises an
Martin Panter7462b6492015-11-02 03:37:02 +0000377 exception if an :func:`os.stat` call on either pathname fails.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000378
Cheryl Sabella2d6097d2018-10-12 10:55:20 -0400379 .. availability:: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000380
Georg Brandlb3823372010-07-10 08:58:37 +0000381 .. versionchanged:: 3.2
382 Added Windows support.
Brian Curtinc7395692010-07-09 15:15:09 +0000383
Brian Curtin490b32a2012-12-26 07:03:03 -0600384 .. versionchanged:: 3.4
385 Windows now uses the same implementation as all other platforms.
386
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700387 .. versionchanged:: 3.6
388 Accepts a :term:`path-like object`.
389
Georg Brandl116aa622007-08-15 14:28:22 +0000390
391.. function:: sameopenfile(fp1, fp2)
392
393 Return ``True`` if the file descriptors *fp1* and *fp2* refer to the same file.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000394
Cheryl Sabella2d6097d2018-10-12 10:55:20 -0400395 .. availability:: Unix, Windows.
Brian Curtin62857742010-09-06 17:07:27 +0000396
Georg Brandl61063cc2012-06-24 22:48:30 +0200397 .. versionchanged:: 3.2
398 Added Windows support.
Georg Brandl116aa622007-08-15 14:28:22 +0000399
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700400 .. versionchanged:: 3.6
401 Accepts a :term:`path-like object`.
402
Georg Brandl116aa622007-08-15 14:28:22 +0000403
404.. function:: samestat(stat1, stat2)
405
406 Return ``True`` if the stat tuples *stat1* and *stat2* refer to the same file.
Serhiy Storchakadab83542013-10-13 20:12:43 +0300407 These structures may have been returned by :func:`os.fstat`,
408 :func:`os.lstat`, or :func:`os.stat`. This function implements the
409 underlying comparison used by :func:`samefile` and :func:`sameopenfile`.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000410
Cheryl Sabella2d6097d2018-10-12 10:55:20 -0400411 .. availability:: Unix, Windows.
Brian Curtin490b32a2012-12-26 07:03:03 -0600412
413 .. versionchanged:: 3.4
414 Added Windows support.
Georg Brandl116aa622007-08-15 14:28:22 +0000415
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700416 .. versionchanged:: 3.6
417 Accepts a :term:`path-like object`.
418
Georg Brandl116aa622007-08-15 14:28:22 +0000419
420.. function:: split(path)
421
Georg Brandl539c1652010-10-14 06:46:08 +0000422 Split the pathname *path* into a pair, ``(head, tail)`` where *tail* is the
423 last pathname component and *head* is everything leading up to that. The
424 *tail* part will never contain a slash; if *path* ends in a slash, *tail*
425 will be empty. If there is no slash in *path*, *head* will be empty. If
426 *path* is empty, both *head* and *tail* are empty. Trailing slashes are
427 stripped from *head* unless it is the root (one or more slashes only). In
428 all cases, ``join(head, tail)`` returns a path to the same location as *path*
Chris Jerdonek0b502ff2012-11-25 20:38:01 -0800429 (but the strings may differ). Also see the functions :func:`dirname` and
430 :func:`basename`.
Georg Brandl116aa622007-08-15 14:28:22 +0000431
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700432 .. versionchanged:: 3.6
433 Accepts a :term:`path-like object`.
434
Georg Brandl116aa622007-08-15 14:28:22 +0000435
436.. function:: splitdrive(path)
437
438 Split the pathname *path* into a pair ``(drive, tail)`` where *drive* is either
Mark Hammond5a607a32009-05-06 08:04:54 +0000439 a mount point or the empty string. On systems which do not use drive
Georg Brandl116aa622007-08-15 14:28:22 +0000440 specifications, *drive* will always be the empty string. In all cases, ``drive
441 + tail`` will be the same as *path*.
442
Mark Hammond5a607a32009-05-06 08:04:54 +0000443 On Windows, splits a pathname into drive/UNC sharepoint and relative path.
444
445 If the path contains a drive letter, drive will contain everything
446 up to and including the colon.
447 e.g. ``splitdrive("c:/dir")`` returns ``("c:", "/dir")``
448
449 If the path contains a UNC path, drive will contain the host name
450 and share, up to but not including the fourth separator.
451 e.g. ``splitdrive("//host/computer/dir")`` returns ``("//host/computer", "/dir")``
452
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700453 .. versionchanged:: 3.6
454 Accepts a :term:`path-like object`.
455
Georg Brandl116aa622007-08-15 14:28:22 +0000456
457.. function:: splitext(path)
458
459 Split the pathname *path* into a pair ``(root, ext)`` such that ``root + ext ==
460 path``, and *ext* is empty or begins with a period and contains at most one
461 period. Leading periods on the basename are ignored; ``splitext('.cshrc')``
462 returns ``('.cshrc', '')``.
463
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700464 .. versionchanged:: 3.6
465 Accepts a :term:`path-like object`.
466
Georg Brandl116aa622007-08-15 14:28:22 +0000467
Georg Brandl116aa622007-08-15 14:28:22 +0000468.. data:: supports_unicode_filenames
469
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200470 ``True`` if arbitrary Unicode strings can be used as file names (within limitations
Victor Stinnerb55e4982010-09-11 00:22:12 +0000471 imposed by the file system).