Lines Matching +full:to +full:- +full:object +full:- +full:path
1 :mod:`os.path` --- Common pathname manipulations
4 .. module:: os.path
10 .. index:: single: path; operations
12 --------------
14 This module implements some useful functions on pathnames. To read or write
16 module. The path parameters can be passed as strings, or bytes, or any object
19 Unlike a Unix shell, Python does not do any *automatic* path expansions.
21 explicitly when an application desires shell-like path expansion. (See also
26 The :mod:`pathlib` module offers high-level path objects.
32 their parameters. The result is an object of the same type, if a path or
37 Since different operating systems have different path name conventions, there
39 :mod:`os.path` module is always the path module suitable for the operating
41 you can also import and use the individual modules if you want to manipulate
42 a path that is *always* in one of the different formats. They all have the
45 * :mod:`posixpath` for UNIX-style paths
57 .. function:: abspath(path)
59 Return a normalized absolutized version of the pathname *path*. On most
60 platforms, this is equivalent to calling the function :func:`normpath` as
61 follows: ``normpath(join(os.getcwd(), path))``.
64 Accepts a :term:`path-like object`.
67 .. function:: basename(path)
69 Return the base name of pathname *path*. This is the second element of the
70 pair returned by passing *path* to the function :func:`split`. Note that
77 Accepts a :term:`path-like object`.
82 Return the longest common sub-path of each pathname in the sequence
86 valid path.
93 Accepts a sequence of :term:`path-like objects <path-like object>`.
98 Return the longest path prefix (taken character-by-character) that is a
105 character at a time. To obtain a valid path, see
110 >>> os.path.commonprefix(['/usr/lib', '/usr/local/lib'])
113 >>> os.path.commonpath(['/usr/lib', '/usr/local/lib'])
117 Accepts a :term:`path-like object`.
120 .. function:: dirname(path)
122 Return the directory name of pathname *path*. This is the first element of
123 the pair returned by passing *path* to the function :func:`split`.
126 Accepts a :term:`path-like object`.
129 .. function:: exists(path)
131 Return ``True`` if *path* refers to an existing path or an open
134 not granted to execute :func:`os.stat` on the requested file, even
135 if the *path* physically exists.
138 *path* can now be an integer: ``True`` is returned if it is an
142 Accepts a :term:`path-like object`.
145 .. function:: lexists(path)
147 Return ``True`` if *path* refers to an existing path. Returns ``True`` for
148 broken symbolic links. Equivalent to :func:`exists` on platforms lacking
152 Accepts a :term:`path-like object`.
157 .. function:: expanduser(path)
166 password directory through the built-in module :mod:`pwd`. An initial ``~user``
174 If the expansion fails or if the path does not begin with a tilde, the path is
178 Accepts a :term:`path-like object`.
187 .. function:: expandvars(path)
191 *name*. Malformed variable names and references to non-existing variables are
194 On Windows, ``%name%`` expansions are supported in addition to ``$name`` and
198 Accepts a :term:`path-like object`.
201 .. function:: getatime(path)
203 Return the time of last access of *path*. The return value is a floating point number giving
208 .. function:: getmtime(path)
210 Return the time of last modification of *path*. The return value is a floating point number
215 Accepts a :term:`path-like object`.
218 .. function:: getctime(path)
221 last metadata change, and, on others (like Windows), is the creation time for *path*.
227 Accepts a :term:`path-like object`.
230 .. function:: getsize(path)
232 Return the size, in bytes, of *path*. Raise :exc:`OSError` if the file does
236 Accepts a :term:`path-like object`.
239 .. function:: isabs(path)
241 Return ``True`` if *path* is an absolute pathname. On Unix, that means it
246 Accepts a :term:`path-like object`.
249 .. function:: isfile(path)
251 Return ``True`` if *path* is an :func:`existing <exists>` regular file.
253 be true for the same path.
256 Accepts a :term:`path-like object`.
259 .. function:: isdir(path)
261 Return ``True`` if *path* is an :func:`existing <exists>` directory. This
263 for the same path.
266 Accepts a :term:`path-like object`.
269 .. function:: islink(path)
271 Return ``True`` if *path* refers to an :func:`existing <exists>` directory
276 Accepts a :term:`path-like object`.
279 .. function:: ismount(path)
281 Return ``True`` if pathname *path* is a :dfn:`mount point`: a point in a
283 function checks whether *path*'s parent, :file:`{path}/..`, is on a different
284 device than *path*, or whether :file:`{path}/..` and *path* point to the same
285 i-node on the same device --- this should detect mount points for all Unix
286 and POSIX variants. It is not able to reliably detect bind mounts on the
288 always mount points, and for any other path ``GetVolumePathName`` is called
289 to see if it is different from the input path.
292 Support for detecting non-root mount points on Windows.
295 Accepts a :term:`path-like object`.
298 .. function:: join(path, *paths)
300 Join one or more path segments intelligently. The return value is the
301 concatenation of *path* and all members of *\*paths*, with exactly one
302 directory separator following each non-empty part, except the last. That is,
304 ends in a separator. If a segment is an absolute path (which on Windows
306 joining continues from the absolute path segment.
308 On Windows, the drive is not reset when a rooted path segment (e.g.,
310 absolute path, all previous segments are ignored and the drive is reset. Note
312 ``os.path.join("c:", "foo")`` represents a path relative to the current
316 Accepts a :term:`path-like object` for *path* and *paths*.
319 .. function:: normcase(path)
322 pathname to lowercase, and also convert forward slashes to backward slashes.
323 On other operating systems, return the path unchanged.
326 Accepts a :term:`path-like object`.
329 .. function:: normpath(path)
331 Normalize a pathname by collapsing redundant separators and up-level
333 become ``A/B``. This string manipulation may change the meaning of a path
334 that contains symbolic links. On Windows, it converts forward slashes to
335 backward slashes. To normalize case, use :func:`normcase`.
341 following the leading characters may be interpreted in an implementation-defined
346 Accepts a :term:`path-like object`.
349 .. function:: realpath(path, *, strict=False)
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
355 If a path doesn't exist or a symlink loop is encountered, and *strict* is
356 ``True``, :exc:`OSError` is raised. If *strict* is ``False``, the path is
361 This function emulates the operating system's procedure for making a path
363 to how links and subsequent path components interact.
366 normally necessary to call this function.
369 Accepts a :term:`path-like object`.
378 .. function:: relpath(path, start=os.curdir)
380 Return a relative filepath to *path* either from the current directory or
381 from an optional *start* directory. This is a path computation: the
382 filesystem is not accessed to confirm the existence or nature of *path* or
383 *start*. On Windows, :exc:`ValueError` is raised when *path* and *start*
386 *start* defaults to :attr:`os.curdir`.
391 Accepts a :term:`path-like object`.
396 Return ``True`` if both pathname arguments refer to the same file or directory.
397 This is determined by the device number and i-node number and raises an
409 Accepts a :term:`path-like object`.
414 Return ``True`` if the file descriptors *fp1* and *fp2* refer to the same file.
422 Accepts a :term:`path-like object`.
427 Return ``True`` if the stat tuples *stat1* and *stat2* refer to the same file.
438 Accepts a :term:`path-like object`.
441 .. function:: split(path)
443 Split the pathname *path* into a pair, ``(head, tail)`` where *tail* is the
444 last pathname component and *head* is everything leading up to that. The
445 *tail* part will never contain a slash; if *path* ends in a slash, *tail*
446 will be empty. If there is no slash in *path*, *head* will be empty. If
447 *path* is empty, both *head* and *tail* are empty. Trailing slashes are
449 all cases, ``join(head, tail)`` returns a path to the same location as *path*
454 Accepts a :term:`path-like object`.
457 .. function:: splitdrive(path)
459 Split the pathname *path* into a pair ``(drive, tail)`` where *drive* is either
462 + tail`` will be the same as *path*.
464 On Windows, splits a pathname into drive/UNC sharepoint and relative path.
466 If the path contains a drive letter, drive will contain everything
467 up to and including the colon::
472 If the path contains a UNC path, drive will contain the host name
473 and share, up to but not including the fourth separator::
479 Accepts a :term:`path-like object`.
482 .. function:: splitext(path)
484 Split the pathname *path* into a pair ``(root, ext)`` such that ``root + ext ==
485 path``, and the extension, *ext*, is empty or begins with a period and contains at
488 If the path contains no extension, *ext* will be ``''``::
493 If the path contains an extension, then *ext* will be set to this extension,
501 Leading periods of the last component of the path are considered to
510 Accepts a :term:`path-like object`.