blob: 99d106b655ef61db7a6441d988a188c3cc635ef2 [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
Georg Brandl116aa622007-08-15 14:28:22 +00007.. index:: single: path; operations
8
9This module implements some useful functions on pathnames. To read or
10write files see :func:`open`, and for accessing the filesystem see the
Martin v. Löwis651423c2008-10-07 07:03:04 +000011:mod:`os` module. The path parameters can be passed as either strings,
12or bytes. Applications are encouraged to represent file names as
13(Unicode) character strings. Unfortunately, some file names may not be
14representable as strings on Unix, so applications that need to support
15arbitrary file names on Unix should use bytes objects to represent
16path names. Vice versa, using bytes objects cannot represent all file
17names on Windows (in the standard ``mbcs`` encoding), hence Windows
18applications should use string objects to access all files.
Georg Brandl116aa622007-08-15 14:28:22 +000019
Georg Brandl76e55382008-10-08 16:34:57 +000020.. note::
21
22 All of these functions accept either only bytes or only string objects as
23 their parameters. The result is an object of the same type, if a path or
24 file name is returned.
25
Georg Brandle720c0a2009-04-27 16:20:50 +000026.. note::
Georg Brandl116aa622007-08-15 14:28:22 +000027
28 On Windows, many of these functions do not properly support UNC pathnames.
29 :func:`splitunc` and :func:`ismount` do handle them correctly.
30
Benjamin Petersond23f8222009-04-05 19:13:16 +000031.. note::
32
33 Since different operating systems have different path name conventions, there
34 are several versions of this module in the standard library. The
35 :mod:`os.path` module is always the path module suitable for the operating
36 system Python is running on, and therefore usable for local paths. However,
37 you can also import and use the individual modules if you want to manipulate
38 a path that is *always* in one of the different formats. They all have the
39 same interface:
40
41 * :mod:`posixpath` for UNIX-style paths
42 * :mod:`ntpath` for Windows paths
43 * :mod:`macpath` for old-style MacOS paths
44 * :mod:`os2emxpath` for OS/2 EMX paths
45
46
Georg Brandl116aa622007-08-15 14:28:22 +000047.. function:: abspath(path)
48
49 Return a normalized absolutized version of the pathname *path*. On most
50 platforms, this is equivalent to ``normpath(join(os.getcwd(), path))``.
51
Georg Brandl116aa622007-08-15 14:28:22 +000052
53.. function:: basename(path)
54
55 Return the base name of pathname *path*. This is the second half of the pair
56 returned by ``split(path)``. Note that the result of this function is different
57 from the Unix :program:`basename` program; where :program:`basename` for
58 ``'/foo/bar/'`` returns ``'bar'``, the :func:`basename` function returns an
59 empty string (``''``).
60
61
62.. function:: commonprefix(list)
63
64 Return the longest path prefix (taken character-by-character) that is a prefix
65 of all paths in *list*. If *list* is empty, return the empty string (``''``).
66 Note that this may return invalid paths because it works a character at a time.
67
68
69.. function:: dirname(path)
70
71 Return the directory name of pathname *path*. This is the first half of the
72 pair returned by ``split(path)``.
73
74
75.. function:: exists(path)
76
77 Return ``True`` if *path* refers to an existing path. Returns ``False`` for
78 broken symbolic links. On some platforms, this function may return ``False`` if
79 permission is not granted to execute :func:`os.stat` on the requested file, even
80 if the *path* physically exists.
81
82
83.. function:: lexists(path)
84
85 Return ``True`` if *path* refers to an existing path. Returns ``True`` for
86 broken symbolic links. Equivalent to :func:`exists` on platforms lacking
87 :func:`os.lstat`.
88
Georg Brandl116aa622007-08-15 14:28:22 +000089
90.. function:: expanduser(path)
91
92 On Unix and Windows, return the argument with an initial component of ``~`` or
93 ``~user`` replaced by that *user*'s home directory.
94
95 .. index:: module: pwd
96
97 On Unix, an initial ``~`` is replaced by the environment variable :envvar:`HOME`
98 if it is set; otherwise the current user's home directory is looked up in the
99 password directory through the built-in module :mod:`pwd`. An initial ``~user``
100 is looked up directly in the password directory.
101
102 On Windows, :envvar:`HOME` and :envvar:`USERPROFILE` will be used if set,
103 otherwise a combination of :envvar:`HOMEPATH` and :envvar:`HOMEDRIVE` will be
104 used. An initial ``~user`` is handled by stripping the last directory component
105 from the created user path derived above.
106
107 If the expansion fails or if the path does not begin with a tilde, the path is
108 returned unchanged.
109
110
111.. function:: expandvars(path)
112
113 Return the argument with environment variables expanded. Substrings of the form
114 ``$name`` or ``${name}`` are replaced by the value of environment variable
115 *name*. Malformed variable names and references to non-existing variables are
116 left unchanged.
117
118 On Windows, ``%name%`` expansions are supported in addition to ``$name`` and
119 ``${name}``.
120
121
122.. function:: getatime(path)
123
124 Return the time of last access of *path*. The return value is a number giving
125 the number of seconds since the epoch (see the :mod:`time` module). Raise
126 :exc:`os.error` if the file does not exist or is inaccessible.
127
Georg Brandl55ac8f02007-09-01 13:51:09 +0000128 If :func:`os.stat_float_times` returns True, the result is a floating point
129 number.
Georg Brandl116aa622007-08-15 14:28:22 +0000130
131
132.. function:: getmtime(path)
133
134 Return the time of last modification of *path*. The return value is a number
135 giving the number of seconds since the epoch (see the :mod:`time` module).
136 Raise :exc:`os.error` if the file does not exist or is inaccessible.
137
Georg Brandl55ac8f02007-09-01 13:51:09 +0000138 If :func:`os.stat_float_times` returns True, the result is a floating point
139 number.
Georg Brandl116aa622007-08-15 14:28:22 +0000140
141
142.. function:: getctime(path)
143
144 Return the system's ctime which, on some systems (like Unix) is the time of the
145 last change, and, on others (like Windows), is the creation time for *path*.
146 The return value is a number giving the number of seconds since the epoch (see
147 the :mod:`time` module). Raise :exc:`os.error` if the file does not exist or
148 is inaccessible.
149
Georg Brandl116aa622007-08-15 14:28:22 +0000150
151.. function:: getsize(path)
152
153 Return the size, in bytes, of *path*. Raise :exc:`os.error` if the file does
154 not exist or is inaccessible.
155
Georg Brandl116aa622007-08-15 14:28:22 +0000156
157.. function:: isabs(path)
158
Christian Heimesaf98da12008-01-27 15:18:18 +0000159 Return ``True`` if *path* is an absolute pathname. On Unix, that means it
160 begins with a slash, on Windows that it begins with a (back)slash after chopping
161 off a potential drive letter.
Georg Brandl116aa622007-08-15 14:28:22 +0000162
163
164.. function:: isfile(path)
165
166 Return ``True`` if *path* is an existing regular file. This follows symbolic
167 links, so both :func:`islink` and :func:`isfile` can be true for the same path.
168
169
170.. function:: isdir(path)
171
172 Return ``True`` if *path* is an existing directory. This follows symbolic
173 links, so both :func:`islink` and :func:`isdir` can be true for the same path.
174
175
176.. function:: islink(path)
177
178 Return ``True`` if *path* refers to a directory entry that is a symbolic link.
179 Always ``False`` if symbolic links are not supported.
180
181
182.. function:: ismount(path)
183
184 Return ``True`` if pathname *path* is a :dfn:`mount point`: a point in a file
185 system where a different file system has been mounted. The function checks
186 whether *path*'s parent, :file:`path/..`, is on a different device than *path*,
187 or whether :file:`path/..` and *path* point to the same i-node on the same
188 device --- this should detect mount points for all Unix and POSIX variants.
189
190
191.. function:: join(path1[, path2[, ...]])
192
193 Join one or more path components intelligently. If any component is an absolute
194 path, all previous components (on Windows, including the previous drive letter,
195 if there was one) are thrown away, and joining continues. The return value is
196 the concatenation of *path1*, and optionally *path2*, etc., with exactly one
197 directory separator (``os.sep``) inserted between components, unless *path2* is
198 empty. Note that on Windows, since there is a current directory for each drive,
199 ``os.path.join("c:", "foo")`` represents a path relative to the current
200 directory on drive :file:`C:` (:file:`c:foo`), not :file:`c:\\foo`.
201
202
203.. function:: normcase(path)
204
Benjamin Petersond23f8222009-04-05 19:13:16 +0000205 Normalize the case of a pathname. On Unix and Mac OS X, this returns the
206 path unchanged; on case-insensitive filesystems, it converts the path to
207 lowercase. On Windows, it also converts forward slashes to backward slashes.
Georg Brandl116aa622007-08-15 14:28:22 +0000208
209
210.. function:: normpath(path)
211
212 Normalize a pathname. This collapses redundant separators and up-level
213 references so that ``A//B``, ``A/./B`` and ``A/foo/../B`` all become ``A/B``.
214 It does not normalize the case (use :func:`normcase` for that). On Windows, it
215 converts forward slashes to backward slashes. It should be understood that this
216 may change the meaning of the path if it contains symbolic links!
217
218
219.. function:: realpath(path)
220
221 Return the canonical path of the specified filename, eliminating any symbolic
222 links encountered in the path (if they are supported by the operating system).
223
Georg Brandl116aa622007-08-15 14:28:22 +0000224
225.. function:: relpath(path[, start])
226
227 Return a relative filepath to *path* either from the current directory or from
228 an optional *start* point.
229
230 *start* defaults to :attr:`os.curdir`. Availability: Windows, Unix.
231
Georg Brandl116aa622007-08-15 14:28:22 +0000232
233.. function:: samefile(path1, path2)
234
235 Return ``True`` if both pathname arguments refer to the same file or directory
236 (as indicated by device number and i-node number). Raise an exception if a
Georg Brandlc575c902008-09-13 17:46:05 +0000237 :func:`os.stat` call on either pathname fails. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000238
239
240.. function:: sameopenfile(fp1, fp2)
241
242 Return ``True`` if the file descriptors *fp1* and *fp2* refer to the same file.
Georg Brandlc575c902008-09-13 17:46:05 +0000243 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000244
245
246.. function:: samestat(stat1, stat2)
247
248 Return ``True`` if the stat tuples *stat1* and *stat2* refer to the same file.
249 These structures may have been returned by :func:`fstat`, :func:`lstat`, or
250 :func:`stat`. This function implements the underlying comparison used by
Georg Brandlc575c902008-09-13 17:46:05 +0000251 :func:`samefile` and :func:`sameopenfile`. Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000252
253
254.. function:: split(path)
255
256 Split the pathname *path* into a pair, ``(head, tail)`` where *tail* is the last
257 pathname component and *head* is everything leading up to that. The *tail* part
258 will never contain a slash; if *path* ends in a slash, *tail* will be empty. If
259 there is no slash in *path*, *head* will be empty. If *path* is empty, both
260 *head* and *tail* are empty. Trailing slashes are stripped from *head* unless
261 it is the root (one or more slashes only). In nearly all cases, ``join(head,
262 tail)`` equals *path* (the only exception being when there were multiple slashes
263 separating *head* from *tail*).
264
265
266.. function:: splitdrive(path)
267
268 Split the pathname *path* into a pair ``(drive, tail)`` where *drive* is either
269 a drive specification or the empty string. On systems which do not use drive
270 specifications, *drive* will always be the empty string. In all cases, ``drive
271 + tail`` will be the same as *path*.
272
Georg Brandl116aa622007-08-15 14:28:22 +0000273
274.. function:: splitext(path)
275
276 Split the pathname *path* into a pair ``(root, ext)`` such that ``root + ext ==
277 path``, and *ext* is empty or begins with a period and contains at most one
278 period. Leading periods on the basename are ignored; ``splitext('.cshrc')``
279 returns ``('.cshrc', '')``.
280
Georg Brandl116aa622007-08-15 14:28:22 +0000281
282.. function:: splitunc(path)
283
284 Split the pathname *path* into a pair ``(unc, rest)`` so that *unc* is the UNC
285 mount point (such as ``r'\\host\mount'``), if present, and *rest* the rest of
286 the path (such as ``r'\path\file.ext'``). For paths containing drive letters,
287 *unc* will always be the empty string. Availability: Windows.
288
289
Georg Brandle720c0a2009-04-27 16:20:50 +0000290<<<<<<< .working
291=======
292.. function:: walk(path, visit, arg)
293
294 Calls the function *visit* with arguments ``(arg, dirname, names)`` for each
295 directory in the directory tree rooted at *path* (including *path* itself, if it
296 is a directory). The argument *dirname* specifies the visited directory, the
297 argument *names* lists the files in the directory (gotten from
298 ``os.listdir(dirname)``). The *visit* function may modify *names* to influence
299 the set of directories visited below *dirname*, e.g. to avoid visiting certain
300 parts of the tree. (The object referred to by *names* must be modified in
301 place, using :keyword:`del` or slice assignment.)
302
303 .. note::
304
305 Symbolic links to directories are not treated as subdirectories, and that
306 :func:`walk` therefore will not visit them. To visit linked directories you must
307 identify them with ``os.path.islink(file)`` and ``os.path.isdir(file)``, and
308 invoke :func:`walk` as necessary.
309
310 .. note::
311
312 This function is deprecated and has been removed in 3.0 in favor of
313 :func:`os.walk`.
314
315
316>>>>>>> .merge-right.r72009
Georg Brandl116aa622007-08-15 14:28:22 +0000317.. data:: supports_unicode_filenames
318
319 True if arbitrary Unicode strings can be used as file names (within limitations
Georg Brandlf6945182008-02-01 11:56:49 +0000320 imposed by the file system), and if :func:`os.listdir` returns strings that
321 contain characters that cannot be represented by ASCII.