/Doc/library/os.path.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 333 lines · 207 code · 126 blank · 0 comment · 0 complexity · ccac2ba3566536ea52eed0e68e52d40c MD5 · raw file

  1. :mod:`os.path` --- Common pathname manipulations
  2. ================================================
  3. .. module:: os.path
  4. :synopsis: Operations on pathnames.
  5. .. index:: single: path; operations
  6. This module implements some useful functions on pathnames. To read or
  7. write files see :func:`open`, and for accessing the filesystem see the
  8. :mod:`os` module.
  9. .. note::
  10. On Windows, many of these functions do not properly support UNC pathnames.
  11. :func:`splitunc` and :func:`ismount` do handle them correctly.
  12. .. note::
  13. Since different operating systems have different path name conventions, there
  14. are several versions of this module in the standard library. The
  15. :mod:`os.path` module is always the path module suitable for the operating
  16. system Python is running on, and therefore usable for local paths. However,
  17. you can also import and use the individual modules if you want to manipulate
  18. a path that is *always* in one of the different formats. They all have the
  19. same interface:
  20. * :mod:`posixpath` for UNIX-style paths
  21. * :mod:`ntpath` for Windows paths
  22. * :mod:`macpath` for old-style MacOS paths
  23. * :mod:`os2emxpath` for OS/2 EMX paths
  24. .. function:: abspath(path)
  25. Return a normalized absolutized version of the pathname *path*. On most
  26. platforms, this is equivalent to ``normpath(join(os.getcwd(), path))``.
  27. .. versionadded:: 1.5.2
  28. .. function:: basename(path)
  29. Return the base name of pathname *path*. This is the second half of the pair
  30. returned by ``split(path)``. Note that the result of this function is different
  31. from the Unix :program:`basename` program; where :program:`basename` for
  32. ``'/foo/bar/'`` returns ``'bar'``, the :func:`basename` function returns an
  33. empty string (``''``).
  34. .. function:: commonprefix(list)
  35. Return the longest path prefix (taken character-by-character) that is a prefix
  36. of all paths in *list*. If *list* is empty, return the empty string (``''``).
  37. Note that this may return invalid paths because it works a character at a time.
  38. .. function:: dirname(path)
  39. Return the directory name of pathname *path*. This is the first half of the
  40. pair returned by ``split(path)``.
  41. .. function:: exists(path)
  42. Return ``True`` if *path* refers to an existing path. Returns ``False`` for
  43. broken symbolic links. On some platforms, this function may return ``False`` if
  44. permission is not granted to execute :func:`os.stat` on the requested file, even
  45. if the *path* physically exists.
  46. .. function:: lexists(path)
  47. Return ``True`` if *path* refers to an existing path. Returns ``True`` for
  48. broken symbolic links. Equivalent to :func:`exists` on platforms lacking
  49. :func:`os.lstat`.
  50. .. versionadded:: 2.4
  51. .. function:: expanduser(path)
  52. On Unix and Windows, return the argument with an initial component of ``~`` or
  53. ``~user`` replaced by that *user*'s home directory.
  54. .. index:: module: pwd
  55. On Unix, an initial ``~`` is replaced by the environment variable :envvar:`HOME`
  56. if it is set; otherwise the current user's home directory is looked up in the
  57. password directory through the built-in module :mod:`pwd`. An initial ``~user``
  58. is looked up directly in the password directory.
  59. On Windows, :envvar:`HOME` and :envvar:`USERPROFILE` will be used if set,
  60. otherwise a combination of :envvar:`HOMEPATH` and :envvar:`HOMEDRIVE` will be
  61. used. An initial ``~user`` is handled by stripping the last directory component
  62. from the created user path derived above.
  63. If the expansion fails or if the path does not begin with a tilde, the path is
  64. returned unchanged.
  65. .. function:: expandvars(path)
  66. Return the argument with environment variables expanded. Substrings of the form
  67. ``$name`` or ``${name}`` are replaced by the value of environment variable
  68. *name*. Malformed variable names and references to non-existing variables are
  69. left unchanged.
  70. On Windows, ``%name%`` expansions are supported in addition to ``$name`` and
  71. ``${name}``.
  72. .. function:: getatime(path)
  73. Return the time of last access of *path*. The return value is a number giving
  74. the number of seconds since the epoch (see the :mod:`time` module). Raise
  75. :exc:`os.error` if the file does not exist or is inaccessible.
  76. .. versionadded:: 1.5.2
  77. .. versionchanged:: 2.3
  78. If :func:`os.stat_float_times` returns True, the result is a floating point
  79. number.
  80. .. function:: getmtime(path)
  81. Return the time of last modification of *path*. The return value is a number
  82. giving the number of seconds since the epoch (see the :mod:`time` module).
  83. Raise :exc:`os.error` if the file does not exist or is inaccessible.
  84. .. versionadded:: 1.5.2
  85. .. versionchanged:: 2.3
  86. If :func:`os.stat_float_times` returns True, the result is a floating point
  87. number.
  88. .. function:: getctime(path)
  89. Return the system's ctime which, on some systems (like Unix) is the time of the
  90. last change, and, on others (like Windows), is the creation time for *path*.
  91. The return value is a number giving the number of seconds since the epoch (see
  92. the :mod:`time` module). Raise :exc:`os.error` if the file does not exist or
  93. is inaccessible.
  94. .. versionadded:: 2.3
  95. .. function:: getsize(path)
  96. Return the size, in bytes, of *path*. Raise :exc:`os.error` if the file does
  97. not exist or is inaccessible.
  98. .. versionadded:: 1.5.2
  99. .. function:: isabs(path)
  100. Return ``True`` if *path* is an absolute pathname. On Unix, that means it
  101. begins with a slash, on Windows that it begins with a (back)slash after chopping
  102. off a potential drive letter.
  103. .. function:: isfile(path)
  104. Return ``True`` if *path* is an existing regular file. This follows symbolic
  105. links, so both :func:`islink` and :func:`isfile` can be true for the same path.
  106. .. function:: isdir(path)
  107. Return ``True`` if *path* is an existing directory. This follows symbolic
  108. links, so both :func:`islink` and :func:`isdir` can be true for the same path.
  109. .. function:: islink(path)
  110. Return ``True`` if *path* refers to a directory entry that is a symbolic link.
  111. Always ``False`` if symbolic links are not supported.
  112. .. function:: ismount(path)
  113. Return ``True`` if pathname *path* is a :dfn:`mount point`: a point in a file
  114. system where a different file system has been mounted. The function checks
  115. whether *path*'s parent, :file:`path/..`, is on a different device than *path*,
  116. or whether :file:`path/..` and *path* point to the same i-node on the same
  117. device --- this should detect mount points for all Unix and POSIX variants.
  118. .. function:: join(path1[, path2[, ...]])
  119. Join one or more path components intelligently. If any component is an absolute
  120. path, all previous components (on Windows, including the previous drive letter,
  121. if there was one) are thrown away, and joining continues. The return value is
  122. the concatenation of *path1*, and optionally *path2*, etc., with exactly one
  123. directory separator (``os.sep``) inserted between components, unless *path2* is
  124. empty. Note that on Windows, since there is a current directory for each drive,
  125. ``os.path.join("c:", "foo")`` represents a path relative to the current
  126. directory on drive :file:`C:` (:file:`c:foo`), not :file:`c:\\foo`.
  127. .. function:: normcase(path)
  128. Normalize the case of a pathname. On Unix and Mac OS X, this returns the
  129. path unchanged; on case-insensitive filesystems, it converts the path to
  130. lowercase. On Windows, it also converts forward slashes to backward slashes.
  131. .. function:: normpath(path)
  132. Normalize a pathname. This collapses redundant separators and up-level
  133. references so that ``A//B``, ``A/./B`` and ``A/foo/../B`` all become ``A/B``.
  134. It does not normalize the case (use :func:`normcase` for that). On Windows, it
  135. converts forward slashes to backward slashes. It should be understood that this
  136. may change the meaning of the path if it contains symbolic links!
  137. .. function:: realpath(path)
  138. Return the canonical path of the specified filename, eliminating any symbolic
  139. links encountered in the path (if they are supported by the operating system).
  140. .. versionadded:: 2.2
  141. .. function:: relpath(path[, start])
  142. Return a relative filepath to *path* either from the current directory or from
  143. an optional *start* point.
  144. *start* defaults to :attr:`os.curdir`. Availability: Windows, Unix.
  145. .. versionadded:: 2.6
  146. .. function:: samefile(path1, path2)
  147. Return ``True`` if both pathname arguments refer to the same file or directory
  148. (as indicated by device number and i-node number). Raise an exception if a
  149. :func:`os.stat` call on either pathname fails. Availability: Unix.
  150. .. function:: sameopenfile(fp1, fp2)
  151. Return ``True`` if the file descriptors *fp1* and *fp2* refer to the same file.
  152. Availability: Unix.
  153. .. function:: samestat(stat1, stat2)
  154. Return ``True`` if the stat tuples *stat1* and *stat2* refer to the same file.
  155. These structures may have been returned by :func:`fstat`, :func:`lstat`, or
  156. :func:`stat`. This function implements the underlying comparison used by
  157. :func:`samefile` and :func:`sameopenfile`. Availability: Unix.
  158. .. function:: split(path)
  159. Split the pathname *path* into a pair, ``(head, tail)`` where *tail* is the last
  160. pathname component and *head* is everything leading up to that. The *tail* part
  161. will never contain a slash; if *path* ends in a slash, *tail* will be empty. If
  162. there is no slash in *path*, *head* will be empty. If *path* is empty, both
  163. *head* and *tail* are empty. Trailing slashes are stripped from *head* unless
  164. it is the root (one or more slashes only). In nearly all cases, ``join(head,
  165. tail)`` equals *path* (the only exception being when there were multiple slashes
  166. separating *head* from *tail*).
  167. .. function:: splitdrive(path)
  168. Split the pathname *path* into a pair ``(drive, tail)`` where *drive* is either
  169. a drive specification or the empty string. On systems which do not use drive
  170. specifications, *drive* will always be the empty string. In all cases, ``drive
  171. + tail`` will be the same as *path*.
  172. .. versionadded:: 1.3
  173. .. function:: splitext(path)
  174. Split the pathname *path* into a pair ``(root, ext)`` such that ``root + ext ==
  175. path``, and *ext* is empty or begins with a period and contains at most one
  176. period. Leading periods on the basename are ignored; ``splitext('.cshrc')``
  177. returns ``('.cshrc', '')``.
  178. .. versionchanged:: 2.6
  179. Earlier versions could produce an empty root when the only period was the
  180. first character.
  181. .. function:: splitunc(path)
  182. Split the pathname *path* into a pair ``(unc, rest)`` so that *unc* is the UNC
  183. mount point (such as ``r'\\host\mount'``), if present, and *rest* the rest of
  184. the path (such as ``r'\path\file.ext'``). For paths containing drive letters,
  185. *unc* will always be the empty string. Availability: Windows.
  186. .. function:: walk(path, visit, arg)
  187. Calls the function *visit* with arguments ``(arg, dirname, names)`` for each
  188. directory in the directory tree rooted at *path* (including *path* itself, if it
  189. is a directory). The argument *dirname* specifies the visited directory, the
  190. argument *names* lists the files in the directory (gotten from
  191. ``os.listdir(dirname)``). The *visit* function may modify *names* to influence
  192. the set of directories visited below *dirname*, e.g. to avoid visiting certain
  193. parts of the tree. (The object referred to by *names* must be modified in
  194. place, using :keyword:`del` or slice assignment.)
  195. .. note::
  196. Symbolic links to directories are not treated as subdirectories, and that
  197. :func:`walk` therefore will not visit them. To visit linked directories you must
  198. identify them with ``os.path.islink(file)`` and ``os.path.isdir(file)``, and
  199. invoke :func:`walk` as necessary.
  200. .. note::
  201. This function is deprecated and has been removed in 3.0 in favor of
  202. :func:`os.walk`.
  203. .. data:: supports_unicode_filenames
  204. True if arbitrary Unicode strings can be used as file names (within limitations
  205. imposed by the file system), and if :func:`os.listdir` returns Unicode strings
  206. for a Unicode argument.
  207. .. versionadded:: 2.3