blob: 469b2626722375bacc3ffb36c2cbdcd5018a8b4c [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
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007**Source code:** :source:`Lib/posixpath.py` (for POSIX),
8:source:`Lib/ntpath.py` (for Windows NT),
9and :source:`Lib/macpath.py` (for Macintosh)
10
Georg Brandl116aa622007-08-15 14:28:22 +000011.. index:: single: path; operations
12
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040013--------------
14
Georg Brandl116aa622007-08-15 14:28:22 +000015This module implements some useful functions on pathnames. To read or
16write files see :func:`open`, and for accessing the filesystem see the
Martin v. Löwis651423c2008-10-07 07:03:04 +000017:mod:`os` module. The path parameters can be passed as either strings,
18or bytes. Applications are encouraged to represent file names as
19(Unicode) character strings. Unfortunately, some file names may not be
20representable as strings on Unix, so applications that need to support
21arbitrary file names on Unix should use bytes objects to represent
22path names. Vice versa, using bytes objects cannot represent all file
23names on Windows (in the standard ``mbcs`` encoding), hence Windows
24applications should use string objects to access all files.
Georg Brandl116aa622007-08-15 14:28:22 +000025
R David Murraya4e700c2013-01-06 16:13:10 -050026Unlike a unix shell, Python does not do any *automatic* path expansions.
27Functions such as :func:`expanduser` and :func:`expandvars` can be invoked
28explicitly when an application desires shell-like path expansion. (See also
29the :mod:`glob` module.)
30
Antoine Pitrou31119e42013-11-22 17:38:12 +010031
32.. seealso::
33 The :mod:`pathlib` module offers high-level path objects.
34
35
Georg Brandl76e55382008-10-08 16:34:57 +000036.. note::
37
38 All of these functions accept either only bytes or only string objects as
39 their parameters. The result is an object of the same type, if a path or
40 file name is returned.
41
Georg Brandl116aa622007-08-15 14:28:22 +000042
Benjamin Petersond23f8222009-04-05 19:13:16 +000043.. note::
44
45 Since different operating systems have different path name conventions, there
46 are several versions of this module in the standard library. The
47 :mod:`os.path` module is always the path module suitable for the operating
48 system Python is running on, and therefore usable for local paths. However,
49 you can also import and use the individual modules if you want to manipulate
50 a path that is *always* in one of the different formats. They all have the
51 same interface:
52
53 * :mod:`posixpath` for UNIX-style paths
54 * :mod:`ntpath` for Windows paths
55 * :mod:`macpath` for old-style MacOS paths
Benjamin Petersond23f8222009-04-05 19:13:16 +000056
57
Serhiy Storchaka0185f342018-09-18 11:28:51 +030058.. versionchanged:: 3.8
59
60 :func:`exists`, :func:`lexists`, :func:`isdir`, :func:`isfile`,
61 :func:`islink`, and :func:`ismount` now return ``False`` instead of
62 raising an exception for paths that contain characters or bytes
63 unrepresentable at the OS level.
64
65
Georg Brandl116aa622007-08-15 14:28:22 +000066.. function:: abspath(path)
67
68 Return a normalized absolutized version of the pathname *path*. On most
Chris Jerdonek0b502ff2012-11-25 20:38:01 -080069 platforms, this is equivalent to calling the function :func:`normpath` as
70 follows: ``normpath(join(os.getcwd(), path))``.
Georg Brandl116aa622007-08-15 14:28:22 +000071
Brett Cannon6fa7aad2016-09-06 15:55:02 -070072 .. versionchanged:: 3.6
73 Accepts a :term:`path-like object`.
74
Georg Brandl116aa622007-08-15 14:28:22 +000075
76.. function:: basename(path)
77
Chris Jerdonek0b502ff2012-11-25 20:38:01 -080078 Return the base name of pathname *path*. This is the second element of the
79 pair returned by passing *path* to the function :func:`split`. Note that
80 the result of this function is different
Georg Brandl116aa622007-08-15 14:28:22 +000081 from the Unix :program:`basename` program; where :program:`basename` for
82 ``'/foo/bar/'`` returns ``'bar'``, the :func:`basename` function returns an
83 empty string (``''``).
84
Brett Cannon6fa7aad2016-09-06 15:55:02 -070085 .. versionchanged:: 3.6
86 Accepts a :term:`path-like object`.
87
Georg Brandl116aa622007-08-15 14:28:22 +000088
Serhiy Storchaka38220932015-03-31 15:31:53 +030089.. function:: commonpath(paths)
90
91 Return the longest common sub-path of each pathname in the sequence
92 *paths*. Raise ValueError if *paths* contains both absolute and relative
93 pathnames, or if *paths* is empty. Unlike :func:`commonprefix`, this
94 returns a valid path.
95
Cheryl Sabella2d6097d2018-10-12 10:55:20 -040096 .. availability:: Unix, Windows.
Serhiy Storchaka38220932015-03-31 15:31:53 +030097
98 .. versionadded:: 3.5
99
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700100 .. versionchanged:: 3.6
101 Accepts a sequence of :term:`path-like objects <path-like object>`.
102
Serhiy Storchaka38220932015-03-31 15:31:53 +0300103
Georg Brandl116aa622007-08-15 14:28:22 +0000104.. function:: commonprefix(list)
105
Serhiy Storchaka38220932015-03-31 15:31:53 +0300106 Return the longest path prefix (taken character-by-character) that is a
107 prefix of all paths in *list*. If *list* is empty, return the empty string
Yury Selivanov80ac11f2015-08-17 23:43:43 -0400108 (``''``).
109
110 .. note::
111
112 This function may return invalid paths because it works a
113 character at a time. To obtain a valid path, see
114 :func:`commonpath`.
115
116 ::
117
Yury Selivanovde115612015-08-19 09:53:28 -0400118 >>> os.path.commonprefix(['/usr/lib', '/usr/local/lib'])
119 '/usr/l'
Yury Selivanov80ac11f2015-08-17 23:43:43 -0400120
Yury Selivanovde115612015-08-19 09:53:28 -0400121 >>> os.path.commonpath(['/usr/lib', '/usr/local/lib'])
122 '/usr'
Georg Brandl116aa622007-08-15 14:28:22 +0000123
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700124 .. versionchanged:: 3.6
125 Accepts a :term:`path-like object`.
126
Georg Brandl116aa622007-08-15 14:28:22 +0000127
128.. function:: dirname(path)
129
Chris Jerdonek0b502ff2012-11-25 20:38:01 -0800130 Return the directory name of pathname *path*. This is the first element of
131 the pair returned by passing *path* to the function :func:`split`.
Georg Brandl116aa622007-08-15 14:28:22 +0000132
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700133 .. versionchanged:: 3.6
134 Accepts a :term:`path-like object`.
135
Georg Brandl116aa622007-08-15 14:28:22 +0000136
137.. function:: exists(path)
138
Richard Oudkerk2240ac12012-07-06 12:05:32 +0100139 Return ``True`` if *path* refers to an existing path or an open
140 file descriptor. Returns ``False`` for broken symbolic links. On
141 some platforms, this function may return ``False`` if permission is
142 not granted to execute :func:`os.stat` on the requested file, even
Georg Brandl116aa622007-08-15 14:28:22 +0000143 if the *path* physically exists.
144
Richard Oudkerk2240ac12012-07-06 12:05:32 +0100145 .. versionchanged:: 3.3
146 *path* can now be an integer: ``True`` is returned if it is an
147 open file descriptor, ``False`` otherwise.
148
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700149 .. versionchanged:: 3.6
150 Accepts a :term:`path-like object`.
151
Georg Brandl116aa622007-08-15 14:28:22 +0000152
153.. function:: lexists(path)
154
155 Return ``True`` if *path* refers to an existing path. Returns ``True`` for
156 broken symbolic links. Equivalent to :func:`exists` on platforms lacking
157 :func:`os.lstat`.
158
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700159 .. versionchanged:: 3.6
160 Accepts a :term:`path-like object`.
161
Georg Brandl116aa622007-08-15 14:28:22 +0000162
Serhiy Storchaka913876d2018-10-28 13:41:26 +0200163.. index:: single: ~ (tilde); home directory expansion
Serhiy Storchakaddb961d2018-10-26 09:00:49 +0300164
Georg Brandl116aa622007-08-15 14:28:22 +0000165.. function:: expanduser(path)
166
167 On Unix and Windows, return the argument with an initial component of ``~`` or
168 ``~user`` replaced by that *user*'s home directory.
169
170 .. index:: module: pwd
171
172 On Unix, an initial ``~`` is replaced by the environment variable :envvar:`HOME`
173 if it is set; otherwise the current user's home directory is looked up in the
174 password directory through the built-in module :mod:`pwd`. An initial ``~user``
175 is looked up directly in the password directory.
176
177 On Windows, :envvar:`HOME` and :envvar:`USERPROFILE` will be used if set,
178 otherwise a combination of :envvar:`HOMEPATH` and :envvar:`HOMEDRIVE` will be
179 used. An initial ``~user`` is handled by stripping the last directory component
180 from the created user path derived above.
181
182 If the expansion fails or if the path does not begin with a tilde, the path is
183 returned unchanged.
184
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700185 .. versionchanged:: 3.6
186 Accepts a :term:`path-like object`.
187
Serhiy Storchakaddb961d2018-10-26 09:00:49 +0300188.. index::
Serhiy Storchaka913876d2018-10-28 13:41:26 +0200189 single: $ (dollar); environment variables expansion
190 single: % (percent); environment variables expansion (Windows)
Georg Brandl116aa622007-08-15 14:28:22 +0000191
192.. function:: expandvars(path)
193
194 Return the argument with environment variables expanded. Substrings of the form
195 ``$name`` or ``${name}`` are replaced by the value of environment variable
196 *name*. Malformed variable names and references to non-existing variables are
197 left unchanged.
198
199 On Windows, ``%name%`` expansions are supported in addition to ``$name`` and
200 ``${name}``.
201
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700202 .. versionchanged:: 3.6
203 Accepts a :term:`path-like object`.
204
Georg Brandl116aa622007-08-15 14:28:22 +0000205
206.. function:: getatime(path)
207
Victor Stinner01b5aab2017-10-24 02:02:00 -0700208 Return the time of last access of *path*. The return value is a floating point number giving
Georg Brandl116aa622007-08-15 14:28:22 +0000209 the number of seconds since the epoch (see the :mod:`time` module). Raise
Andrew Svetlov618c2e12012-12-15 22:59:24 +0200210 :exc:`OSError` if the file does not exist or is inaccessible.
Georg Brandl116aa622007-08-15 14:28:22 +0000211
Georg Brandl116aa622007-08-15 14:28:22 +0000212
213.. function:: getmtime(path)
214
Victor Stinner01b5aab2017-10-24 02:02:00 -0700215 Return the time of last modification of *path*. The return value is a floating point number
Georg Brandl116aa622007-08-15 14:28:22 +0000216 giving the number of seconds since the epoch (see the :mod:`time` module).
Andrew Svetlov618c2e12012-12-15 22:59:24 +0200217 Raise :exc:`OSError` if the file does not exist or is inaccessible.
Georg Brandl116aa622007-08-15 14:28:22 +0000218
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700219 .. versionchanged:: 3.6
220 Accepts a :term:`path-like object`.
221
Georg Brandl116aa622007-08-15 14:28:22 +0000222
223.. function:: getctime(path)
224
225 Return the system's ctime which, on some systems (like Unix) is the time of the
Georg Brandlf6324942013-10-06 09:52:55 +0200226 last metadata change, and, on others (like Windows), is the creation time for *path*.
Georg Brandl116aa622007-08-15 14:28:22 +0000227 The return value is a number giving the number of seconds since the epoch (see
Andrew Svetlov618c2e12012-12-15 22:59:24 +0200228 the :mod:`time` module). Raise :exc:`OSError` if the file does not exist or
Georg Brandl116aa622007-08-15 14:28:22 +0000229 is inaccessible.
230
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700231 .. versionchanged:: 3.6
232 Accepts a :term:`path-like object`.
233
Georg Brandl116aa622007-08-15 14:28:22 +0000234
235.. function:: getsize(path)
236
Andrew Svetlov618c2e12012-12-15 22:59:24 +0200237 Return the size, in bytes, of *path*. Raise :exc:`OSError` if the file does
Georg Brandl116aa622007-08-15 14:28:22 +0000238 not exist or is inaccessible.
239
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700240 .. versionchanged:: 3.6
241 Accepts a :term:`path-like object`.
242
Georg Brandl116aa622007-08-15 14:28:22 +0000243
244.. function:: isabs(path)
245
Christian Heimesaf98da12008-01-27 15:18:18 +0000246 Return ``True`` if *path* is an absolute pathname. On Unix, that means it
247 begins with a slash, on Windows that it begins with a (back)slash after chopping
248 off a potential drive letter.
Georg Brandl116aa622007-08-15 14:28:22 +0000249
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700250 .. versionchanged:: 3.6
251 Accepts a :term:`path-like object`.
252
Georg Brandl116aa622007-08-15 14:28:22 +0000253
254.. function:: isfile(path)
255
Cheryl Sabellab3dd18d2018-01-14 23:57:51 -0500256 Return ``True`` if *path* is an :func:`existing <exists>` regular file.
257 This follows symbolic links, so both :func:`islink` and :func:`isfile` can
258 be true for the same path.
Georg Brandl116aa622007-08-15 14:28:22 +0000259
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700260 .. versionchanged:: 3.6
261 Accepts a :term:`path-like object`.
262
Georg Brandl116aa622007-08-15 14:28:22 +0000263
264.. function:: isdir(path)
265
Cheryl Sabellab3dd18d2018-01-14 23:57:51 -0500266 Return ``True`` if *path* is an :func:`existing <exists>` directory. This
267 follows symbolic links, so both :func:`islink` and :func:`isdir` can be true
268 for the same path.
Georg Brandl116aa622007-08-15 14:28:22 +0000269
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700270 .. versionchanged:: 3.6
271 Accepts a :term:`path-like object`.
272
Georg Brandl116aa622007-08-15 14:28:22 +0000273
274.. function:: islink(path)
275
Cheryl Sabellab3dd18d2018-01-14 23:57:51 -0500276 Return ``True`` if *path* refers to an :func:`existing <exists>` directory
277 entry that is a symbolic link. Always ``False`` if symbolic links are not
278 supported by the Python runtime.
Georg Brandl116aa622007-08-15 14:28:22 +0000279
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700280 .. versionchanged:: 3.6
281 Accepts a :term:`path-like object`.
282
Georg Brandl116aa622007-08-15 14:28:22 +0000283
284.. function:: ismount(path)
285
Larry Hastings3732ed22014-03-15 21:13:56 -0700286 Return ``True`` if pathname *path* is a :dfn:`mount point`: a point in a
287 file system where a different file system has been mounted. On POSIX, the
288 function checks whether *path*'s parent, :file:`path/..`, is on a different
289 device than *path*, or whether :file:`path/..` and *path* point to the same
290 i-node on the same device --- this should detect mount points for all Unix
291 and POSIX variants. On Windows, a drive letter root and a share UNC are
292 always mount points, and for any other path ``GetVolumePathName`` is called
293 to see if it is different from the input path.
294
295 .. versionadded:: 3.4
296 Support for detecting non-root mount points on Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000297
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700298 .. versionchanged:: 3.6
299 Accepts a :term:`path-like object`.
300
Georg Brandl116aa622007-08-15 14:28:22 +0000301
Zachary Warea13dab42014-10-10 16:03:14 -0500302.. function:: join(path, *paths)
Georg Brandl116aa622007-08-15 14:28:22 +0000303
Zachary Warea13dab42014-10-10 16:03:14 -0500304 Join one or more path components intelligently. The return value is the
305 concatenation of *path* and any members of *\*paths* with exactly one
306 directory separator (``os.sep``) following each non-empty part except the
307 last, meaning that the result will only end in a separator if the last
308 part is empty. If a component is an absolute path, all previous
309 components are thrown away and joining continues from the absolute path
310 component.
311
312 On Windows, the drive letter is not reset when an absolute path component
313 (e.g., ``r'\foo'``) is encountered. If a component contains a drive
314 letter, all previous components are thrown away and the drive letter is
315 reset. Note that since there is a current directory for each drive,
316 ``os.path.join("c:", "foo")`` represents a path relative to the current
317 directory on drive :file:`C:` (:file:`c:foo`), not :file:`c:\\foo`.
Georg Brandl116aa622007-08-15 14:28:22 +0000318
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700319 .. versionchanged:: 3.6
320 Accepts a :term:`path-like object` for *path* and *paths*.
321
Georg Brandl116aa622007-08-15 14:28:22 +0000322
323.. function:: normcase(path)
324
Benjamin Petersond23f8222009-04-05 19:13:16 +0000325 Normalize the case of a pathname. On Unix and Mac OS X, this returns the
326 path unchanged; on case-insensitive filesystems, it converts the path to
327 lowercase. On Windows, it also converts forward slashes to backward slashes.
Stéphane Wirtele483f022018-10-26 12:52:11 +0200328 Raise a :exc:`TypeError` if the type of *path* is not ``str`` or ``bytes`` (directly
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700329 or indirectly through the :class:`os.PathLike` interface).
330
331 .. versionchanged:: 3.6
332 Accepts a :term:`path-like object`.
Georg Brandl116aa622007-08-15 14:28:22 +0000333
334
335.. function:: normpath(path)
336
Terry Jan Reedyec6e1322013-03-17 15:21:26 -0400337 Normalize a pathname by collapsing redundant separators and up-level
338 references so that ``A//B``, ``A/B/``, ``A/./B`` and ``A/foo/../B`` all
339 become ``A/B``. This string manipulation may change the meaning of a path
340 that contains symbolic links. On Windows, it converts forward slashes to
Terry Jan Reedyf3460412013-03-17 15:28:10 -0400341 backward slashes. To normalize case, use :func:`normcase`.
Georg Brandl116aa622007-08-15 14:28:22 +0000342
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700343 .. versionchanged:: 3.6
344 Accepts a :term:`path-like object`.
345
Georg Brandl116aa622007-08-15 14:28:22 +0000346
347.. function:: realpath(path)
348
349 Return the canonical path of the specified filename, eliminating any symbolic
350 links encountered in the path (if they are supported by the operating system).
351
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700352 .. versionchanged:: 3.6
353 Accepts a :term:`path-like object`.
354
Georg Brandl116aa622007-08-15 14:28:22 +0000355
Benjamin Peterson409a1be2014-03-20 12:39:53 -0500356.. function:: relpath(path, start=os.curdir)
Georg Brandl116aa622007-08-15 14:28:22 +0000357
R David Murrayce10fab2013-07-12 17:43:11 -0400358 Return a relative filepath to *path* either from the current directory or
359 from an optional *start* directory. This is a path computation: the
360 filesystem is not accessed to confirm the existence or nature of *path* or
361 *start*.
Georg Brandl116aa622007-08-15 14:28:22 +0000362
Benjamin Petersonf650e462010-05-06 23:03:05 +0000363 *start* defaults to :attr:`os.curdir`.
364
Cheryl Sabella2d6097d2018-10-12 10:55:20 -0400365 .. availability:: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000366
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700367 .. versionchanged:: 3.6
368 Accepts a :term:`path-like object`.
369
Georg Brandl116aa622007-08-15 14:28:22 +0000370
371.. function:: samefile(path1, path2)
372
Brian Curtind40e6f72010-07-08 21:39:08 +0000373 Return ``True`` if both pathname arguments refer to the same file or directory.
Larry Hastings3732ed22014-03-15 21:13:56 -0700374 This is determined by the device number and i-node number and raises an
Martin Panter7462b6492015-11-02 03:37:02 +0000375 exception if an :func:`os.stat` call on either pathname fails.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000376
Cheryl Sabella2d6097d2018-10-12 10:55:20 -0400377 .. availability:: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000378
Georg Brandlb3823372010-07-10 08:58:37 +0000379 .. versionchanged:: 3.2
380 Added Windows support.
Brian Curtinc7395692010-07-09 15:15:09 +0000381
Brian Curtin490b32a2012-12-26 07:03:03 -0600382 .. versionchanged:: 3.4
383 Windows now uses the same implementation as all other platforms.
384
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700385 .. versionchanged:: 3.6
386 Accepts a :term:`path-like object`.
387
Georg Brandl116aa622007-08-15 14:28:22 +0000388
389.. function:: sameopenfile(fp1, fp2)
390
391 Return ``True`` if the file descriptors *fp1* and *fp2* refer to the same file.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000392
Cheryl Sabella2d6097d2018-10-12 10:55:20 -0400393 .. availability:: Unix, Windows.
Brian Curtin62857742010-09-06 17:07:27 +0000394
Georg Brandl61063cc2012-06-24 22:48:30 +0200395 .. versionchanged:: 3.2
396 Added Windows support.
Georg Brandl116aa622007-08-15 14:28:22 +0000397
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700398 .. versionchanged:: 3.6
399 Accepts a :term:`path-like object`.
400
Georg Brandl116aa622007-08-15 14:28:22 +0000401
402.. function:: samestat(stat1, stat2)
403
404 Return ``True`` if the stat tuples *stat1* and *stat2* refer to the same file.
Serhiy Storchakadab83542013-10-13 20:12:43 +0300405 These structures may have been returned by :func:`os.fstat`,
406 :func:`os.lstat`, or :func:`os.stat`. This function implements the
407 underlying comparison used by :func:`samefile` and :func:`sameopenfile`.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000408
Cheryl Sabella2d6097d2018-10-12 10:55:20 -0400409 .. availability:: Unix, Windows.
Brian Curtin490b32a2012-12-26 07:03:03 -0600410
411 .. versionchanged:: 3.4
412 Added Windows support.
Georg Brandl116aa622007-08-15 14:28:22 +0000413
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700414 .. versionchanged:: 3.6
415 Accepts a :term:`path-like object`.
416
Georg Brandl116aa622007-08-15 14:28:22 +0000417
418.. function:: split(path)
419
Georg Brandl539c1652010-10-14 06:46:08 +0000420 Split the pathname *path* into a pair, ``(head, tail)`` where *tail* is the
421 last pathname component and *head* is everything leading up to that. The
422 *tail* part will never contain a slash; if *path* ends in a slash, *tail*
423 will be empty. If there is no slash in *path*, *head* will be empty. If
424 *path* is empty, both *head* and *tail* are empty. Trailing slashes are
425 stripped from *head* unless it is the root (one or more slashes only). In
426 all cases, ``join(head, tail)`` returns a path to the same location as *path*
Chris Jerdonek0b502ff2012-11-25 20:38:01 -0800427 (but the strings may differ). Also see the functions :func:`dirname` and
428 :func:`basename`.
Georg Brandl116aa622007-08-15 14:28:22 +0000429
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700430 .. versionchanged:: 3.6
431 Accepts a :term:`path-like object`.
432
Georg Brandl116aa622007-08-15 14:28:22 +0000433
434.. function:: splitdrive(path)
435
436 Split the pathname *path* into a pair ``(drive, tail)`` where *drive* is either
Mark Hammond5a607a32009-05-06 08:04:54 +0000437 a mount point or the empty string. On systems which do not use drive
Georg Brandl116aa622007-08-15 14:28:22 +0000438 specifications, *drive* will always be the empty string. In all cases, ``drive
439 + tail`` will be the same as *path*.
440
Mark Hammond5a607a32009-05-06 08:04:54 +0000441 On Windows, splits a pathname into drive/UNC sharepoint and relative path.
442
443 If the path contains a drive letter, drive will contain everything
444 up to and including the colon.
445 e.g. ``splitdrive("c:/dir")`` returns ``("c:", "/dir")``
446
447 If the path contains a UNC path, drive will contain the host name
448 and share, up to but not including the fourth separator.
449 e.g. ``splitdrive("//host/computer/dir")`` returns ``("//host/computer", "/dir")``
450
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700451 .. versionchanged:: 3.6
452 Accepts a :term:`path-like object`.
453
Georg Brandl116aa622007-08-15 14:28:22 +0000454
455.. function:: splitext(path)
456
457 Split the pathname *path* into a pair ``(root, ext)`` such that ``root + ext ==
458 path``, and *ext* is empty or begins with a period and contains at most one
459 period. Leading periods on the basename are ignored; ``splitext('.cshrc')``
460 returns ``('.cshrc', '')``.
461
Brett Cannon6fa7aad2016-09-06 15:55:02 -0700462 .. versionchanged:: 3.6
463 Accepts a :term:`path-like object`.
464
Georg Brandl116aa622007-08-15 14:28:22 +0000465
Georg Brandl116aa622007-08-15 14:28:22 +0000466.. data:: supports_unicode_filenames
467
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200468 ``True`` if arbitrary Unicode strings can be used as file names (within limitations
Victor Stinnerb55e4982010-09-11 00:22:12 +0000469 imposed by the file system).