blob: a5abacf02144a765113763718cb4cbc76f339107 [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
Makdon95492032019-06-13 21:59:49 +080090 *paths*. Raise :exc:`ValueError` if *paths* contain both absolute
91 and relative pathnames, the *paths* are on the different drives or
92 if *paths* is empty. Unlike :func:`commonprefix`, this returns a
93 valid path.
Serhiy Storchaka38220932015-03-31 15:31:53 +030094
Cheryl Sabella2d6097d2018-10-12 10:55:20 -040095 .. availability:: Unix, Windows.
Serhiy Storchaka38220932015-03-31 15:31:53 +030096
97 .. versionadded:: 3.5
98
Brett Cannon6fa7aad2016-09-06 15:55:02 -070099 .. versionchanged:: 3.6
100 Accepts a sequence of :term:`path-like objects <path-like object>`.
101
Serhiy Storchaka38220932015-03-31 15:31:53 +0300102
Georg Brandl116aa622007-08-15 14:28:22 +0000103.. function:: commonprefix(list)
104
Serhiy Storchaka38220932015-03-31 15:31:53 +0300105 Return the longest path prefix (taken character-by-character) that is a
106 prefix of all paths in *list*. If *list* is empty, return the empty string
Yury Selivanov80ac11f2015-08-17 23:43:43 -0400107 (``''``).
108
109 .. note::
110
111 This function may return invalid paths because it works a
112 character at a time. To obtain a valid path, see
113 :func:`commonpath`.
114
115 ::
116
Yury Selivanovde115612015-08-19 09:53:28 -0400117 >>> os.path.commonprefix(['/usr/lib', '/usr/local/lib'])
118 '/usr/l'
Yury Selivanov80ac11f2015-08-17 23:43:43 -0400119
Yury Selivanovde115612015-08-19 09:53:28 -0400120 >>> os.path.commonpath(['/usr/lib', '/usr/local/lib'])
121 '/usr'
Georg Brandl116aa622007-08-15 14:28:22 +0000122
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700123 .. versionchanged:: 3.6
124 Accepts a :term:`path-like object`.
125
Georg Brandl116aa622007-08-15 14:28:22 +0000126
127.. function:: dirname(path)
128
Chris Jerdonek0b502ff2012-11-25 20:38:01 -0800129 Return the directory name of pathname *path*. This is the first element of
130 the pair returned by passing *path* to the function :func:`split`.
Georg Brandl116aa622007-08-15 14:28:22 +0000131
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700132 .. versionchanged:: 3.6
133 Accepts a :term:`path-like object`.
134
Georg Brandl116aa622007-08-15 14:28:22 +0000135
136.. function:: exists(path)
137
Richard Oudkerk2240ac12012-07-06 12:05:32 +0100138 Return ``True`` if *path* refers to an existing path or an open
139 file descriptor. Returns ``False`` for broken symbolic links. On
140 some platforms, this function may return ``False`` if permission is
141 not granted to execute :func:`os.stat` on the requested file, even
Georg Brandl116aa622007-08-15 14:28:22 +0000142 if the *path* physically exists.
143
Richard Oudkerk2240ac12012-07-06 12:05:32 +0100144 .. versionchanged:: 3.3
145 *path* can now be an integer: ``True`` is returned if it is an
146 open file descriptor, ``False`` otherwise.
147
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700148 .. versionchanged:: 3.6
149 Accepts a :term:`path-like object`.
150
Georg Brandl116aa622007-08-15 14:28:22 +0000151
152.. function:: lexists(path)
153
154 Return ``True`` if *path* refers to an existing path. Returns ``True`` for
155 broken symbolic links. Equivalent to :func:`exists` on platforms lacking
156 :func:`os.lstat`.
157
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700158 .. versionchanged:: 3.6
159 Accepts a :term:`path-like object`.
160
Georg Brandl116aa622007-08-15 14:28:22 +0000161
Serhiy Storchaka913876d2018-10-28 13:41:26 +0200162.. index:: single: ~ (tilde); home directory expansion
Serhiy Storchakaddb961d2018-10-26 09:00:49 +0300163
Georg Brandl116aa622007-08-15 14:28:22 +0000164.. function:: expanduser(path)
165
166 On Unix and Windows, return the argument with an initial component of ``~`` or
167 ``~user`` replaced by that *user*'s home directory.
168
169 .. index:: module: pwd
170
171 On Unix, an initial ``~`` is replaced by the environment variable :envvar:`HOME`
172 if it is set; otherwise the current user's home directory is looked up in the
173 password directory through the built-in module :mod:`pwd`. An initial ``~user``
174 is looked up directly in the password directory.
175
Steve Dower8ef864d2019-03-12 15:15:26 -0700176 On Windows, :envvar:`USERPROFILE` will be used if set, otherwise a combination
177 of :envvar:`HOMEPATH` and :envvar:`HOMEDRIVE` will be used. An initial
178 ``~user`` is handled by stripping the last directory component from the created
179 user path derived above.
Georg Brandl116aa622007-08-15 14:28:22 +0000180
181 If the expansion fails or if the path does not begin with a tilde, the path is
182 returned unchanged.
183
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700184 .. versionchanged:: 3.6
185 Accepts a :term:`path-like object`.
186
Steve Dower8ef864d2019-03-12 15:15:26 -0700187 .. versionchanged:: 3.8
188 No longer uses :envvar:`HOME` on Windows.
189
Serhiy Storchakaddb961d2018-10-26 09:00:49 +0300190.. index::
Serhiy Storchaka913876d2018-10-28 13:41:26 +0200191 single: $ (dollar); environment variables expansion
192 single: % (percent); environment variables expansion (Windows)
Georg Brandl116aa622007-08-15 14:28:22 +0000193
194.. function:: expandvars(path)
195
196 Return the argument with environment variables expanded. Substrings of the form
197 ``$name`` or ``${name}`` are replaced by the value of environment variable
198 *name*. Malformed variable names and references to non-existing variables are
199 left unchanged.
200
201 On Windows, ``%name%`` expansions are supported in addition to ``$name`` and
202 ``${name}``.
203
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700204 .. versionchanged:: 3.6
205 Accepts a :term:`path-like object`.
206
Georg Brandl116aa622007-08-15 14:28:22 +0000207
208.. function:: getatime(path)
209
Victor Stinner01b5aab2017-10-24 02:02:00 -0700210 Return the time of last access of *path*. The return value is a floating point number giving
Georg Brandl116aa622007-08-15 14:28:22 +0000211 the number of seconds since the epoch (see the :mod:`time` module). Raise
Andrew Svetlov618c2e12012-12-15 22:59:24 +0200212 :exc:`OSError` if the file does not exist or is inaccessible.
Georg Brandl116aa622007-08-15 14:28:22 +0000213
Georg Brandl116aa622007-08-15 14:28:22 +0000214
215.. function:: getmtime(path)
216
Victor Stinner01b5aab2017-10-24 02:02:00 -0700217 Return the time of last modification of *path*. The return value is a floating point number
Georg Brandl116aa622007-08-15 14:28:22 +0000218 giving the number of seconds since the epoch (see the :mod:`time` module).
Andrew Svetlov618c2e12012-12-15 22:59:24 +0200219 Raise :exc:`OSError` if the file does not exist or is inaccessible.
Georg Brandl116aa622007-08-15 14:28:22 +0000220
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700221 .. versionchanged:: 3.6
222 Accepts a :term:`path-like object`.
223
Georg Brandl116aa622007-08-15 14:28:22 +0000224
225.. function:: getctime(path)
226
227 Return the system's ctime which, on some systems (like Unix) is the time of the
Georg Brandlf6324942013-10-06 09:52:55 +0200228 last metadata change, and, on others (like Windows), is the creation time for *path*.
Georg Brandl116aa622007-08-15 14:28:22 +0000229 The return value is a number giving the number of seconds since the epoch (see
Andrew Svetlov618c2e12012-12-15 22:59:24 +0200230 the :mod:`time` module). Raise :exc:`OSError` if the file does not exist or
Georg Brandl116aa622007-08-15 14:28:22 +0000231 is inaccessible.
232
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700233 .. versionchanged:: 3.6
234 Accepts a :term:`path-like object`.
235
Georg Brandl116aa622007-08-15 14:28:22 +0000236
237.. function:: getsize(path)
238
Andrew Svetlov618c2e12012-12-15 22:59:24 +0200239 Return the size, in bytes, of *path*. Raise :exc:`OSError` if the file does
Georg Brandl116aa622007-08-15 14:28:22 +0000240 not exist or is inaccessible.
241
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700242 .. versionchanged:: 3.6
243 Accepts a :term:`path-like object`.
244
Georg Brandl116aa622007-08-15 14:28:22 +0000245
246.. function:: isabs(path)
247
Christian Heimesaf98da12008-01-27 15:18:18 +0000248 Return ``True`` if *path* is an absolute pathname. On Unix, that means it
249 begins with a slash, on Windows that it begins with a (back)slash after chopping
250 off a potential drive letter.
Georg Brandl116aa622007-08-15 14:28:22 +0000251
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700252 .. versionchanged:: 3.6
253 Accepts a :term:`path-like object`.
254
Georg Brandl116aa622007-08-15 14:28:22 +0000255
256.. function:: isfile(path)
257
Cheryl Sabellab3dd18d2018-01-14 23:57:51 -0500258 Return ``True`` if *path* is an :func:`existing <exists>` regular file.
259 This follows symbolic links, so both :func:`islink` and :func:`isfile` can
260 be true for the same path.
Georg Brandl116aa622007-08-15 14:28:22 +0000261
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700262 .. versionchanged:: 3.6
263 Accepts a :term:`path-like object`.
264
Georg Brandl116aa622007-08-15 14:28:22 +0000265
266.. function:: isdir(path)
267
Cheryl Sabellab3dd18d2018-01-14 23:57:51 -0500268 Return ``True`` if *path* is an :func:`existing <exists>` directory. This
269 follows symbolic links, so both :func:`islink` and :func:`isdir` can be true
270 for the same path.
Georg Brandl116aa622007-08-15 14:28:22 +0000271
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700272 .. versionchanged:: 3.6
273 Accepts a :term:`path-like object`.
274
Georg Brandl116aa622007-08-15 14:28:22 +0000275
276.. function:: islink(path)
277
Cheryl Sabellab3dd18d2018-01-14 23:57:51 -0500278 Return ``True`` if *path* refers to an :func:`existing <exists>` directory
279 entry that is a symbolic link. Always ``False`` if symbolic links are not
280 supported by the Python runtime.
Georg Brandl116aa622007-08-15 14:28:22 +0000281
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700282 .. versionchanged:: 3.6
283 Accepts a :term:`path-like object`.
284
Georg Brandl116aa622007-08-15 14:28:22 +0000285
286.. function:: ismount(path)
287
Larry Hastings3732ed22014-03-15 21:13:56 -0700288 Return ``True`` if pathname *path* is a :dfn:`mount point`: a point in a
289 file system where a different file system has been mounted. On POSIX, the
Serhiy Storchaka32ebd852019-01-15 10:55:40 +0200290 function checks whether *path*'s parent, :file:`{path}/..`, is on a different
291 device than *path*, or whether :file:`{path}/..` and *path* point to the same
Larry Hastings3732ed22014-03-15 21:13:56 -0700292 i-node on the same device --- this should detect mount points for all Unix
Serhiy Storchaka32ebd852019-01-15 10:55:40 +0200293 and POSIX variants. It is not able to reliably detect bind mounts on the
294 same filesystem. On Windows, a drive letter root and a share UNC are
Larry Hastings3732ed22014-03-15 21:13:56 -0700295 always mount points, and for any other path ``GetVolumePathName`` is called
296 to see if it is different from the input path.
297
298 .. versionadded:: 3.4
299 Support for detecting non-root mount points on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000300
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700301 .. versionchanged:: 3.6
302 Accepts a :term:`path-like object`.
303
Georg Brandl116aa622007-08-15 14:28:22 +0000304
Zachary Warea13dab42014-10-10 16:03:14 -0500305.. function:: join(path, *paths)
Georg Brandl116aa622007-08-15 14:28:22 +0000306
Zachary Warea13dab42014-10-10 16:03:14 -0500307 Join one or more path components intelligently. The return value is the
308 concatenation of *path* and any members of *\*paths* with exactly one
309 directory separator (``os.sep``) following each non-empty part except the
310 last, meaning that the result will only end in a separator if the last
311 part is empty. If a component is an absolute path, all previous
312 components are thrown away and joining continues from the absolute path
313 component.
314
315 On Windows, the drive letter is not reset when an absolute path component
316 (e.g., ``r'\foo'``) is encountered. If a component contains a drive
317 letter, all previous components are thrown away and the drive letter is
318 reset. Note that since there is a current directory for each drive,
319 ``os.path.join("c:", "foo")`` represents a path relative to the current
320 directory on drive :file:`C:` (:file:`c:foo`), not :file:`c:\\foo`.
Georg Brandl116aa622007-08-15 14:28:22 +0000321
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700322 .. versionchanged:: 3.6
323 Accepts a :term:`path-like object` for *path* and *paths*.
324
Georg Brandl116aa622007-08-15 14:28:22 +0000325
326.. function:: normcase(path)
327
Kexuan Sun32d14582019-05-13 04:38:20 -0700328 Normalize the case of a pathname. On Windows, convert all characters in the
329 pathname to lowercase, and also convert forward slashes to backward slashes.
330 On other operating systems, return the path unchanged.
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700331
332 .. versionchanged:: 3.6
333 Accepts a :term:`path-like object`.
Georg Brandl116aa622007-08-15 14:28:22 +0000334
335
336.. function:: normpath(path)
337
Terry Jan Reedyec6e1322013-03-17 15:21:26 -0400338 Normalize a pathname by collapsing redundant separators and up-level
339 references so that ``A//B``, ``A/B/``, ``A/./B`` and ``A/foo/../B`` all
340 become ``A/B``. This string manipulation may change the meaning of a path
341 that contains symbolic links. On Windows, it converts forward slashes to
Terry Jan Reedyf3460412013-03-17 15:28:10 -0400342 backward slashes. To normalize case, use :func:`normcase`.
Georg Brandl116aa622007-08-15 14:28:22 +0000343
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700344 .. versionchanged:: 3.6
345 Accepts a :term:`path-like object`.
346
Georg Brandl116aa622007-08-15 14:28:22 +0000347
348.. function:: realpath(path)
349
350 Return the canonical path of the specified filename, eliminating any symbolic
Steve Dower75e06492019-08-21 13:43:06 -0700351 links encountered in the path (if they are supported by the operating
352 system).
353
354 .. note::
355 When symbolic link cycles occur, the returned path will be one member of
356 the cycle, but no guarantee is made about which member that will be.
Georg Brandl116aa622007-08-15 14:28:22 +0000357
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700358 .. versionchanged:: 3.6
359 Accepts a :term:`path-like object`.
360
Steve Dower75e06492019-08-21 13:43:06 -0700361 .. versionchanged:: 3.8
362 Symbolic links and junctions are now resolved on Windows.
363
Georg Brandl116aa622007-08-15 14:28:22 +0000364
Benjamin Peterson409a1be2014-03-20 12:39:53 -0500365.. function:: relpath(path, start=os.curdir)
Georg Brandl116aa622007-08-15 14:28:22 +0000366
R David Murrayce10fab2013-07-12 17:43:11 -0400367 Return a relative filepath to *path* either from the current directory or
368 from an optional *start* directory. This is a path computation: the
369 filesystem is not accessed to confirm the existence or nature of *path* or
370 *start*.
Georg Brandl116aa622007-08-15 14:28:22 +0000371
Benjamin Petersonf650e462010-05-06 23:03:05 +0000372 *start* defaults to :attr:`os.curdir`.
373
Cheryl Sabella2d6097d2018-10-12 10:55:20 -0400374 .. availability:: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000375
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700376 .. versionchanged:: 3.6
377 Accepts a :term:`path-like object`.
378
Georg Brandl116aa622007-08-15 14:28:22 +0000379
380.. function:: samefile(path1, path2)
381
Brian Curtind40e6f72010-07-08 21:39:08 +0000382 Return ``True`` if both pathname arguments refer to the same file or directory.
Larry Hastings3732ed22014-03-15 21:13:56 -0700383 This is determined by the device number and i-node number and raises an
Martin Panter7462b6492015-11-02 03:37:02 +0000384 exception if an :func:`os.stat` call on either pathname fails.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000385
Cheryl Sabella2d6097d2018-10-12 10:55:20 -0400386 .. availability:: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000387
Georg Brandlb3823372010-07-10 08:58:37 +0000388 .. versionchanged:: 3.2
389 Added Windows support.
Brian Curtinc7395692010-07-09 15:15:09 +0000390
Brian Curtin490b32a2012-12-26 07:03:03 -0600391 .. versionchanged:: 3.4
392 Windows now uses the same implementation as all other platforms.
393
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700394 .. versionchanged:: 3.6
395 Accepts a :term:`path-like object`.
396
Georg Brandl116aa622007-08-15 14:28:22 +0000397
398.. function:: sameopenfile(fp1, fp2)
399
400 Return ``True`` if the file descriptors *fp1* and *fp2* refer to the same file.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000401
Cheryl Sabella2d6097d2018-10-12 10:55:20 -0400402 .. availability:: Unix, Windows.
Brian Curtin62857742010-09-06 17:07:27 +0000403
Georg Brandl61063cc2012-06-24 22:48:30 +0200404 .. versionchanged:: 3.2
405 Added Windows support.
Georg Brandl116aa622007-08-15 14:28:22 +0000406
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700407 .. versionchanged:: 3.6
408 Accepts a :term:`path-like object`.
409
Georg Brandl116aa622007-08-15 14:28:22 +0000410
411.. function:: samestat(stat1, stat2)
412
413 Return ``True`` if the stat tuples *stat1* and *stat2* refer to the same file.
Serhiy Storchakadab83542013-10-13 20:12:43 +0300414 These structures may have been returned by :func:`os.fstat`,
415 :func:`os.lstat`, or :func:`os.stat`. This function implements the
416 underlying comparison used by :func:`samefile` and :func:`sameopenfile`.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000417
Cheryl Sabella2d6097d2018-10-12 10:55:20 -0400418 .. availability:: Unix, Windows.
Brian Curtin490b32a2012-12-26 07:03:03 -0600419
420 .. versionchanged:: 3.4
421 Added Windows support.
Georg Brandl116aa622007-08-15 14:28:22 +0000422
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700423 .. versionchanged:: 3.6
424 Accepts a :term:`path-like object`.
425
Georg Brandl116aa622007-08-15 14:28:22 +0000426
427.. function:: split(path)
428
Georg Brandl539c1652010-10-14 06:46:08 +0000429 Split the pathname *path* into a pair, ``(head, tail)`` where *tail* is the
430 last pathname component and *head* is everything leading up to that. The
431 *tail* part will never contain a slash; if *path* ends in a slash, *tail*
432 will be empty. If there is no slash in *path*, *head* will be empty. If
433 *path* is empty, both *head* and *tail* are empty. Trailing slashes are
434 stripped from *head* unless it is the root (one or more slashes only). In
435 all cases, ``join(head, tail)`` returns a path to the same location as *path*
Chris Jerdonek0b502ff2012-11-25 20:38:01 -0800436 (but the strings may differ). Also see the functions :func:`dirname` and
437 :func:`basename`.
Georg Brandl116aa622007-08-15 14:28:22 +0000438
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700439 .. versionchanged:: 3.6
440 Accepts a :term:`path-like object`.
441
Georg Brandl116aa622007-08-15 14:28:22 +0000442
443.. function:: splitdrive(path)
444
445 Split the pathname *path* into a pair ``(drive, tail)`` where *drive* is either
Mark Hammond5a607a32009-05-06 08:04:54 +0000446 a mount point or the empty string. On systems which do not use drive
Georg Brandl116aa622007-08-15 14:28:22 +0000447 specifications, *drive* will always be the empty string. In all cases, ``drive
448 + tail`` will be the same as *path*.
449
Mark Hammond5a607a32009-05-06 08:04:54 +0000450 On Windows, splits a pathname into drive/UNC sharepoint and relative path.
451
452 If the path contains a drive letter, drive will contain everything
453 up to and including the colon.
454 e.g. ``splitdrive("c:/dir")`` returns ``("c:", "/dir")``
455
456 If the path contains a UNC path, drive will contain the host name
457 and share, up to but not including the fourth separator.
458 e.g. ``splitdrive("//host/computer/dir")`` returns ``("//host/computer", "/dir")``
459
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700460 .. versionchanged:: 3.6
461 Accepts a :term:`path-like object`.
462
Georg Brandl116aa622007-08-15 14:28:22 +0000463
464.. function:: splitext(path)
465
466 Split the pathname *path* into a pair ``(root, ext)`` such that ``root + ext ==
467 path``, and *ext* is empty or begins with a period and contains at most one
468 period. Leading periods on the basename are ignored; ``splitext('.cshrc')``
469 returns ``('.cshrc', '')``.
470
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700471 .. versionchanged:: 3.6
472 Accepts a :term:`path-like object`.
473
Georg Brandl116aa622007-08-15 14:28:22 +0000474
Georg Brandl116aa622007-08-15 14:28:22 +0000475.. data:: supports_unicode_filenames
476
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200477 ``True`` if arbitrary Unicode strings can be used as file names (within limitations
Victor Stinnerb55e4982010-09-11 00:22:12 +0000478 imposed by the file system).