blob: 2c14f8f485c78f1b3fedba0f3ee8cf1857dec770 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`os` --- Miscellaneous operating system interfaces
2=======================================================
3
4.. module:: os
5 :synopsis: Miscellaneous operating system interfaces.
6
7
Christian Heimesa62da1d2008-01-12 19:39:10 +00008This module provides a portable way of using operating system dependent
9functionality. If you just want to read or write a file see :func:`open`, if
10you want to manipulate paths, see the :mod:`os.path` module, and if you want to
11read all the lines in all the files on the command line see the :mod:`fileinput`
12module. For creating temporary files and directories see the :mod:`tempfile`
13module, and for high-level file and directory handling see the :mod:`shutil`
14module.
Georg Brandl116aa622007-08-15 14:28:22 +000015
Benjamin Peterson1baf4652009-12-31 03:11:23 +000016Notes on the availability of these functions:
Georg Brandl116aa622007-08-15 14:28:22 +000017
Benjamin Peterson1baf4652009-12-31 03:11:23 +000018* The design of all built-in operating system dependent modules of Python is
19 such that as long as the same functionality is available, it uses the same
20 interface; for example, the function ``os.stat(path)`` returns stat
21 information about *path* in the same format (which happens to have originated
22 with the POSIX interface).
Georg Brandl116aa622007-08-15 14:28:22 +000023
Benjamin Peterson1baf4652009-12-31 03:11:23 +000024* Extensions peculiar to a particular operating system are also available
25 through the :mod:`os` module, but using them is of course a threat to
26 portability.
Georg Brandl116aa622007-08-15 14:28:22 +000027
Benjamin Peterson1baf4652009-12-31 03:11:23 +000028* All functions accepting path or file names accept both bytes and string
29 objects, and result in an object of the same type, if a path or file name is
30 returned.
Georg Brandl76e55382008-10-08 16:34:57 +000031
Benjamin Peterson1baf4652009-12-31 03:11:23 +000032* An "Availability: Unix" note means that this function is commonly found on
33 Unix systems. It does not make any claims about its existence on a specific
34 operating system.
35
36* If not separately noted, all functions that claim "Availability: Unix" are
37 supported on Mac OS X, which builds on a Unix core.
38
Benjamin Petersonf650e462010-05-06 23:03:05 +000039.. Availability notes get their own line and occur at the end of the function
40.. documentation.
41
Georg Brandlc575c902008-09-13 17:46:05 +000042.. note::
43
Christian Heimesa62da1d2008-01-12 19:39:10 +000044 All functions in this module raise :exc:`OSError` in the case of invalid or
45 inaccessible file names and paths, or other arguments that have the correct
46 type, but are not accepted by the operating system.
Georg Brandl116aa622007-08-15 14:28:22 +000047
Georg Brandl116aa622007-08-15 14:28:22 +000048.. exception:: error
49
Christian Heimesa62da1d2008-01-12 19:39:10 +000050 An alias for the built-in :exc:`OSError` exception.
Georg Brandl116aa622007-08-15 14:28:22 +000051
52
53.. data:: name
54
Benjamin Peterson1baf4652009-12-31 03:11:23 +000055 The name of the operating system dependent module imported. The following
Ned Deily5c867012014-06-26 23:40:06 -070056 names have currently been registered: ``'posix'``, ``'nt'``,
Jesus Ceaf6c2e892012-10-05 01:11:10 +020057 ``'ce'``, ``'java'``.
Georg Brandl116aa622007-08-15 14:28:22 +000058
Antoine Pitroua83cdaa2011-07-09 15:54:23 +020059 .. seealso::
60 :attr:`sys.platform` has a finer granularity. :func:`os.uname` gives
61 system-dependent version information.
62
63 The :mod:`platform` module provides detailed checks for the
64 system's identity.
65
Georg Brandl116aa622007-08-15 14:28:22 +000066
Martin v. Löwis011e8422009-05-05 04:43:17 +000067.. _os-filenames:
Victor Stinner6bfd8542014-06-19 12:50:27 +020068.. _filesystem-encoding:
Martin v. Löwis011e8422009-05-05 04:43:17 +000069
70File Names, Command Line Arguments, and Environment Variables
71-------------------------------------------------------------
72
Georg Brandl67b21b72010-08-17 15:07:14 +000073In Python, file names, command line arguments, and environment variables are
74represented using the string type. On some systems, decoding these strings to
75and from bytes is necessary before passing them to the operating system. Python
76uses the file system encoding to perform this conversion (see
77:func:`sys.getfilesystemencoding`).
Martin v. Löwis011e8422009-05-05 04:43:17 +000078
79.. versionchanged:: 3.1
Georg Brandl67b21b72010-08-17 15:07:14 +000080 On some systems, conversion using the file system encoding may fail. In this
Victor Stinnerf6a271a2014-08-01 12:28:48 +020081 case, Python uses the :ref:`surrogateescape encoding error handler
82 <surrogateescape>`, which means that undecodable bytes are replaced by a
83 Unicode character U+DCxx on decoding, and these are again translated to the
84 original byte on encoding.
Martin v. Löwis011e8422009-05-05 04:43:17 +000085
86
Georg Brandl67b21b72010-08-17 15:07:14 +000087The file system encoding must guarantee to successfully decode all bytes
88below 128. If the file system encoding fails to provide this guarantee, API
89functions may raise UnicodeErrors.
Martin v. Löwis011e8422009-05-05 04:43:17 +000090
91
Georg Brandl116aa622007-08-15 14:28:22 +000092.. _os-procinfo:
93
94Process Parameters
95------------------
96
97These functions and data items provide information and operate on the current
98process and user.
99
100
Georg Brandl8ccadaa2012-06-24 12:50:06 +0200101.. function:: ctermid()
102
103 Return the filename corresponding to the controlling terminal of the process.
104
105 Availability: Unix.
106
107
Georg Brandl116aa622007-08-15 14:28:22 +0000108.. data:: environ
109
Chris Jerdonek11f3f172012-11-03 12:05:55 -0700110 A :term:`mapping` object representing the string environment. For example,
Georg Brandl116aa622007-08-15 14:28:22 +0000111 ``environ['HOME']`` is the pathname of your home directory (on some platforms),
112 and is equivalent to ``getenv("HOME")`` in C.
113
114 This mapping is captured the first time the :mod:`os` module is imported,
115 typically during Python startup as part of processing :file:`site.py`. Changes
116 to the environment made after this time are not reflected in ``os.environ``,
117 except for changes made by modifying ``os.environ`` directly.
118
119 If the platform supports the :func:`putenv` function, this mapping may be used
120 to modify the environment as well as query the environment. :func:`putenv` will
121 be called automatically when the mapping is modified.
122
Victor Stinner84ae1182010-05-06 22:05:07 +0000123 On Unix, keys and values use :func:`sys.getfilesystemencoding` and
124 ``'surrogateescape'`` error handler. Use :data:`environb` if you would like
125 to use a different encoding.
126
Georg Brandl116aa622007-08-15 14:28:22 +0000127 .. note::
128
129 Calling :func:`putenv` directly does not change ``os.environ``, so it's better
130 to modify ``os.environ``.
131
132 .. note::
133
Georg Brandlc575c902008-09-13 17:46:05 +0000134 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
135 cause memory leaks. Refer to the system documentation for
Georg Brandl60203b42010-10-06 10:11:56 +0000136 :c:func:`putenv`.
Georg Brandl116aa622007-08-15 14:28:22 +0000137
138 If :func:`putenv` is not provided, a modified copy of this mapping may be
139 passed to the appropriate process-creation functions to cause child processes
140 to use a modified environment.
141
Georg Brandl9afde1c2007-11-01 20:32:30 +0000142 If the platform supports the :func:`unsetenv` function, you can delete items in
Georg Brandl116aa622007-08-15 14:28:22 +0000143 this mapping to unset environment variables. :func:`unsetenv` will be called
Georg Brandl9afde1c2007-11-01 20:32:30 +0000144 automatically when an item is deleted from ``os.environ``, and when
145 one of the :meth:`pop` or :meth:`clear` methods is called.
146
Georg Brandl116aa622007-08-15 14:28:22 +0000147
Victor Stinner84ae1182010-05-06 22:05:07 +0000148.. data:: environb
149
Chris Jerdonek11f3f172012-11-03 12:05:55 -0700150 Bytes version of :data:`environ`: a :term:`mapping` object representing the
Victor Stinner84ae1182010-05-06 22:05:07 +0000151 environment as byte strings. :data:`environ` and :data:`environb` are
152 synchronized (modify :data:`environb` updates :data:`environ`, and vice
153 versa).
154
Victor Stinnerb745a742010-05-18 17:17:23 +0000155 :data:`environb` is only available if :data:`supports_bytes_environ` is
156 True.
Victor Stinner84ae1182010-05-06 22:05:07 +0000157
Benjamin Peterson662c74f2010-05-06 22:09:03 +0000158 .. versionadded:: 3.2
159
Victor Stinner84ae1182010-05-06 22:05:07 +0000160
Georg Brandl116aa622007-08-15 14:28:22 +0000161.. function:: chdir(path)
162 fchdir(fd)
163 getcwd()
164 :noindex:
165
166 These functions are described in :ref:`os-file-dir`.
167
168
Victor Stinnere8d51452010-08-19 01:05:19 +0000169.. function:: fsencode(filename)
Victor Stinner449c4662010-05-08 11:10:09 +0000170
Victor Stinnere8d51452010-08-19 01:05:19 +0000171 Encode *filename* to the filesystem encoding with ``'surrogateescape'``
Victor Stinner62165d62010-10-09 10:34:37 +0000172 error handler, or ``'strict'`` on Windows; return :class:`bytes` unchanged.
Victor Stinnere8d51452010-08-19 01:05:19 +0000173
Antoine Pitroua305ca72010-09-25 22:12:00 +0000174 :func:`fsdecode` is the reverse function.
Victor Stinnere8d51452010-08-19 01:05:19 +0000175
176 .. versionadded:: 3.2
177
178
179.. function:: fsdecode(filename)
180
181 Decode *filename* from the filesystem encoding with ``'surrogateescape'``
Victor Stinner62165d62010-10-09 10:34:37 +0000182 error handler, or ``'strict'`` on Windows; return :class:`str` unchanged.
Victor Stinnere8d51452010-08-19 01:05:19 +0000183
184 :func:`fsencode` is the reverse function.
Victor Stinner449c4662010-05-08 11:10:09 +0000185
186 .. versionadded:: 3.2
187
188
Georg Brandl8ccadaa2012-06-24 12:50:06 +0200189.. function:: getenv(key, default=None)
190
191 Return the value of the environment variable *key* if it exists, or
192 *default* if it doesn't. *key*, *default* and the result are str.
193
194 On Unix, keys and values are decoded with :func:`sys.getfilesystemencoding`
195 and ``'surrogateescape'`` error handler. Use :func:`os.getenvb` if you
196 would like to use a different encoding.
197
198 Availability: most flavors of Unix, Windows.
199
200
201.. function:: getenvb(key, default=None)
202
203 Return the value of the environment variable *key* if it exists, or
204 *default* if it doesn't. *key*, *default* and the result are bytes.
205
206 Availability: most flavors of Unix.
207
208 .. versionadded:: 3.2
209
210
Gregory P. Smithb6e8c7e2010-02-27 07:22:22 +0000211.. function:: get_exec_path(env=None)
212
213 Returns the list of directories that will be searched for a named
214 executable, similar to a shell, when launching a process.
215 *env*, when specified, should be an environment variable dictionary
216 to lookup the PATH in.
217 By default, when *env* is None, :data:`environ` is used.
218
219 .. versionadded:: 3.2
220
221
Georg Brandl116aa622007-08-15 14:28:22 +0000222.. function:: getegid()
223
224 Return the effective group id of the current process. This corresponds to the
Benjamin Petersonf650e462010-05-06 23:03:05 +0000225 "set id" bit on the file being executed in the current process.
226
227 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000228
229
230.. function:: geteuid()
231
232 .. index:: single: user; effective id
233
Benjamin Petersonf650e462010-05-06 23:03:05 +0000234 Return the current process's effective user id.
235
236 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000237
238
239.. function:: getgid()
240
241 .. index:: single: process; group
242
Benjamin Petersonf650e462010-05-06 23:03:05 +0000243 Return the real group id of the current process.
244
245 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000246
247
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +0200248.. function:: getgrouplist(user, group)
249
250 Return list of group ids that *user* belongs to. If *group* is not in the
251 list, it is included; typically, *group* is specified as the group ID
252 field from the password record for *user*.
253
254 Availability: Unix.
255
256 .. versionadded:: 3.3
257
258
Georg Brandl116aa622007-08-15 14:28:22 +0000259.. function:: getgroups()
260
261 Return list of supplemental group ids associated with the current process.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000262
Georg Brandl116aa622007-08-15 14:28:22 +0000263 Availability: Unix.
264
Larry Hastings3732ed22014-03-15 21:13:56 -0700265 .. note::
266
267 On Mac OS X, :func:`getgroups` behavior differs somewhat from
Ned Deily2e209682012-04-30 11:14:02 -0700268 other Unix platforms. If the Python interpreter was built with a
269 deployment target of :const:`10.5` or earlier, :func:`getgroups` returns
270 the list of effective group ids associated with the current user process;
271 this list is limited to a system-defined number of entries, typically 16,
272 and may be modified by calls to :func:`setgroups` if suitably privileged.
273 If built with a deployment target greater than :const:`10.5`,
274 :func:`getgroups` returns the current group access list for the user
275 associated with the effective user id of the process; the group access
276 list may change over the lifetime of the process, it is not affected by
277 calls to :func:`setgroups`, and its length is not limited to 16. The
278 deployment target value, :const:`MACOSX_DEPLOYMENT_TARGET`, can be
279 obtained with :func:`sysconfig.get_config_var`.
280
Georg Brandl116aa622007-08-15 14:28:22 +0000281
282.. function:: getlogin()
283
284 Return the name of the user logged in on the controlling terminal of the
Benjamin Petersone218bcb2014-08-30 21:04:15 -0400285 process. For most purposes, it is more useful to use the environment
286 variables :envvar:`LOGNAME` or :envvar:`USERNAME` to find out who the user
287 is, or ``pwd.getpwuid(os.getuid())[0]`` to get the login name of the current
288 real user id.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000289
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000290 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000291
292
293.. function:: getpgid(pid)
294
295 Return the process group id of the process with process id *pid*. If *pid* is 0,
Benjamin Petersonf650e462010-05-06 23:03:05 +0000296 the process group id of the current process is returned.
Georg Brandl116aa622007-08-15 14:28:22 +0000297
Benjamin Petersonf650e462010-05-06 23:03:05 +0000298 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000299
300.. function:: getpgrp()
301
302 .. index:: single: process; group
303
Benjamin Petersonf650e462010-05-06 23:03:05 +0000304 Return the id of the current process group.
305
306 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000307
308
309.. function:: getpid()
310
311 .. index:: single: process; id
312
Benjamin Petersonf650e462010-05-06 23:03:05 +0000313 Return the current process id.
314
315 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000316
317
318.. function:: getppid()
319
320 .. index:: single: process; id of parent
321
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000322 Return the parent's process id. When the parent process has exited, on Unix
323 the id returned is the one of the init process (1), on Windows it is still
324 the same id, which may be already reused by another process.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000325
Georg Brandl8a5555f2012-06-24 13:29:09 +0200326 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000327
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000328 .. versionchanged:: 3.2
329 Added support for Windows.
Georg Brandl1b83a452009-11-28 11:12:26 +0000330
Georg Brandl8a5555f2012-06-24 13:29:09 +0200331
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +0000332.. function:: getpriority(which, who)
333
334 .. index:: single: process; scheduling priority
335
Georg Brandl8ccadaa2012-06-24 12:50:06 +0200336 Get program scheduling priority. The value *which* is one of
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +0000337 :const:`PRIO_PROCESS`, :const:`PRIO_PGRP`, or :const:`PRIO_USER`, and *who*
338 is interpreted relative to *which* (a process identifier for
339 :const:`PRIO_PROCESS`, process group identifier for :const:`PRIO_PGRP`, and a
Georg Brandl8ccadaa2012-06-24 12:50:06 +0200340 user ID for :const:`PRIO_USER`). A zero value for *who* denotes
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +0000341 (respectively) the calling process, the process group of the calling process,
342 or the real user ID of the calling process.
343
Georg Brandl8ccadaa2012-06-24 12:50:06 +0200344 Availability: Unix.
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +0000345
346 .. versionadded:: 3.3
347
Georg Brandl8ccadaa2012-06-24 12:50:06 +0200348
349.. data:: PRIO_PROCESS
350 PRIO_PGRP
351 PRIO_USER
352
353 Parameters for the :func:`getpriority` and :func:`setpriority` functions.
354
355 Availability: Unix.
356
357 .. versionadded:: 3.3
358
359
Gregory P. Smithcf02c6a2009-11-27 17:54:17 +0000360.. function:: getresuid()
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000361
362 Return a tuple (ruid, euid, suid) denoting the current process's
Benjamin Petersonf650e462010-05-06 23:03:05 +0000363 real, effective, and saved user ids.
364
365 Availability: Unix.
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000366
Georg Brandl1b83a452009-11-28 11:12:26 +0000367 .. versionadded:: 3.2
368
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000369
Gregory P. Smithcf02c6a2009-11-27 17:54:17 +0000370.. function:: getresgid()
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000371
372 Return a tuple (rgid, egid, sgid) denoting the current process's
Georg Brandla9b51d22010-09-05 17:07:12 +0000373 real, effective, and saved group ids.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000374
375 Availability: Unix.
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000376
Georg Brandl1b83a452009-11-28 11:12:26 +0000377 .. versionadded:: 3.2
378
Georg Brandl116aa622007-08-15 14:28:22 +0000379
380.. function:: getuid()
381
382 .. index:: single: user; id
383
Benjamin Peterson4bb09c82014-06-07 13:50:34 -0700384 Return the current process's real user id.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000385
386 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000387
388
Georg Brandl8ccadaa2012-06-24 12:50:06 +0200389.. function:: initgroups(username, gid)
Georg Brandl116aa622007-08-15 14:28:22 +0000390
Georg Brandl8ccadaa2012-06-24 12:50:06 +0200391 Call the system initgroups() to initialize the group access list with all of
392 the groups of which the specified username is a member, plus the specified
393 group id.
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +0000394
395 Availability: Unix.
396
Georg Brandl8ccadaa2012-06-24 12:50:06 +0200397 .. versionadded:: 3.2
398
Georg Brandl116aa622007-08-15 14:28:22 +0000399
Georg Brandl18244152009-09-02 20:34:52 +0000400.. function:: putenv(key, value)
Georg Brandl116aa622007-08-15 14:28:22 +0000401
402 .. index:: single: environment variables; setting
403
Georg Brandl18244152009-09-02 20:34:52 +0000404 Set the environment variable named *key* to the string *value*. Such
Georg Brandl116aa622007-08-15 14:28:22 +0000405 changes to the environment affect subprocesses started with :func:`os.system`,
Benjamin Petersonf650e462010-05-06 23:03:05 +0000406 :func:`popen` or :func:`fork` and :func:`execv`.
407
408 Availability: most flavors of Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000409
410 .. note::
411
Georg Brandlc575c902008-09-13 17:46:05 +0000412 On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may
413 cause memory leaks. Refer to the system documentation for putenv.
Georg Brandl116aa622007-08-15 14:28:22 +0000414
415 When :func:`putenv` is supported, assignments to items in ``os.environ`` are
416 automatically translated into corresponding calls to :func:`putenv`; however,
417 calls to :func:`putenv` don't update ``os.environ``, so it is actually
418 preferable to assign to items of ``os.environ``.
419
420
421.. function:: setegid(egid)
422
Benjamin Petersonf650e462010-05-06 23:03:05 +0000423 Set the current process's effective group id.
424
425 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000426
427
428.. function:: seteuid(euid)
429
Benjamin Petersonf650e462010-05-06 23:03:05 +0000430 Set the current process's effective user id.
431
432 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000433
434
435.. function:: setgid(gid)
436
Benjamin Petersonf650e462010-05-06 23:03:05 +0000437 Set the current process' group id.
438
439 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000440
441
442.. function:: setgroups(groups)
443
444 Set the list of supplemental group ids associated with the current process to
445 *groups*. *groups* must be a sequence, and each element must be an integer
Christian Heimesfaf2f632008-01-06 16:59:19 +0000446 identifying a group. This operation is typically available only to the superuser.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000447
Georg Brandl116aa622007-08-15 14:28:22 +0000448 Availability: Unix.
449
Ned Deily2e209682012-04-30 11:14:02 -0700450 .. note:: On Mac OS X, the length of *groups* may not exceed the
451 system-defined maximum number of effective group ids, typically 16.
452 See the documentation for :func:`getgroups` for cases where it may not
453 return the same group list set by calling setgroups().
Georg Brandl116aa622007-08-15 14:28:22 +0000454
455.. function:: setpgrp()
456
Andrew Svetlova2fe3342012-08-11 21:14:08 +0300457 Call the system call :c:func:`setpgrp` or ``setpgrp(0, 0)`` depending on
Georg Brandl116aa622007-08-15 14:28:22 +0000458 which version is implemented (if any). See the Unix manual for the semantics.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000459
Georg Brandl116aa622007-08-15 14:28:22 +0000460 Availability: Unix.
461
462
463.. function:: setpgid(pid, pgrp)
464
Georg Brandl60203b42010-10-06 10:11:56 +0000465 Call the system call :c:func:`setpgid` to set the process group id of the
Georg Brandl116aa622007-08-15 14:28:22 +0000466 process with id *pid* to the process group with id *pgrp*. See the Unix manual
Benjamin Petersonf650e462010-05-06 23:03:05 +0000467 for the semantics.
468
469 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000470
471
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +0000472.. function:: setpriority(which, who, priority)
473
474 .. index:: single: process; scheduling priority
475
476 Set program scheduling priority. The value *which* is one of
477 :const:`PRIO_PROCESS`, :const:`PRIO_PGRP`, or :const:`PRIO_USER`, and *who*
478 is interpreted relative to *which* (a process identifier for
479 :const:`PRIO_PROCESS`, process group identifier for :const:`PRIO_PGRP`, and a
480 user ID for :const:`PRIO_USER`). A zero value for *who* denotes
481 (respectively) the calling process, the process group of the calling process,
482 or the real user ID of the calling process.
483 *priority* is a value in the range -20 to 19. The default priority is 0;
484 lower priorities cause more favorable scheduling.
485
486 Availability: Unix
487
488 .. versionadded:: 3.3
489
490
Georg Brandl116aa622007-08-15 14:28:22 +0000491.. function:: setregid(rgid, egid)
492
Benjamin Petersonf650e462010-05-06 23:03:05 +0000493 Set the current process's real and effective group ids.
494
495 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000496
Georg Brandl1b83a452009-11-28 11:12:26 +0000497
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000498.. function:: setresgid(rgid, egid, sgid)
499
500 Set the current process's real, effective, and saved group ids.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000501
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000502 Availability: Unix.
503
Georg Brandl1b83a452009-11-28 11:12:26 +0000504 .. versionadded:: 3.2
505
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000506
507.. function:: setresuid(ruid, euid, suid)
508
509 Set the current process's real, effective, and saved user ids.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000510
Georg Brandl6faee4e2010-09-21 14:48:28 +0000511 Availability: Unix.
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000512
Georg Brandl1b83a452009-11-28 11:12:26 +0000513 .. versionadded:: 3.2
514
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000515
516.. function:: setreuid(ruid, euid)
517
Benjamin Petersonf650e462010-05-06 23:03:05 +0000518 Set the current process's real and effective user ids.
519
520 Availability: Unix.
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000521
Georg Brandl116aa622007-08-15 14:28:22 +0000522
523.. function:: getsid(pid)
524
Georg Brandl60203b42010-10-06 10:11:56 +0000525 Call the system call :c:func:`getsid`. See the Unix manual for the semantics.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000526
Georg Brandl116aa622007-08-15 14:28:22 +0000527 Availability: Unix.
528
Georg Brandl116aa622007-08-15 14:28:22 +0000529
530.. function:: setsid()
531
Georg Brandl60203b42010-10-06 10:11:56 +0000532 Call the system call :c:func:`setsid`. See the Unix manual for the semantics.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000533
Georg Brandl116aa622007-08-15 14:28:22 +0000534 Availability: Unix.
535
536
537.. function:: setuid(uid)
538
539 .. index:: single: user; id, setting
540
Benjamin Petersonf650e462010-05-06 23:03:05 +0000541 Set the current process's user id.
542
543 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000544
Georg Brandl116aa622007-08-15 14:28:22 +0000545
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000546.. placed in this section since it relates to errno.... a little weak
Georg Brandl116aa622007-08-15 14:28:22 +0000547.. function:: strerror(code)
548
549 Return the error message corresponding to the error code in *code*.
Georg Brandl60203b42010-10-06 10:11:56 +0000550 On platforms where :c:func:`strerror` returns ``NULL`` when given an unknown
Benjamin Petersonf650e462010-05-06 23:03:05 +0000551 error number, :exc:`ValueError` is raised.
552
553 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000554
555
Victor Stinnerb745a742010-05-18 17:17:23 +0000556.. data:: supports_bytes_environ
557
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200558 ``True`` if the native OS type of the environment is bytes (eg. ``False`` on
Victor Stinnerb745a742010-05-18 17:17:23 +0000559 Windows).
560
Victor Stinner8fddc9e2010-05-18 17:24:09 +0000561 .. versionadded:: 3.2
562
Victor Stinnerb745a742010-05-18 17:17:23 +0000563
Georg Brandl116aa622007-08-15 14:28:22 +0000564.. function:: umask(mask)
565
Benjamin Petersonf650e462010-05-06 23:03:05 +0000566 Set the current numeric umask and return the previous umask.
567
568 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000569
570
571.. function:: uname()
572
573 .. index::
574 single: gethostname() (in module socket)
575 single: gethostbyaddr() (in module socket)
576
Larry Hastings605a62d2012-06-24 04:33:36 -0700577 Returns information identifying the current operating system.
578 The return value is an object with five attributes:
579
580 * :attr:`sysname` - operating system name
581 * :attr:`nodename` - name of machine on network (implementation-defined)
582 * :attr:`release` - operating system release
583 * :attr:`version` - operating system version
584 * :attr:`machine` - hardware identifier
585
586 For backwards compatibility, this object is also iterable, behaving
587 like a five-tuple containing :attr:`sysname`, :attr:`nodename`,
588 :attr:`release`, :attr:`version`, and :attr:`machine`
589 in that order.
590
591 Some systems truncate :attr:`nodename` to 8 characters or to the
Georg Brandl116aa622007-08-15 14:28:22 +0000592 leading component; a better way to get the hostname is
593 :func:`socket.gethostname` or even
Benjamin Petersonf650e462010-05-06 23:03:05 +0000594 ``socket.gethostbyaddr(socket.gethostname())``.
595
596 Availability: recent flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000597
Larry Hastings605a62d2012-06-24 04:33:36 -0700598 .. versionchanged:: 3.3
599 Return type changed from a tuple to a tuple-like object
600 with named attributes.
601
Georg Brandl116aa622007-08-15 14:28:22 +0000602
Georg Brandl18244152009-09-02 20:34:52 +0000603.. function:: unsetenv(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000604
605 .. index:: single: environment variables; deleting
606
Georg Brandl18244152009-09-02 20:34:52 +0000607 Unset (delete) the environment variable named *key*. Such changes to the
Georg Brandl116aa622007-08-15 14:28:22 +0000608 environment affect subprocesses started with :func:`os.system`, :func:`popen` or
Benjamin Petersonf650e462010-05-06 23:03:05 +0000609 :func:`fork` and :func:`execv`.
Georg Brandl116aa622007-08-15 14:28:22 +0000610
611 When :func:`unsetenv` is supported, deletion of items in ``os.environ`` is
612 automatically translated into a corresponding call to :func:`unsetenv`; however,
613 calls to :func:`unsetenv` don't update ``os.environ``, so it is actually
614 preferable to delete items of ``os.environ``.
615
Benjamin Petersonf650e462010-05-06 23:03:05 +0000616 Availability: most flavors of Unix, Windows.
617
Georg Brandl116aa622007-08-15 14:28:22 +0000618
619.. _os-newstreams:
620
621File Object Creation
622--------------------
623
Georg Brandla570e982012-06-24 13:26:22 +0200624This function creates new :term:`file objects <file object>`. (See also
Georg Brandlb2462e22012-06-24 13:24:56 +0200625:func:`~os.open` for opening file descriptors.)
Georg Brandl116aa622007-08-15 14:28:22 +0000626
627
Petri Lehtinen1a01ebc2012-05-24 21:44:07 +0300628.. function:: fdopen(fd, *args, **kwargs)
Georg Brandl116aa622007-08-15 14:28:22 +0000629
Georg Brandlb2462e22012-06-24 13:24:56 +0200630 Return an open file object connected to the file descriptor *fd*. This is an
631 alias of the :func:`open` built-in function and accepts the same arguments.
632 The only difference is that the first argument of :func:`fdopen` must always
633 be an integer.
Georg Brandl116aa622007-08-15 14:28:22 +0000634
Georg Brandl116aa622007-08-15 14:28:22 +0000635
Georg Brandl116aa622007-08-15 14:28:22 +0000636.. _os-fd-ops:
637
638File Descriptor Operations
639--------------------------
640
641These functions operate on I/O streams referenced using file descriptors.
642
643File descriptors are small integers corresponding to a file that has been opened
644by the current process. For example, standard input is usually file descriptor
6450, standard output is 1, and standard error is 2. Further files opened by a
646process will then be assigned 3, 4, 5, and so forth. The name "file descriptor"
647is slightly deceptive; on Unix platforms, sockets and pipes are also referenced
648by file descriptors.
649
Serhiy Storchakadab83542013-10-13 20:12:43 +0300650The :meth:`~io.IOBase.fileno` method can be used to obtain the file descriptor
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000651associated with a :term:`file object` when required. Note that using the file
Benjamin Peterson08bf91c2010-04-11 16:12:57 +0000652descriptor directly will bypass the file object methods, ignoring aspects such
653as internal buffering of data.
Georg Brandl116aa622007-08-15 14:28:22 +0000654
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000655
Georg Brandl116aa622007-08-15 14:28:22 +0000656.. function:: close(fd)
657
Benjamin Petersonf650e462010-05-06 23:03:05 +0000658 Close file descriptor *fd*.
659
660 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000661
662 .. note::
663
664 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000665 descriptor as returned by :func:`os.open` or :func:`pipe`. To close a "file
Georg Brandl116aa622007-08-15 14:28:22 +0000666 object" returned by the built-in function :func:`open` or by :func:`popen` or
Serhiy Storchakadab83542013-10-13 20:12:43 +0300667 :func:`fdopen`, use its :meth:`~io.IOBase.close` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000668
669
Christian Heimesfdab48e2008-01-20 09:06:41 +0000670.. function:: closerange(fd_low, fd_high)
671
672 Close all file descriptors from *fd_low* (inclusive) to *fd_high* (exclusive),
Georg Brandlb1a1ac02012-06-24 11:54:07 +0200673 ignoring errors. Equivalent to (but much faster than)::
Christian Heimesfdab48e2008-01-20 09:06:41 +0000674
Georg Brandlc9a5a0e2009-09-01 07:34:27 +0000675 for fd in range(fd_low, fd_high):
Christian Heimesfdab48e2008-01-20 09:06:41 +0000676 try:
677 os.close(fd)
678 except OSError:
679 pass
680
Benjamin Petersonf650e462010-05-06 23:03:05 +0000681 Availability: Unix, Windows.
682
Christian Heimesfdab48e2008-01-20 09:06:41 +0000683
Georg Brandl81f11302007-12-21 08:45:42 +0000684.. function:: device_encoding(fd)
685
686 Return a string describing the encoding of the device associated with *fd*
687 if it is connected to a terminal; else return :const:`None`.
688
689
Georg Brandl116aa622007-08-15 14:28:22 +0000690.. function:: dup(fd)
691
Victor Stinnerdaf45552013-08-28 00:53:59 +0200692 Return a duplicate of file descriptor *fd*. The new file descriptor is
693 :ref:`non-inheritable <fd_inheritance>`.
694
695 On Windows, when duplicating a standard stream (0: stdin, 1: stdout,
696 2: stderr), the new file descriptor is :ref:`inheritable
697 <fd_inheritance>`.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000698
699 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000700
Victor Stinnerdaf45552013-08-28 00:53:59 +0200701 .. versionchanged:: 3.4
702 The new file descriptor is now non-inheritable.
Georg Brandl116aa622007-08-15 14:28:22 +0000703
Victor Stinnerdaf45552013-08-28 00:53:59 +0200704
705.. function:: dup2(fd, fd2, inheritable=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000706
707 Duplicate file descriptor *fd* to *fd2*, closing the latter first if necessary.
Victor Stinnerdaf45552013-08-28 00:53:59 +0200708 The file descriptor *fd2* is :ref:`inheritable <fd_inheritance>` by default,
709 or non-inheritable if *inheritable* is ``False``.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000710
Georg Brandlc575c902008-09-13 17:46:05 +0000711 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000712
Victor Stinnerdaf45552013-08-28 00:53:59 +0200713 .. versionchanged:: 3.4
714 Add the optional *inheritable* parameter.
715
Georg Brandl116aa622007-08-15 14:28:22 +0000716
Christian Heimes4e30a842007-11-30 22:12:06 +0000717.. function:: fchmod(fd, mode)
718
Georg Brandlb9df00c2012-06-24 12:38:14 +0200719 Change the mode of the file given by *fd* to the numeric *mode*. See the
Georg Brandl4d399a42012-06-25 07:40:32 +0200720 docs for :func:`chmod` for possible values of *mode*. As of Python 3.3, this
Georg Brandlb9df00c2012-06-24 12:38:14 +0200721 is equivalent to ``os.chmod(fd, mode)``.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000722
723 Availability: Unix.
Christian Heimes4e30a842007-11-30 22:12:06 +0000724
725
726.. function:: fchown(fd, uid, gid)
727
728 Change the owner and group id of the file given by *fd* to the numeric *uid*
Georg Brandlb9df00c2012-06-24 12:38:14 +0200729 and *gid*. To leave one of the ids unchanged, set it to -1. See
Georg Brandl4d399a42012-06-25 07:40:32 +0200730 :func:`chown`. As of Python 3.3, this is equivalent to ``os.chown(fd, uid,
Georg Brandlb9df00c2012-06-24 12:38:14 +0200731 gid)``.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000732
Christian Heimes4e30a842007-11-30 22:12:06 +0000733 Availability: Unix.
734
735
Georg Brandl116aa622007-08-15 14:28:22 +0000736.. function:: fdatasync(fd)
737
738 Force write of file with filedescriptor *fd* to disk. Does not force update of
Benjamin Petersonf650e462010-05-06 23:03:05 +0000739 metadata.
740
741 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000742
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000743 .. note::
744 This function is not available on MacOS.
745
Georg Brandl116aa622007-08-15 14:28:22 +0000746
747.. function:: fpathconf(fd, name)
748
749 Return system configuration information relevant to an open file. *name*
750 specifies the configuration value to retrieve; it may be a string which is the
751 name of a defined system value; these names are specified in a number of
752 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
753 additional names as well. The names known to the host operating system are
754 given in the ``pathconf_names`` dictionary. For configuration variables not
755 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandl116aa622007-08-15 14:28:22 +0000756
757 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
758 specific value for *name* is not supported by the host system, even if it is
759 included in ``pathconf_names``, an :exc:`OSError` is raised with
760 :const:`errno.EINVAL` for the error number.
761
Georg Brandl4d399a42012-06-25 07:40:32 +0200762 As of Python 3.3, this is equivalent to ``os.pathconf(fd, name)``.
Georg Brandl306336b2012-06-24 12:55:33 +0200763
Senthil Kumaran2a97cee2013-09-19 00:08:56 -0700764 Availability: Unix.
765
Georg Brandl116aa622007-08-15 14:28:22 +0000766
Victor Stinner4195b5c2012-02-08 23:03:19 +0100767.. function:: fstat(fd)
Georg Brandl116aa622007-08-15 14:28:22 +0000768
Victor Stinner6d4f4fe2014-07-24 12:42:16 +0200769 Get the status of the file descriptor *fd*. Return a :class:`stat_result`
770 object.
771
772 As of Python 3.3, this is equivalent to ``os.stat(fd)``.
773
774 .. seealso::
775
776 The :func:`stat` function.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000777
778 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000779
Georg Brandlb1a1ac02012-06-24 11:54:07 +0200780
Georg Brandl116aa622007-08-15 14:28:22 +0000781.. function:: fstatvfs(fd)
782
Georg Brandlb9df00c2012-06-24 12:38:14 +0200783 Return information about the filesystem containing the file associated with
Georg Brandl4d399a42012-06-25 07:40:32 +0200784 file descriptor *fd*, like :func:`statvfs`. As of Python 3.3, this is
Georg Brandlb9df00c2012-06-24 12:38:14 +0200785 equivalent to ``os.statvfs(fd)``.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000786
787 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000788
789
790.. function:: fsync(fd)
791
792 Force write of file with filedescriptor *fd* to disk. On Unix, this calls the
Georg Brandl60203b42010-10-06 10:11:56 +0000793 native :c:func:`fsync` function; on Windows, the MS :c:func:`_commit` function.
Georg Brandl116aa622007-08-15 14:28:22 +0000794
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000795 If you're starting with a buffered Python :term:`file object` *f*, first do
796 ``f.flush()``, and then do ``os.fsync(f.fileno())``, to ensure that all internal
797 buffers associated with *f* are written to disk.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000798
Georg Brandl8a5555f2012-06-24 13:29:09 +0200799 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000800
801
802.. function:: ftruncate(fd, length)
803
Georg Brandl306336b2012-06-24 12:55:33 +0200804 Truncate the file corresponding to file descriptor *fd*, so that it is at
Georg Brandl4d399a42012-06-25 07:40:32 +0200805 most *length* bytes in size. As of Python 3.3, this is equivalent to
Georg Brandl306336b2012-06-24 12:55:33 +0200806 ``os.truncate(fd, length)``.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000807
808 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000809
810
Victor Stinner1db9e7b2014-07-29 22:32:47 +0200811.. function:: get_blocking(fd)
812
813 Get the blocking mode of the file descriptor: ``False`` if the
814 :data:`O_NONBLOCK` flag is set, ``True`` if the flag is cleared.
815
816 See also :func:`set_blocking` and :meth:`socket.socket.setblocking`.
817
818 Availability: Unix.
819
820 .. versionadded:: 3.5
821
Georg Brandl116aa622007-08-15 14:28:22 +0000822.. function:: isatty(fd)
823
824 Return ``True`` if the file descriptor *fd* is open and connected to a
Benjamin Petersonf650e462010-05-06 23:03:05 +0000825 tty(-like) device, else ``False``.
826
Georg Brandl116aa622007-08-15 14:28:22 +0000827
Ross Lagerwall7807c352011-03-17 20:20:30 +0200828.. function:: lockf(fd, cmd, len)
829
830 Apply, test or remove a POSIX lock on an open file descriptor.
831 *fd* is an open file descriptor.
832 *cmd* specifies the command to use - one of :data:`F_LOCK`, :data:`F_TLOCK`,
833 :data:`F_ULOCK` or :data:`F_TEST`.
834 *len* specifies the section of the file to lock.
835
836 Availability: Unix.
837
838 .. versionadded:: 3.3
839
840
841.. data:: F_LOCK
842 F_TLOCK
843 F_ULOCK
844 F_TEST
845
846 Flags that specify what action :func:`lockf` will take.
847
848 Availability: Unix.
849
850 .. versionadded:: 3.3
851
Georg Brandlf62445a2012-06-24 13:31:20 +0200852
Georg Brandl116aa622007-08-15 14:28:22 +0000853.. function:: lseek(fd, pos, how)
854
Christian Heimesfaf2f632008-01-06 16:59:19 +0000855 Set the current position of file descriptor *fd* to position *pos*, modified
856 by *how*: :const:`SEEK_SET` or ``0`` to set the position relative to the
857 beginning of the file; :const:`SEEK_CUR` or ``1`` to set it relative to the
Serhiy Storchakadab83542013-10-13 20:12:43 +0300858 current position; :const:`SEEK_END` or ``2`` to set it relative to the end of
Victor Stinnere83f8992011-12-17 23:15:09 +0100859 the file. Return the new cursor position in bytes, starting from the beginning.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000860
861 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000862
863
Georg Brandl8569e582010-05-19 20:57:08 +0000864.. data:: SEEK_SET
865 SEEK_CUR
866 SEEK_END
867
868 Parameters to the :func:`lseek` function. Their values are 0, 1, and 2,
Georg Brandl8a5555f2012-06-24 13:29:09 +0200869 respectively.
870
871 Availability: Unix, Windows.
Georg Brandl8569e582010-05-19 20:57:08 +0000872
Jesus Cea94363612012-06-22 18:32:07 +0200873 .. versionadded:: 3.3
874 Some operating systems could support additional values, like
875 :data:`os.SEEK_HOLE` or :data:`os.SEEK_DATA`.
876
Georg Brandl8569e582010-05-19 20:57:08 +0000877
Larry Hastings9cf065c2012-06-22 16:30:09 -0700878.. function:: open(file, flags, mode=0o777, *, dir_fd=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000879
Georg Brandlf4a41232008-05-26 17:55:52 +0000880 Open the file *file* and set various flags according to *flags* and possibly
Larry Hastings9cf065c2012-06-22 16:30:09 -0700881 its mode according to *mode*. When computing *mode*, the current umask value
882 is first masked out. Return the file descriptor for the newly opened file.
Victor Stinnerdaf45552013-08-28 00:53:59 +0200883 The new file descriptor is :ref:`non-inheritable <fd_inheritance>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000884
885 For a description of the flag and mode values, see the C run-time documentation;
886 flag constants (like :const:`O_RDONLY` and :const:`O_WRONLY`) are defined in
Andrew Kuchling4921a082013-06-21 11:49:57 -0400887 the :mod:`os` module. In particular, on Windows adding
Georg Brandl8569e582010-05-19 20:57:08 +0000888 :const:`O_BINARY` is needed to open files in binary mode.
Georg Brandl116aa622007-08-15 14:28:22 +0000889
Georg Brandl50c40002012-06-24 11:45:20 +0200890 This function can support :ref:`paths relative to directory descriptors
Andrew Kuchling4921a082013-06-21 11:49:57 -0400891 <dir_fd>` with the *dir_fd* parameter.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700892
Benjamin Petersonf650e462010-05-06 23:03:05 +0000893 Availability: Unix, Windows.
894
Victor Stinnerdaf45552013-08-28 00:53:59 +0200895 .. versionchanged:: 3.4
896 The new file descriptor is now non-inheritable.
897
Georg Brandl116aa622007-08-15 14:28:22 +0000898 .. note::
899
Georg Brandl502d9a52009-07-26 15:02:41 +0000900 This function is intended for low-level I/O. For normal usage, use the
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000901 built-in function :func:`open`, which returns a :term:`file object` with
Jeroen Ruigrok van der Werven9c558bcf2010-07-13 14:47:01 +0000902 :meth:`~file.read` and :meth:`~file.write` methods (and many more). To
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000903 wrap a file descriptor in a file object, use :func:`fdopen`.
Georg Brandl116aa622007-08-15 14:28:22 +0000904
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000905 .. versionadded:: 3.3
Larry Hastings9cf065c2012-06-22 16:30:09 -0700906 The *dir_fd* argument.
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000907
Andrew Kuchling4921a082013-06-21 11:49:57 -0400908The following constants are options for the *flags* parameter to the
909:func:`~os.open` function. They can be combined using the bitwise OR operator
910``|``. Some of them are not available on all platforms. For descriptions of
911their availability and use, consult the :manpage:`open(2)` manual page on Unix
912or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Windows.
913
914
915.. data:: O_RDONLY
916 O_WRONLY
917 O_RDWR
918 O_APPEND
919 O_CREAT
920 O_EXCL
921 O_TRUNC
922
923 These constants are available on Unix and Windows.
924
925
926.. data:: O_DSYNC
927 O_RSYNC
928 O_SYNC
929 O_NDELAY
930 O_NONBLOCK
931 O_NOCTTY
932 O_SHLOCK
933 O_EXLOCK
934 O_CLOEXEC
935
936 These constants are only available on Unix.
937
938 .. versionchanged:: 3.3
939 Add :data:`O_CLOEXEC` constant.
940
941.. data:: O_BINARY
942 O_NOINHERIT
943 O_SHORT_LIVED
944 O_TEMPORARY
945 O_RANDOM
946 O_SEQUENTIAL
947 O_TEXT
948
949 These constants are only available on Windows.
950
951
952.. data:: O_ASYNC
953 O_DIRECT
954 O_DIRECTORY
955 O_NOFOLLOW
956 O_NOATIME
957 O_PATH
Christian Heimes177b3f92013-08-16 14:35:09 +0200958 O_TMPFILE
Andrew Kuchling4921a082013-06-21 11:49:57 -0400959
960 These constants are GNU extensions and not present if they are not defined by
961 the C library.
962
Christian Heimesd88f7352013-08-16 14:37:50 +0200963 .. versionchanged:: 3.4
Larry Hastings3732ed22014-03-15 21:13:56 -0700964 Add :data:`O_PATH` on systems that support it.
965 Add :data:`O_TMPFILE`, only available on Linux Kernel 3.11
966 or newer.
Christian Heimesd88f7352013-08-16 14:37:50 +0200967
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000968
Georg Brandl116aa622007-08-15 14:28:22 +0000969.. function:: openpty()
970
971 .. index:: module: pty
972
Victor Stinnerdaf45552013-08-28 00:53:59 +0200973 Open a new pseudo-terminal pair. Return a pair of file descriptors
974 ``(master, slave)`` for the pty and the tty, respectively. The new file
975 descriptors are :ref:`non-inheritable <fd_inheritance>`. For a (slightly) more
976 portable approach, use the :mod:`pty` module.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000977
978 Availability: some flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +0000979
Victor Stinnerdaf45552013-08-28 00:53:59 +0200980 .. versionchanged:: 3.4
981 The new file descriptors are now non-inheritable.
982
Georg Brandl116aa622007-08-15 14:28:22 +0000983
984.. function:: pipe()
985
Victor Stinnerdaf45552013-08-28 00:53:59 +0200986 Create a pipe. Return a pair of file descriptors ``(r, w)`` usable for
Victor Stinner05f31bf2013-11-06 01:48:45 +0100987 reading and writing, respectively. The new file descriptor is
Victor Stinnerdaf45552013-08-28 00:53:59 +0200988 :ref:`non-inheritable <fd_inheritance>`.
Benjamin Petersonf650e462010-05-06 23:03:05 +0000989
990 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +0000991
Victor Stinnerdaf45552013-08-28 00:53:59 +0200992 .. versionchanged:: 3.4
993 The new file descriptors are now non-inheritable.
994
Georg Brandl116aa622007-08-15 14:28:22 +0000995
Charles-François Natali368f34b2011-06-06 19:49:47 +0200996.. function:: pipe2(flags)
Charles-François Natalidaafdd52011-05-29 20:07:40 +0200997
998 Create a pipe with *flags* set atomically.
Charles-François Natali368f34b2011-06-06 19:49:47 +0200999 *flags* can be constructed by ORing together one or more of these values:
1000 :data:`O_NONBLOCK`, :data:`O_CLOEXEC`.
Charles-François Natalidaafdd52011-05-29 20:07:40 +02001001 Return a pair of file descriptors ``(r, w)`` usable for reading and writing,
1002 respectively.
1003
1004 Availability: some flavors of Unix.
1005
1006 .. versionadded:: 3.3
1007
1008
Ross Lagerwall7807c352011-03-17 20:20:30 +02001009.. function:: posix_fallocate(fd, offset, len)
1010
1011 Ensures that enough disk space is allocated for the file specified by *fd*
1012 starting from *offset* and continuing for *len* bytes.
1013
1014 Availability: Unix.
1015
1016 .. versionadded:: 3.3
1017
1018
1019.. function:: posix_fadvise(fd, offset, len, advice)
1020
1021 Announces an intention to access data in a specific pattern thus allowing
1022 the kernel to make optimizations.
1023 The advice applies to the region of the file specified by *fd* starting at
1024 *offset* and continuing for *len* bytes.
1025 *advice* is one of :data:`POSIX_FADV_NORMAL`, :data:`POSIX_FADV_SEQUENTIAL`,
1026 :data:`POSIX_FADV_RANDOM`, :data:`POSIX_FADV_NOREUSE`,
1027 :data:`POSIX_FADV_WILLNEED` or :data:`POSIX_FADV_DONTNEED`.
1028
1029 Availability: Unix.
1030
1031 .. versionadded:: 3.3
1032
1033
1034.. data:: POSIX_FADV_NORMAL
1035 POSIX_FADV_SEQUENTIAL
1036 POSIX_FADV_RANDOM
1037 POSIX_FADV_NOREUSE
1038 POSIX_FADV_WILLNEED
1039 POSIX_FADV_DONTNEED
1040
1041 Flags that can be used in *advice* in :func:`posix_fadvise` that specify
1042 the access pattern that is likely to be used.
1043
1044 Availability: Unix.
1045
1046 .. versionadded:: 3.3
1047
1048
1049.. function:: pread(fd, buffersize, offset)
1050
1051 Read from a file descriptor, *fd*, at a position of *offset*. It will read up
1052 to *buffersize* number of bytes. The file offset remains unchanged.
1053
1054 Availability: Unix.
1055
1056 .. versionadded:: 3.3
1057
1058
Jesus Cea67503c52014-10-20 16:18:24 +02001059.. function:: pwrite(fd, str, offset)
Ross Lagerwall7807c352011-03-17 20:20:30 +02001060
Jesus Cea67503c52014-10-20 16:18:24 +02001061 Write *bytestring* to a file descriptor, *fd*, from *offset*,
1062 leaving the file offset unchanged.
Ross Lagerwall7807c352011-03-17 20:20:30 +02001063
1064 Availability: Unix.
1065
1066 .. versionadded:: 3.3
1067
1068
Georg Brandl116aa622007-08-15 14:28:22 +00001069.. function:: read(fd, n)
1070
Georg Brandlb90be692009-07-29 16:14:16 +00001071 Read at most *n* bytes from file descriptor *fd*. Return a bytestring containing the
Georg Brandl116aa622007-08-15 14:28:22 +00001072 bytes read. If the end of the file referred to by *fd* has been reached, an
Benjamin Petersonf650e462010-05-06 23:03:05 +00001073 empty bytes object is returned.
1074
1075 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001076
1077 .. note::
1078
1079 This function is intended for low-level I/O and must be applied to a file
Georg Brandlb2462e22012-06-24 13:24:56 +02001080 descriptor as returned by :func:`os.open` or :func:`pipe`. To read a
1081 "file object" returned by the built-in function :func:`open` or by
1082 :func:`popen` or :func:`fdopen`, or :data:`sys.stdin`, use its
1083 :meth:`~file.read` or :meth:`~file.readline` methods.
Georg Brandl116aa622007-08-15 14:28:22 +00001084
1085
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001086.. function:: sendfile(out, in, offset, nbytes)
1087 sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)
1088
1089 Copy *nbytes* bytes from file descriptor *in* to file descriptor *out*
1090 starting at *offset*.
1091 Return the number of bytes sent. When EOF is reached return 0.
1092
1093 The first function notation is supported by all platforms that define
1094 :func:`sendfile`.
1095
1096 On Linux, if *offset* is given as ``None``, the bytes are read from the
1097 current position of *in* and the position of *in* is updated.
1098
1099 The second case may be used on Mac OS X and FreeBSD where *headers* and
1100 *trailers* are arbitrary sequences of buffers that are written before and
1101 after the data from *in* is written. It returns the same as the first case.
1102
1103 On Mac OS X and FreeBSD, a value of 0 for *nbytes* specifies to send until
1104 the end of *in* is reached.
1105
Charles-Francois Natalia771a1b2013-05-01 15:12:20 +02001106 All platforms support sockets as *out* file descriptor, and some platforms
1107 allow other types (e.g. regular file, pipe) as well.
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001108
Giampaolo Rodola'409569b2014-04-24 18:09:21 +02001109 Cross-platform applications should not use *headers*, *trailers* and *flags*
1110 arguments.
1111
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001112 Availability: Unix.
1113
Giampaolo Rodola'915d1412014-06-11 03:54:30 +02001114 .. note::
1115
Benjamin Peterson0ce95372014-06-15 18:30:27 -07001116 For a higher-level wrapper of :func:`sendfile`, see
1117 :mod:`socket.socket.sendfile`.
Giampaolo Rodola'915d1412014-06-11 03:54:30 +02001118
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001119 .. versionadded:: 3.3
1120
1121
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001122.. function:: set_blocking(fd, blocking)
1123
1124 Set the blocking mode of the specified file descriptor. Set the
1125 :data:`O_NONBLOCK` flag if blocking is ``False``, clear the flag otherwise.
1126
1127 See also :func:`get_blocking` and :meth:`socket.socket.setblocking`.
1128
1129 Availability: Unix.
1130
1131 .. versionadded:: 3.5
1132
1133
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001134.. data:: SF_NODISKIO
1135 SF_MNOWAIT
1136 SF_SYNC
1137
1138 Parameters to the :func:`sendfile` function, if the implementation supports
1139 them.
1140
1141 Availability: Unix.
1142
1143 .. versionadded:: 3.3
1144
1145
Ross Lagerwall7807c352011-03-17 20:20:30 +02001146.. function:: readv(fd, buffers)
1147
Benjamin Petersone83ed432014-01-18 22:54:59 -05001148 Read from a file descriptor *fd* into a number of mutable :term:`bytes-like
1149 objects <bytes-like object>` *buffers*. :func:`~os.readv` will transfer data
1150 into each buffer until it is full and then move on to the next buffer in the
1151 sequence to hold the rest of the data. :func:`~os.readv` returns the total
1152 number of bytes read (which may be less than the total capacity of all the
1153 objects).
Ross Lagerwall7807c352011-03-17 20:20:30 +02001154
1155 Availability: Unix.
1156
1157 .. versionadded:: 3.3
1158
1159
Georg Brandl116aa622007-08-15 14:28:22 +00001160.. function:: tcgetpgrp(fd)
1161
1162 Return the process group associated with the terminal given by *fd* (an open
Benjamin Petersonf650e462010-05-06 23:03:05 +00001163 file descriptor as returned by :func:`os.open`).
1164
1165 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001166
1167
1168.. function:: tcsetpgrp(fd, pg)
1169
1170 Set the process group associated with the terminal given by *fd* (an open file
Benjamin Petersonf650e462010-05-06 23:03:05 +00001171 descriptor as returned by :func:`os.open`) to *pg*.
1172
1173 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001174
1175
1176.. function:: ttyname(fd)
1177
1178 Return a string which specifies the terminal device associated with
Georg Brandl9afde1c2007-11-01 20:32:30 +00001179 file descriptor *fd*. If *fd* is not associated with a terminal device, an
Benjamin Petersonf650e462010-05-06 23:03:05 +00001180 exception is raised.
1181
1182 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001183
1184
1185.. function:: write(fd, str)
1186
Georg Brandlb90be692009-07-29 16:14:16 +00001187 Write the bytestring in *str* to file descriptor *fd*. Return the number of
Benjamin Petersonf650e462010-05-06 23:03:05 +00001188 bytes actually written.
1189
1190 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001191
1192 .. note::
1193
1194 This function is intended for low-level I/O and must be applied to a file
Benjamin Petersonfa0d7032009-06-01 22:42:33 +00001195 descriptor as returned by :func:`os.open` or :func:`pipe`. To write a "file
Georg Brandl116aa622007-08-15 14:28:22 +00001196 object" returned by the built-in function :func:`open` or by :func:`popen` or
Benjamin Petersonfa0d7032009-06-01 22:42:33 +00001197 :func:`fdopen`, or :data:`sys.stdout` or :data:`sys.stderr`, use its
1198 :meth:`~file.write` method.
Georg Brandl116aa622007-08-15 14:28:22 +00001199
Georg Brandl8569e582010-05-19 20:57:08 +00001200
Ross Lagerwall7807c352011-03-17 20:20:30 +02001201.. function:: writev(fd, buffers)
1202
Benjamin Petersone83ed432014-01-18 22:54:59 -05001203 Write the contents of *buffers* to file descriptor *fd*. *buffers* must be a
1204 sequence of :term:`bytes-like objects <bytes-like object>`.
1205 :func:`~os.writev` writes the contents of each object to the file descriptor
1206 and returns the total number of bytes written.
Ross Lagerwall7807c352011-03-17 20:20:30 +02001207
1208 Availability: Unix.
1209
1210 .. versionadded:: 3.3
1211
1212
Antoine Pitroubcf2b592012-02-08 23:28:36 +01001213.. _terminal-size:
1214
1215Querying the size of a terminal
1216~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1217
1218.. versionadded:: 3.3
1219
1220.. function:: get_terminal_size(fd=STDOUT_FILENO)
1221
1222 Return the size of the terminal window as ``(columns, lines)``,
1223 tuple of type :class:`terminal_size`.
1224
1225 The optional argument ``fd`` (default ``STDOUT_FILENO``, or standard
1226 output) specifies which file descriptor should be queried.
1227
1228 If the file descriptor is not connected to a terminal, an :exc:`OSError`
Andrew Svetlov5b898402012-12-18 21:26:36 +02001229 is raised.
Antoine Pitroubcf2b592012-02-08 23:28:36 +01001230
1231 :func:`shutil.get_terminal_size` is the high-level function which
1232 should normally be used, ``os.get_terminal_size`` is the low-level
1233 implementation.
1234
1235 Availability: Unix, Windows.
1236
Georg Brandl6cff9ff2012-06-24 14:05:40 +02001237.. class:: terminal_size
Antoine Pitroubcf2b592012-02-08 23:28:36 +01001238
Georg Brandl6cff9ff2012-06-24 14:05:40 +02001239 A subclass of tuple, holding ``(columns, lines)`` of the terminal window size.
Antoine Pitroubcf2b592012-02-08 23:28:36 +01001240
1241 .. attribute:: columns
1242
1243 Width of the terminal window in characters.
1244
1245 .. attribute:: lines
1246
1247 Height of the terminal window in characters.
1248
1249
Victor Stinnerdaf45552013-08-28 00:53:59 +02001250.. _fd_inheritance:
1251
1252Inheritance of File Descriptors
1253~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1254
Georg Brandl5642ff92013-09-15 10:37:57 +02001255.. versionadded:: 3.4
1256
1257A file descriptor has an "inheritable" flag which indicates if the file descriptor
1258can be inherited by child processes. Since Python 3.4, file descriptors
Victor Stinnerdaf45552013-08-28 00:53:59 +02001259created by Python are non-inheritable by default.
1260
1261On UNIX, non-inheritable file descriptors are closed in child processes at the
1262execution of a new program, other file descriptors are inherited.
1263
1264On Windows, non-inheritable handles and file descriptors are closed in child
Georg Brandl5642ff92013-09-15 10:37:57 +02001265processes, except for standard streams (file descriptors 0, 1 and 2: stdin, stdout
Serhiy Storchaka690a6a92013-10-13 20:13:37 +03001266and stderr), which are always inherited. Using :func:`spawn\* <spawnl>` functions,
Victor Stinnerdaf45552013-08-28 00:53:59 +02001267all inheritable handles and all inheritable file descriptors are inherited.
1268Using the :mod:`subprocess` module, all file descriptors except standard
Georg Brandl5642ff92013-09-15 10:37:57 +02001269streams are closed, and inheritable handles are only inherited if the
1270*close_fds* parameter is ``False``.
Victor Stinnerdaf45552013-08-28 00:53:59 +02001271
1272.. function:: get_inheritable(fd)
1273
Georg Brandl5642ff92013-09-15 10:37:57 +02001274 Get the "inheritable" flag of the specified file descriptor (a boolean).
Victor Stinnerdaf45552013-08-28 00:53:59 +02001275
1276.. function:: set_inheritable(fd, inheritable)
1277
Georg Brandl5642ff92013-09-15 10:37:57 +02001278 Set the "inheritable" flag of the specified file descriptor.
Victor Stinnerdaf45552013-08-28 00:53:59 +02001279
1280.. function:: get_handle_inheritable(handle)
1281
Georg Brandl5642ff92013-09-15 10:37:57 +02001282 Get the "inheritable" flag of the specified handle (a boolean).
Victor Stinnerdaf45552013-08-28 00:53:59 +02001283
1284 Availability: Windows.
1285
1286.. function:: set_handle_inheritable(handle, inheritable)
1287
Georg Brandl5642ff92013-09-15 10:37:57 +02001288 Set the "inheritable" flag of the specified handle.
Victor Stinnerdaf45552013-08-28 00:53:59 +02001289
1290 Availability: Windows.
1291
1292
Georg Brandl116aa622007-08-15 14:28:22 +00001293.. _os-file-dir:
1294
1295Files and Directories
1296---------------------
1297
Georg Brandl50c40002012-06-24 11:45:20 +02001298On some Unix platforms, many of these functions support one or more of these
1299features:
1300
1301.. _path_fd:
1302
Larry Hastings77892dc2012-06-25 03:27:33 -07001303* **specifying a file descriptor:**
1304 For some functions, the *path* argument can be not only a string giving a path
Georg Brandl50c40002012-06-24 11:45:20 +02001305 name, but also a file descriptor. The function will then operate on the file
Georg Brandlaceaf902012-06-25 08:33:56 +02001306 referred to by the descriptor. (For POSIX systems, Python will call the
1307 ``f...`` version of the function.)
Georg Brandl50c40002012-06-24 11:45:20 +02001308
1309 You can check whether or not *path* can be specified as a file descriptor on
1310 your platform using :data:`os.supports_fd`. If it is unavailable, using it
1311 will raise a :exc:`NotImplementedError`.
1312
1313 If the function also supports *dir_fd* or *follow_symlinks* arguments, it is
1314 an error to specify one of those when supplying *path* as a file descriptor.
1315
1316.. _dir_fd:
1317
Larry Hastings77892dc2012-06-25 03:27:33 -07001318* **paths relative to directory descriptors:** If *dir_fd* is not ``None``, it
Georg Brandl50c40002012-06-24 11:45:20 +02001319 should be a file descriptor referring to a directory, and the path to operate
1320 on should be relative; path will then be relative to that directory. If the
Georg Brandlaceaf902012-06-25 08:33:56 +02001321 path is absolute, *dir_fd* is ignored. (For POSIX systems, Python will call
Larry Hastings77892dc2012-06-25 03:27:33 -07001322 the ``...at`` or ``f...at`` version of the function.)
Georg Brandl50c40002012-06-24 11:45:20 +02001323
1324 You can check whether or not *dir_fd* is supported on your platform using
1325 :data:`os.supports_dir_fd`. If it is unavailable, using it will raise a
1326 :exc:`NotImplementedError`.
1327
1328.. _follow_symlinks:
1329
Larry Hastings77892dc2012-06-25 03:27:33 -07001330* **not following symlinks:** If *follow_symlinks* is
Georg Brandl50c40002012-06-24 11:45:20 +02001331 ``False``, and the last element of the path to operate on is a symbolic link,
1332 the function will operate on the symbolic link itself instead of the file the
Georg Brandlaceaf902012-06-25 08:33:56 +02001333 link points to. (For POSIX systems, Python will call the ``l...`` version of
Georg Brandl50c40002012-06-24 11:45:20 +02001334 the function.)
1335
1336 You can check whether or not *follow_symlinks* is supported on your platform
1337 using :data:`os.supports_follow_symlinks`. If it is unavailable, using it
1338 will raise a :exc:`NotImplementedError`.
1339
1340
1341
Larry Hastings9cf065c2012-06-22 16:30:09 -07001342.. function:: access(path, mode, *, dir_fd=None, effective_ids=False, follow_symlinks=True)
Georg Brandl116aa622007-08-15 14:28:22 +00001343
1344 Use the real uid/gid to test for access to *path*. Note that most operations
1345 will use the effective uid/gid, therefore this routine can be used in a
1346 suid/sgid environment to test if the invoking user has the specified access to
1347 *path*. *mode* should be :const:`F_OK` to test the existence of *path*, or it
1348 can be the inclusive OR of one or more of :const:`R_OK`, :const:`W_OK`, and
1349 :const:`X_OK` to test permissions. Return :const:`True` if access is allowed,
1350 :const:`False` if not. See the Unix man page :manpage:`access(2)` for more
Benjamin Petersonf650e462010-05-06 23:03:05 +00001351 information.
1352
Georg Brandl50c40002012-06-24 11:45:20 +02001353 This function can support specifying :ref:`paths relative to directory
1354 descriptors <dir_fd>` and :ref:`not following symlinks <follow_symlinks>`.
Larry Hastings9cf065c2012-06-22 16:30:09 -07001355
1356 If *effective_ids* is ``True``, :func:`access` will perform its access
1357 checks using the effective uid/gid instead of the real uid/gid.
1358 *effective_ids* may not be supported on your platform; you can check whether
1359 or not it is available using :data:`os.supports_effective_ids`. If it is
1360 unavailable, using it will raise a :exc:`NotImplementedError`.
1361
Benjamin Petersonf650e462010-05-06 23:03:05 +00001362 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001363
1364 .. note::
1365
Georg Brandl502d9a52009-07-26 15:02:41 +00001366 Using :func:`access` to check if a user is authorized to e.g. open a file
1367 before actually doing so using :func:`open` creates a security hole,
1368 because the user might exploit the short time interval between checking
Benjamin Peterson249b5082011-05-20 11:41:13 -05001369 and opening the file to manipulate it. It's preferable to use :term:`EAFP`
1370 techniques. For example::
1371
1372 if os.access("myfile", os.R_OK):
1373 with open("myfile") as fp:
1374 return fp.read()
1375 return "some default data"
1376
1377 is better written as::
1378
1379 try:
1380 fp = open("myfile")
Antoine Pitrou62ab10a02011-10-12 20:10:51 +02001381 except PermissionError:
1382 return "some default data"
Benjamin Peterson249b5082011-05-20 11:41:13 -05001383 else:
1384 with fp:
1385 return fp.read()
Georg Brandl116aa622007-08-15 14:28:22 +00001386
1387 .. note::
1388
1389 I/O operations may fail even when :func:`access` indicates that they would
1390 succeed, particularly for operations on network filesystems which may have
1391 permissions semantics beyond the usual POSIX permission-bit model.
1392
Larry Hastings9cf065c2012-06-22 16:30:09 -07001393 .. versionchanged:: 3.3
1394 Added the *dir_fd*, *effective_ids*, and *follow_symlinks* parameters.
1395
Georg Brandl116aa622007-08-15 14:28:22 +00001396
1397.. data:: F_OK
Georg Brandl8ccadaa2012-06-24 12:50:06 +02001398 R_OK
1399 W_OK
1400 X_OK
Georg Brandl116aa622007-08-15 14:28:22 +00001401
Georg Brandl8ccadaa2012-06-24 12:50:06 +02001402 Values to pass as the *mode* parameter of :func:`access` to test the
1403 existence, readability, writability and executability of *path*,
1404 respectively.
Georg Brandl116aa622007-08-15 14:28:22 +00001405
1406
1407.. function:: chdir(path)
1408
1409 .. index:: single: directory; changing
1410
Benjamin Petersonf650e462010-05-06 23:03:05 +00001411 Change the current working directory to *path*.
1412
Larry Hastings77892dc2012-06-25 03:27:33 -07001413 This function can support :ref:`specifying a file descriptor <path_fd>`. The
Georg Brandl50c40002012-06-24 11:45:20 +02001414 descriptor must refer to an opened directory, not an open file.
Larry Hastings9cf065c2012-06-22 16:30:09 -07001415
Benjamin Petersonf650e462010-05-06 23:03:05 +00001416 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001417
Larry Hastings9cf065c2012-06-22 16:30:09 -07001418 .. versionadded:: 3.3
1419 Added support for specifying *path* as a file descriptor
Georg Brandl50c40002012-06-24 11:45:20 +02001420 on some platforms.
Larry Hastings9cf065c2012-06-22 16:30:09 -07001421
Georg Brandl116aa622007-08-15 14:28:22 +00001422
Larry Hastings9cf065c2012-06-22 16:30:09 -07001423.. function:: chflags(path, flags, *, follow_symlinks=True)
Georg Brandl116aa622007-08-15 14:28:22 +00001424
1425 Set the flags of *path* to the numeric *flags*. *flags* may take a combination
1426 (bitwise OR) of the following values (as defined in the :mod:`stat` module):
1427
R David Murray30178062011-03-10 17:18:33 -05001428 * :data:`stat.UF_NODUMP`
1429 * :data:`stat.UF_IMMUTABLE`
1430 * :data:`stat.UF_APPEND`
1431 * :data:`stat.UF_OPAQUE`
1432 * :data:`stat.UF_NOUNLINK`
Ned Deily3eb67d52011-06-28 00:00:28 -07001433 * :data:`stat.UF_COMPRESSED`
1434 * :data:`stat.UF_HIDDEN`
R David Murray30178062011-03-10 17:18:33 -05001435 * :data:`stat.SF_ARCHIVED`
1436 * :data:`stat.SF_IMMUTABLE`
1437 * :data:`stat.SF_APPEND`
1438 * :data:`stat.SF_NOUNLINK`
1439 * :data:`stat.SF_SNAPSHOT`
Georg Brandl116aa622007-08-15 14:28:22 +00001440
Georg Brandl50c40002012-06-24 11:45:20 +02001441 This function can support :ref:`not following symlinks <follow_symlinks>`.
Larry Hastings9cf065c2012-06-22 16:30:09 -07001442
Georg Brandlc575c902008-09-13 17:46:05 +00001443 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001444
Larry Hastings9cf065c2012-06-22 16:30:09 -07001445 .. versionadded:: 3.3
1446 The *follow_symlinks* argument.
1447
Georg Brandl116aa622007-08-15 14:28:22 +00001448
Larry Hastings9cf065c2012-06-22 16:30:09 -07001449.. function:: chmod(path, mode, *, dir_fd=None, follow_symlinks=True)
Georg Brandl116aa622007-08-15 14:28:22 +00001450
1451 Change the mode of *path* to the numeric *mode*. *mode* may take one of the
Christian Heimesfaf2f632008-01-06 16:59:19 +00001452 following values (as defined in the :mod:`stat` module) or bitwise ORed
Georg Brandl116aa622007-08-15 14:28:22 +00001453 combinations of them:
1454
Alexandre Vassalottic22c6f22009-07-21 00:51:58 +00001455 * :data:`stat.S_ISUID`
1456 * :data:`stat.S_ISGID`
1457 * :data:`stat.S_ENFMT`
1458 * :data:`stat.S_ISVTX`
1459 * :data:`stat.S_IREAD`
1460 * :data:`stat.S_IWRITE`
1461 * :data:`stat.S_IEXEC`
1462 * :data:`stat.S_IRWXU`
1463 * :data:`stat.S_IRUSR`
1464 * :data:`stat.S_IWUSR`
1465 * :data:`stat.S_IXUSR`
1466 * :data:`stat.S_IRWXG`
1467 * :data:`stat.S_IRGRP`
1468 * :data:`stat.S_IWGRP`
1469 * :data:`stat.S_IXGRP`
1470 * :data:`stat.S_IRWXO`
1471 * :data:`stat.S_IROTH`
1472 * :data:`stat.S_IWOTH`
1473 * :data:`stat.S_IXOTH`
Georg Brandl116aa622007-08-15 14:28:22 +00001474
Georg Brandl50c40002012-06-24 11:45:20 +02001475 This function can support :ref:`specifying a file descriptor <path_fd>`,
1476 :ref:`paths relative to directory descriptors <dir_fd>` and :ref:`not
1477 following symlinks <follow_symlinks>`.
Larry Hastings9cf065c2012-06-22 16:30:09 -07001478
Georg Brandlc575c902008-09-13 17:46:05 +00001479 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001480
1481 .. note::
1482
Georg Brandl50c40002012-06-24 11:45:20 +02001483 Although Windows supports :func:`chmod`, you can only set the file's
1484 read-only flag with it (via the ``stat.S_IWRITE`` and ``stat.S_IREAD``
1485 constants or a corresponding integer value). All other bits are ignored.
Georg Brandl116aa622007-08-15 14:28:22 +00001486
Larry Hastings9cf065c2012-06-22 16:30:09 -07001487 .. versionadded:: 3.3
1488 Added support for specifying *path* as an open file descriptor,
1489 and the *dir_fd* and *follow_symlinks* arguments.
Georg Brandl116aa622007-08-15 14:28:22 +00001490
Larry Hastings9cf065c2012-06-22 16:30:09 -07001491
1492.. function:: chown(path, uid, gid, *, dir_fd=None, follow_symlinks=True)
Georg Brandl116aa622007-08-15 14:28:22 +00001493
Georg Brandl50c40002012-06-24 11:45:20 +02001494 Change the owner and group id of *path* to the numeric *uid* and *gid*. To
1495 leave one of the ids unchanged, set it to -1.
Benjamin Petersonf650e462010-05-06 23:03:05 +00001496
Georg Brandl50c40002012-06-24 11:45:20 +02001497 This function can support :ref:`specifying a file descriptor <path_fd>`,
1498 :ref:`paths relative to directory descriptors <dir_fd>` and :ref:`not
1499 following symlinks <follow_symlinks>`.
Larry Hastings9cf065c2012-06-22 16:30:09 -07001500
Sandro Tosid902a142011-08-22 23:28:27 +02001501 See :func:`shutil.chown` for a higher-level function that accepts names in
1502 addition to numeric ids.
1503
Benjamin Petersonf650e462010-05-06 23:03:05 +00001504 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001505
Larry Hastings9cf065c2012-06-22 16:30:09 -07001506 .. versionadded:: 3.3
1507 Added support for specifying an open file descriptor for *path*,
1508 and the *dir_fd* and *follow_symlinks* arguments.
Georg Brandl116aa622007-08-15 14:28:22 +00001509
Benjamin Peterson799bd802011-08-31 22:15:17 -04001510
Georg Brandl8ccadaa2012-06-24 12:50:06 +02001511.. function:: chroot(path)
1512
Georg Brandl8a5555f2012-06-24 13:29:09 +02001513 Change the root directory of the current process to *path*.
1514
1515 Availability: Unix.
Georg Brandl8ccadaa2012-06-24 12:50:06 +02001516
1517
1518.. function:: fchdir(fd)
1519
1520 Change the current working directory to the directory represented by the file
1521 descriptor *fd*. The descriptor must refer to an opened directory, not an
Georg Brandl4d399a42012-06-25 07:40:32 +02001522 open file. As of Python 3.3, this is equivalent to ``os.chdir(fd)``.
Georg Brandl8ccadaa2012-06-24 12:50:06 +02001523
1524 Availability: Unix.
1525
1526
1527.. function:: getcwd()
1528
1529 Return a string representing the current working directory.
1530
1531 Availability: Unix, Windows.
1532
1533
1534.. function:: getcwdb()
1535
1536 Return a bytestring representing the current working directory.
1537
1538 Availability: Unix, Windows.
1539
1540
Georg Brandl116aa622007-08-15 14:28:22 +00001541.. function:: lchflags(path, flags)
1542
Georg Brandl50c40002012-06-24 11:45:20 +02001543 Set the flags of *path* to the numeric *flags*, like :func:`chflags`, but do
Georg Brandl4d399a42012-06-25 07:40:32 +02001544 not follow symbolic links. As of Python 3.3, this is equivalent to
Georg Brandlb9df00c2012-06-24 12:38:14 +02001545 ``os.chflags(path, flags, follow_symlinks=False)``.
Benjamin Petersonf650e462010-05-06 23:03:05 +00001546
1547 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001548
Georg Brandl116aa622007-08-15 14:28:22 +00001549
Christian Heimes93852662007-12-01 12:22:32 +00001550.. function:: lchmod(path, mode)
1551
1552 Change the mode of *path* to the numeric *mode*. If path is a symlink, this
Georg Brandl50c40002012-06-24 11:45:20 +02001553 affects the symlink rather than the target. See the docs for :func:`chmod`
Georg Brandl4d399a42012-06-25 07:40:32 +02001554 for possible values of *mode*. As of Python 3.3, this is equivalent to
Georg Brandlb9df00c2012-06-24 12:38:14 +02001555 ``os.chmod(path, mode, follow_symlinks=False)``.
Benjamin Petersonf650e462010-05-06 23:03:05 +00001556
1557 Availability: Unix.
Christian Heimes93852662007-12-01 12:22:32 +00001558
Christian Heimes93852662007-12-01 12:22:32 +00001559
Georg Brandl116aa622007-08-15 14:28:22 +00001560.. function:: lchown(path, uid, gid)
1561
Georg Brandl50c40002012-06-24 11:45:20 +02001562 Change the owner and group id of *path* to the numeric *uid* and *gid*. This
Georg Brandl4d399a42012-06-25 07:40:32 +02001563 function will not follow symbolic links. As of Python 3.3, this is equivalent
Georg Brandlb9df00c2012-06-24 12:38:14 +02001564 to ``os.chown(path, uid, gid, follow_symlinks=False)``.
Benjamin Petersonf650e462010-05-06 23:03:05 +00001565
1566 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001567
Georg Brandl116aa622007-08-15 14:28:22 +00001568
Larry Hastings9cf065c2012-06-22 16:30:09 -07001569.. function:: link(src, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlinks=True)
Benjamin Peterson799bd802011-08-31 22:15:17 -04001570
Larry Hastings9cf065c2012-06-22 16:30:09 -07001571 Create a hard link pointing to *src* named *dst*.
Benjamin Peterson799bd802011-08-31 22:15:17 -04001572
Georg Brandlaceaf902012-06-25 08:33:56 +02001573 This function can support specifying *src_dir_fd* and/or *dst_dir_fd* to
1574 supply :ref:`paths relative to directory descriptors <dir_fd>`, and :ref:`not
1575 following symlinks <follow_symlinks>`.
Benjamin Petersonf650e462010-05-06 23:03:05 +00001576
Brian Curtin1b9df392010-11-24 20:24:31 +00001577 Availability: Unix, Windows.
1578
1579 .. versionchanged:: 3.2
1580 Added Windows support.
Georg Brandl116aa622007-08-15 14:28:22 +00001581
Larry Hastings9cf065c2012-06-22 16:30:09 -07001582 .. versionadded:: 3.3
1583 Added the *src_dir_fd*, *dst_dir_fd*, and *follow_symlinks* arguments.
1584
Georg Brandl116aa622007-08-15 14:28:22 +00001585
Martin v. Löwis9c71f902010-07-24 10:09:11 +00001586.. function:: listdir(path='.')
Georg Brandl116aa622007-08-15 14:28:22 +00001587
Benjamin Peterson4469d0c2008-11-30 22:46:23 +00001588 Return a list containing the names of the entries in the directory given by
Larry Hastingsfdaea062012-06-25 04:42:23 -07001589 *path*. The list is in arbitrary order, and does not include the special
1590 entries ``'.'`` and ``'..'`` even if they are present in the directory.
Georg Brandl116aa622007-08-15 14:28:22 +00001591
Larry Hastingsfdaea062012-06-25 04:42:23 -07001592 *path* may be either of type ``str`` or of type ``bytes``. If *path*
1593 is of type ``bytes``, the filenames returned will also be of type ``bytes``;
1594 in all other circumstances, they will be of type ``str``.
Georg Brandl116aa622007-08-15 14:28:22 +00001595
Larry Hastings77892dc2012-06-25 03:27:33 -07001596 This function can also support :ref:`specifying a file descriptor
1597 <path_fd>`; the file descriptor must refer to a directory.
Larry Hastings9cf065c2012-06-22 16:30:09 -07001598
Larry Hastingsfdaea062012-06-25 04:42:23 -07001599 .. note::
1600 To encode ``str`` filenames to ``bytes``, use :func:`~os.fsencode`.
1601
Benjamin Petersonf650e462010-05-06 23:03:05 +00001602 Availability: Unix, Windows.
1603
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00001604 .. versionchanged:: 3.2
1605 The *path* parameter became optional.
Georg Brandl116aa622007-08-15 14:28:22 +00001606
Larry Hastings9cf065c2012-06-22 16:30:09 -07001607 .. versionadded:: 3.3
1608 Added support for specifying an open file descriptor for *path*.
Benjamin Peterson799bd802011-08-31 22:15:17 -04001609
Georg Brandl50c40002012-06-24 11:45:20 +02001610
Victor Stinner6d4f4fe2014-07-24 12:42:16 +02001611.. function:: lstat(path, \*, dir_fd=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001612
R. David Murray7b1aae92011-01-24 19:34:58 +00001613 Perform the equivalent of an :c:func:`lstat` system call on the given path.
Victor Stinner6d4f4fe2014-07-24 12:42:16 +02001614 Similar to :func:`~os.stat`, but does not follow symbolic links. Return a
1615 :class:`stat_result` object.
1616
1617 On platforms that do not support symbolic links, this is an alias for
1618 :func:`~os.stat`.
1619
1620 As of Python 3.3, this is equivalent to ``os.stat(path, dir_fd=dir_fd,
1621 follow_symlinks=False)``.
Larry Hastings9cf065c2012-06-22 16:30:09 -07001622
Georg Brandl50c40002012-06-24 11:45:20 +02001623 This function can also support :ref:`paths relative to directory descriptors
1624 <dir_fd>`.
Brian Curtinc7395692010-07-09 15:15:09 +00001625
Victor Stinner6d4f4fe2014-07-24 12:42:16 +02001626 .. seealso::
1627
1628 The :func:`stat` function.
1629
Georg Brandlb3823372010-07-10 08:58:37 +00001630 .. versionchanged:: 3.2
1631 Added support for Windows 6.0 (Vista) symbolic links.
Georg Brandl116aa622007-08-15 14:28:22 +00001632
Larry Hastings9cf065c2012-06-22 16:30:09 -07001633 .. versionchanged:: 3.3
1634 Added the *dir_fd* parameter.
Ross Lagerwall7807c352011-03-17 20:20:30 +02001635
1636
Georg Brandl8ccadaa2012-06-24 12:50:06 +02001637.. function:: mkdir(path, mode=0o777, *, dir_fd=None)
1638
1639 Create a directory named *path* with numeric mode *mode*.
1640
1641 On some systems, *mode* is ignored. Where it is used, the current umask
1642 value is first masked out. If the directory already exists, :exc:`OSError`
1643 is raised.
1644
1645 This function can also support :ref:`paths relative to directory descriptors
1646 <dir_fd>`.
1647
1648 It is also possible to create temporary directories; see the
1649 :mod:`tempfile` module's :func:`tempfile.mkdtemp` function.
1650
1651 Availability: Unix, Windows.
1652
1653 .. versionadded:: 3.3
1654 The *dir_fd* argument.
1655
1656
Zachary Warea22ae212014-03-20 09:42:01 -05001657.. function:: makedirs(name, mode=0o777, exist_ok=False)
Georg Brandl8ccadaa2012-06-24 12:50:06 +02001658
1659 .. index::
1660 single: directory; creating
1661 single: UNC paths; and os.makedirs()
1662
1663 Recursive directory creation function. Like :func:`mkdir`, but makes all
Hynek Schlawack0230b6a2012-10-07 18:04:38 +02001664 intermediate-level directories needed to contain the leaf directory.
1665
1666 The default *mode* is ``0o777`` (octal). On some systems, *mode* is
1667 ignored. Where it is used, the current umask value is first masked out.
1668
Benjamin Petersonee5f1c12014-04-01 19:13:18 -04001669 If *exist_ok* is ``False`` (the default), an :exc:`OSError` is raised if the
1670 target directory already exists.
Georg Brandl8ccadaa2012-06-24 12:50:06 +02001671
1672 .. note::
1673
1674 :func:`makedirs` will become confused if the path elements to create
Hynek Schlawack0230b6a2012-10-07 18:04:38 +02001675 include :data:`pardir` (eg. ".." on UNIX systems).
Georg Brandl8ccadaa2012-06-24 12:50:06 +02001676
1677 This function handles UNC paths correctly.
1678
1679 .. versionadded:: 3.2
1680 The *exist_ok* parameter.
1681
Benjamin Peterson1acc69c2014-04-01 19:22:06 -04001682 .. versionchanged:: 3.4.1
Benjamin Petersonee5f1c12014-04-01 19:13:18 -04001683
Benjamin Peterson1acc69c2014-04-01 19:22:06 -04001684 Before Python 3.4.1, if *exist_ok* was ``True`` and the directory existed,
Benjamin Petersonee5f1c12014-04-01 19:13:18 -04001685 :func:`makedirs` would still raise an error if *mode* did not match the
1686 mode of the existing directory. Since this behavior was impossible to
Benjamin Peterson1acc69c2014-04-01 19:22:06 -04001687 implement safely, it was removed in Python 3.4.1. See :issue:`21082`.
Benjamin Peterson4717e212014-04-01 19:17:57 -04001688
Georg Brandl8ccadaa2012-06-24 12:50:06 +02001689
Larry Hastings9cf065c2012-06-22 16:30:09 -07001690.. function:: mkfifo(path, mode=0o666, *, dir_fd=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001691
Larry Hastings9cf065c2012-06-22 16:30:09 -07001692 Create a FIFO (a named pipe) named *path* with numeric mode *mode*.
1693 The current umask value is first masked out from the mode.
1694
Georg Brandl50c40002012-06-24 11:45:20 +02001695 This function can also support :ref:`paths relative to directory descriptors
1696 <dir_fd>`.
Georg Brandl116aa622007-08-15 14:28:22 +00001697
1698 FIFOs are pipes that can be accessed like regular files. FIFOs exist until they
1699 are deleted (for example with :func:`os.unlink`). Generally, FIFOs are used as
1700 rendezvous between "client" and "server" type processes: the server opens the
1701 FIFO for reading, and the client opens it for writing. Note that :func:`mkfifo`
1702 doesn't open the FIFO --- it just creates the rendezvous point.
1703
Benjamin Petersonf650e462010-05-06 23:03:05 +00001704 Availability: Unix.
1705
Larry Hastings9cf065c2012-06-22 16:30:09 -07001706 .. versionadded:: 3.3
1707 The *dir_fd* argument.
Georg Brandl116aa622007-08-15 14:28:22 +00001708
Larry Hastings9cf065c2012-06-22 16:30:09 -07001709
1710.. function:: mknod(filename, mode=0o600, device=0, *, dir_fd=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001711
1712 Create a filesystem node (file, device special file or named pipe) named
Georg Brandl18244152009-09-02 20:34:52 +00001713 *filename*. *mode* specifies both the permissions to use and the type of node
1714 to be created, being combined (bitwise OR) with one of ``stat.S_IFREG``,
1715 ``stat.S_IFCHR``, ``stat.S_IFBLK``, and ``stat.S_IFIFO`` (those constants are
1716 available in :mod:`stat`). For ``stat.S_IFCHR`` and ``stat.S_IFBLK``,
1717 *device* defines the newly created device special file (probably using
Georg Brandl116aa622007-08-15 14:28:22 +00001718 :func:`os.makedev`), otherwise it is ignored.
1719
Georg Brandl50c40002012-06-24 11:45:20 +02001720 This function can also support :ref:`paths relative to directory descriptors
1721 <dir_fd>`.
Larry Hastings9cf065c2012-06-22 16:30:09 -07001722
1723 .. versionadded:: 3.3
1724 The *dir_fd* argument.
1725
Georg Brandl116aa622007-08-15 14:28:22 +00001726
1727.. function:: major(device)
1728
Christian Heimesfaf2f632008-01-06 16:59:19 +00001729 Extract the device major number from a raw device number (usually the
Georg Brandl60203b42010-10-06 10:11:56 +00001730 :attr:`st_dev` or :attr:`st_rdev` field from :c:type:`stat`).
Georg Brandl116aa622007-08-15 14:28:22 +00001731
Georg Brandl116aa622007-08-15 14:28:22 +00001732
1733.. function:: minor(device)
1734
Christian Heimesfaf2f632008-01-06 16:59:19 +00001735 Extract the device minor number from a raw device number (usually the
Georg Brandl60203b42010-10-06 10:11:56 +00001736 :attr:`st_dev` or :attr:`st_rdev` field from :c:type:`stat`).
Georg Brandl116aa622007-08-15 14:28:22 +00001737
Georg Brandl116aa622007-08-15 14:28:22 +00001738
1739.. function:: makedev(major, minor)
1740
Christian Heimesfaf2f632008-01-06 16:59:19 +00001741 Compose a raw device number from the major and minor device numbers.
Georg Brandl116aa622007-08-15 14:28:22 +00001742
Georg Brandl116aa622007-08-15 14:28:22 +00001743
Georg Brandl116aa622007-08-15 14:28:22 +00001744.. function:: pathconf(path, name)
1745
1746 Return system configuration information relevant to a named file. *name*
1747 specifies the configuration value to retrieve; it may be a string which is the
1748 name of a defined system value; these names are specified in a number of
1749 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define
1750 additional names as well. The names known to the host operating system are
1751 given in the ``pathconf_names`` dictionary. For configuration variables not
1752 included in that mapping, passing an integer for *name* is also accepted.
Georg Brandl116aa622007-08-15 14:28:22 +00001753
1754 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
1755 specific value for *name* is not supported by the host system, even if it is
1756 included in ``pathconf_names``, an :exc:`OSError` is raised with
1757 :const:`errno.EINVAL` for the error number.
1758
Larry Hastings77892dc2012-06-25 03:27:33 -07001759 This function can support :ref:`specifying a file descriptor
Georg Brandl306336b2012-06-24 12:55:33 +02001760 <path_fd>`.
1761
Benjamin Petersonf650e462010-05-06 23:03:05 +00001762 Availability: Unix.
1763
Georg Brandl116aa622007-08-15 14:28:22 +00001764
1765.. data:: pathconf_names
1766
1767 Dictionary mapping names accepted by :func:`pathconf` and :func:`fpathconf` to
1768 the integer values defined for those names by the host operating system. This
Georg Brandl8a5555f2012-06-24 13:29:09 +02001769 can be used to determine the set of names known to the system.
1770
1771 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00001772
1773
Larry Hastings9cf065c2012-06-22 16:30:09 -07001774.. function:: readlink(path, *, dir_fd=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001775
1776 Return a string representing the path to which the symbolic link points. The
Georg Brandl50c40002012-06-24 11:45:20 +02001777 result may be either an absolute or relative pathname; if it is relative, it
1778 may be converted to an absolute pathname using
1779 ``os.path.join(os.path.dirname(path), result)``.
Georg Brandl116aa622007-08-15 14:28:22 +00001780
Georg Brandl76e55382008-10-08 16:34:57 +00001781 If the *path* is a string object, the result will also be a string object,
1782 and the call may raise an UnicodeDecodeError. If the *path* is a bytes
1783 object, the result will be a bytes object.
Georg Brandl116aa622007-08-15 14:28:22 +00001784
Georg Brandl50c40002012-06-24 11:45:20 +02001785 This function can also support :ref:`paths relative to directory descriptors
1786 <dir_fd>`.
Larry Hastings9cf065c2012-06-22 16:30:09 -07001787
Brian Curtinc7395692010-07-09 15:15:09 +00001788 Availability: Unix, Windows
1789
Georg Brandlb3823372010-07-10 08:58:37 +00001790 .. versionchanged:: 3.2
1791 Added support for Windows 6.0 (Vista) symbolic links.
Georg Brandl116aa622007-08-15 14:28:22 +00001792
Larry Hastings9cf065c2012-06-22 16:30:09 -07001793 .. versionadded:: 3.3
1794 The *dir_fd* argument.
Georg Brandl116aa622007-08-15 14:28:22 +00001795
Georg Brandl116aa622007-08-15 14:28:22 +00001796
Larry Hastingsb698d8e2012-06-23 16:55:07 -07001797.. function:: remove(path, *, dir_fd=None)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001798
Georg Brandl50c40002012-06-24 11:45:20 +02001799 Remove (delete) the file *path*. If *path* is a directory, :exc:`OSError` is
1800 raised. Use :func:`rmdir` to remove directories.
Larry Hastings9cf065c2012-06-22 16:30:09 -07001801
Georg Brandl50c40002012-06-24 11:45:20 +02001802 This function can support :ref:`paths relative to directory descriptors
1803 <dir_fd>`.
Larry Hastings9cf065c2012-06-22 16:30:09 -07001804
1805 On Windows, attempting to remove a file that is in use causes an exception to
1806 be raised; on Unix, the directory entry is removed but the storage allocated
1807 to the file is not made available until the original file is no longer in use.
Benjamin Petersonf650e462010-05-06 23:03:05 +00001808
Larry Hastingsb698d8e2012-06-23 16:55:07 -07001809 This function is identical to :func:`unlink`.
1810
Benjamin Petersonf650e462010-05-06 23:03:05 +00001811 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001812
Larry Hastings9cf065c2012-06-22 16:30:09 -07001813 .. versionadded:: 3.3
Larry Hastingsb698d8e2012-06-23 16:55:07 -07001814 The *dir_fd* argument.
Larry Hastings9cf065c2012-06-22 16:30:09 -07001815
Georg Brandl116aa622007-08-15 14:28:22 +00001816
Zachary Warea22ae212014-03-20 09:42:01 -05001817.. function:: removedirs(name)
Georg Brandl116aa622007-08-15 14:28:22 +00001818
1819 .. index:: single: directory; deleting
1820
Christian Heimesfaf2f632008-01-06 16:59:19 +00001821 Remove directories recursively. Works like :func:`rmdir` except that, if the
Georg Brandl116aa622007-08-15 14:28:22 +00001822 leaf directory is successfully removed, :func:`removedirs` tries to
1823 successively remove every parent directory mentioned in *path* until an error
1824 is raised (which is ignored, because it generally means that a parent directory
1825 is not empty). For example, ``os.removedirs('foo/bar/baz')`` will first remove
1826 the directory ``'foo/bar/baz'``, and then remove ``'foo/bar'`` and ``'foo'`` if
1827 they are empty. Raises :exc:`OSError` if the leaf directory could not be
1828 successfully removed.
1829
Georg Brandl116aa622007-08-15 14:28:22 +00001830
Larry Hastings9cf065c2012-06-22 16:30:09 -07001831.. function:: rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001832
1833 Rename the file or directory *src* to *dst*. If *dst* is a directory,
1834 :exc:`OSError` will be raised. On Unix, if *dst* exists and is a file, it will
Christian Heimesfaf2f632008-01-06 16:59:19 +00001835 be replaced silently if the user has permission. The operation may fail on some
Georg Brandl116aa622007-08-15 14:28:22 +00001836 Unix flavors if *src* and *dst* are on different filesystems. If successful,
1837 the renaming will be an atomic operation (this is a POSIX requirement). On
1838 Windows, if *dst* already exists, :exc:`OSError` will be raised even if it is a
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01001839 file.
1840
Georg Brandlaceaf902012-06-25 08:33:56 +02001841 This function can support specifying *src_dir_fd* and/or *dst_dir_fd* to
1842 supply :ref:`paths relative to directory descriptors <dir_fd>`.
Larry Hastings9cf065c2012-06-22 16:30:09 -07001843
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01001844 If you want cross-platform overwriting of the destination, use :func:`replace`.
Benjamin Petersonf650e462010-05-06 23:03:05 +00001845
1846 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001847
Larry Hastings9cf065c2012-06-22 16:30:09 -07001848 .. versionadded:: 3.3
1849 The *src_dir_fd* and *dst_dir_fd* arguments.
1850
Georg Brandl116aa622007-08-15 14:28:22 +00001851
1852.. function:: renames(old, new)
1853
1854 Recursive directory or file renaming function. Works like :func:`rename`, except
1855 creation of any intermediate directories needed to make the new pathname good is
1856 attempted first. After the rename, directories corresponding to rightmost path
1857 segments of the old name will be pruned away using :func:`removedirs`.
1858
Georg Brandl116aa622007-08-15 14:28:22 +00001859 .. note::
1860
1861 This function can fail with the new directory structure made if you lack
1862 permissions needed to remove the leaf directory or file.
1863
1864
Larry Hastings9cf065c2012-06-22 16:30:09 -07001865.. function:: replace(src, dst, *, src_dir_fd=None, dst_dir_fd=None)
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01001866
1867 Rename the file or directory *src* to *dst*. If *dst* is a directory,
1868 :exc:`OSError` will be raised. If *dst* exists and is a file, it will
1869 be replaced silently if the user has permission. The operation may fail
1870 if *src* and *dst* are on different filesystems. If successful,
1871 the renaming will be an atomic operation (this is a POSIX requirement).
1872
Georg Brandlaceaf902012-06-25 08:33:56 +02001873 This function can support specifying *src_dir_fd* and/or *dst_dir_fd* to
1874 supply :ref:`paths relative to directory descriptors <dir_fd>`.
Larry Hastings9cf065c2012-06-22 16:30:09 -07001875
Georg Brandl8a5555f2012-06-24 13:29:09 +02001876 Availability: Unix, Windows.
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01001877
1878 .. versionadded:: 3.3
1879
1880
Larry Hastingsb698d8e2012-06-23 16:55:07 -07001881.. function:: rmdir(path, *, dir_fd=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001882
Georg Brandla6053b42009-09-01 08:11:14 +00001883 Remove (delete) the directory *path*. Only works when the directory is
1884 empty, otherwise, :exc:`OSError` is raised. In order to remove whole
Benjamin Petersonf650e462010-05-06 23:03:05 +00001885 directory trees, :func:`shutil.rmtree` can be used.
1886
Georg Brandl50c40002012-06-24 11:45:20 +02001887 This function can support :ref:`paths relative to directory descriptors
1888 <dir_fd>`.
Larry Hastingsb698d8e2012-06-23 16:55:07 -07001889
Benjamin Petersonf650e462010-05-06 23:03:05 +00001890 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001891
Larry Hastingsb698d8e2012-06-23 16:55:07 -07001892 .. versionadded:: 3.3
1893 The *dir_fd* parameter.
1894
Georg Brandl116aa622007-08-15 14:28:22 +00001895
Victor Stinner6d4f4fe2014-07-24 12:42:16 +02001896.. function:: stat(path, \*, dir_fd=None, follow_symlinks=True)
Georg Brandl116aa622007-08-15 14:28:22 +00001897
Victor Stinner6d4f4fe2014-07-24 12:42:16 +02001898 Get the status of a file or a file descriptor. Perform the equivalent of a
1899 :c:func:`stat` system call on the given path. *path* may be specified as
1900 either a string or as an open file descriptor. Return a :class:`stat_result`
1901 object.
Georg Brandl116aa622007-08-15 14:28:22 +00001902
Victor Stinner6d4f4fe2014-07-24 12:42:16 +02001903 This function normally follows symlinks; to stat a symlink add the argument
1904 ``follow_symlinks=False``, or use :func:`lstat`.
R. David Murray7b1aae92011-01-24 19:34:58 +00001905
Victor Stinner6d4f4fe2014-07-24 12:42:16 +02001906 This function can support :ref:`specifying a file descriptor <path_fd>` and
1907 :ref:`not following symlinks <follow_symlinks>`.
Georg Brandl116aa622007-08-15 14:28:22 +00001908
Victor Stinner6d4f4fe2014-07-24 12:42:16 +02001909 .. index:: module: stat
R. David Murray7b1aae92011-01-24 19:34:58 +00001910
Victor Stinner6d4f4fe2014-07-24 12:42:16 +02001911 Example::
Georg Brandl116aa622007-08-15 14:28:22 +00001912
Victor Stinner6d4f4fe2014-07-24 12:42:16 +02001913 >>> import os
1914 >>> statinfo = os.stat('somefile.txt')
1915 >>> statinfo
1916 os.stat_result(st_mode=33188, st_ino=7876932, st_dev=234881026,
1917 st_nlink=1, st_uid=501, st_gid=501, st_size=264, st_atime=1297230295,
1918 st_mtime=1297230027, st_ctime=1297230027)
1919 >>> statinfo.st_size
1920 264
R. David Murray7b1aae92011-01-24 19:34:58 +00001921
Victor Stinner6d4f4fe2014-07-24 12:42:16 +02001922 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00001923
Victor Stinner6d4f4fe2014-07-24 12:42:16 +02001924 .. seealso::
Zachary Ware63f277b2014-06-19 09:46:37 -05001925
Victor Stinner6d4f4fe2014-07-24 12:42:16 +02001926 :func:`fstat` and :func:`lstat` functions.
1927
1928 .. versionadded:: 3.3
1929 Added the *dir_fd* and *follow_symlinks* arguments, specifying a file
1930 descriptor instead of a path.
1931
1932
1933.. class:: stat_result
1934
1935 Object whose attributes correspond roughly to the members of the
1936 :c:type:`stat` structure. It is used for the result of :func:`os.stat`,
1937 :func:`os.fstat` and :func:`os.lstat`.
1938
1939 Attributes:
1940
1941 .. attribute:: st_mode
1942
1943 File mode: file type and file mode bits (permissions).
1944
1945 .. attribute:: st_ino
1946
1947 Inode number.
1948
1949 .. attribute:: st_dev
1950
1951 Identifier of the device on which this file resides.
1952
1953 .. attribute:: st_nlink
1954
1955 Number of hard links.
1956
1957 .. attribute:: st_uid
1958
1959 User identifier of the file owner.
1960
1961 .. attribute:: st_gid
1962
1963 Group identifier of the file owner.
1964
1965 .. attribute:: st_size
1966
1967 Size of the file in bytes, if it is a regular file or a symbolic link.
1968 The size of a symbolic link is the length of the pathname it contains,
1969 without a terminating null byte.
1970
1971 Timestamps:
1972
1973 .. attribute:: st_atime
1974
1975 Time of most recent access expressed in seconds.
1976
1977 .. attribute:: st_mtime
1978
1979 Time of most recent content modification expressed in seconds.
1980
1981 .. attribute:: st_ctime
1982
1983 Platform dependent:
1984
1985 * the time of most recent metadata change on Unix,
1986 * the time of creation on Windows, expressed in seconds.
1987
1988 .. attribute:: st_atime_ns
1989
1990 Time of most recent access expressed in nanoseconds as an integer.
1991
1992 .. attribute:: st_mtime_ns
1993
1994 Time of most recent content modification expressed in nanoseconds as an
1995 integer.
1996
1997 .. attribute:: st_ctime_ns
1998
1999 Platform dependent:
2000
2001 * the time of most recent metadata change on Unix,
2002 * the time of creation on Windows, expressed in nanoseconds as an
2003 integer.
2004
2005 See also the :func:`stat_float_times` function.
Zachary Ware63f277b2014-06-19 09:46:37 -05002006
Georg Brandl116aa622007-08-15 14:28:22 +00002007 .. note::
2008
Senthil Kumaran3aac1792011-07-04 11:43:51 -07002009 The exact meaning and resolution of the :attr:`st_atime`,
Senthil Kumarana6bac952011-07-04 11:28:30 -07002010 :attr:`st_mtime`, and :attr:`st_ctime` attributes depend on the operating
2011 system and the file system. For example, on Windows systems using the FAT
2012 or FAT32 file systems, :attr:`st_mtime` has 2-second resolution, and
2013 :attr:`st_atime` has only 1-day resolution. See your operating system
2014 documentation for details.
Victor Stinner6d4f4fe2014-07-24 12:42:16 +02002015
Larry Hastings6fe20b32012-04-19 15:07:49 -07002016 Similarly, although :attr:`st_atime_ns`, :attr:`st_mtime_ns`,
2017 and :attr:`st_ctime_ns` are always expressed in nanoseconds, many
2018 systems do not provide nanosecond precision. On systems that do
2019 provide nanosecond precision, the floating-point object used to
2020 store :attr:`st_atime`, :attr:`st_mtime`, and :attr:`st_ctime`
2021 cannot preserve all of it, and as such will be slightly inexact.
2022 If you need the exact timestamps you should always use
2023 :attr:`st_atime_ns`, :attr:`st_mtime_ns`, and :attr:`st_ctime_ns`.
Georg Brandl116aa622007-08-15 14:28:22 +00002024
Victor Stinner6d4f4fe2014-07-24 12:42:16 +02002025 On some Unix systems (such as Linux), the following attributes may also be
2026 available:
2027
2028 .. attribute:: st_blocks
2029
2030 Number of 512-byte blocks allocated for file.
2031 This may be smaller than :attr:`st_size`/512 when the file has holes.
2032
2033 .. attribute:: st_blksize
2034
2035 "Preferred" blocksize for efficient file system I/O. Writing to a file in
2036 smaller chunks may cause an inefficient read-modify-rewrite.
2037
2038 .. attribute:: st_rdev
2039
2040 Type of device if an inode device.
2041
2042 .. attribute:: st_flags
2043
2044 User defined flags for file.
2045
2046 On other Unix systems (such as FreeBSD), the following attributes may be
2047 available (but may be only filled out if root tries to use them):
2048
2049 .. attribute:: st_gen
2050
2051 File generation number.
2052
2053 .. attribute:: st_birthtime
2054
2055 Time of file creation.
2056
2057 On Mac OS systems, the following attributes may also be available:
2058
2059 .. attribute:: st_rsize
2060
2061 Real size of the file.
2062
2063 .. attribute:: st_creator
2064
2065 Creator of the file.
2066
2067 .. attribute:: st_type
2068
2069 File type.
2070
Victor Stinnere1d24f72014-07-24 12:44:07 +02002071 On Windows systems, the following attribute is also available:
2072
2073 .. attribute:: st_file_attributes
2074
2075 Windows file attributes: ``dwFileAttributes`` member of the
2076 ``BY_HANDLE_FILE_INFORMATION`` structure returned by
2077 :c:func:`GetFileInformationByHandle`. See the ``FILE_ATTRIBUTE_*``
2078 constants in the :mod:`stat` module.
2079
Victor Stinner6d4f4fe2014-07-24 12:42:16 +02002080 The standard module :mod:`stat` defines functions and constants that are
2081 useful for extracting information from a :c:type:`stat` structure. (On
2082 Windows, some items are filled with dummy values.)
2083
2084 For backward compatibility, a :class:`stat_result` instance is also
Georg Brandl50c40002012-06-24 11:45:20 +02002085 accessible as a tuple of at least 10 integers giving the most important (and
2086 portable) members of the :c:type:`stat` structure, in the order
2087 :attr:`st_mode`, :attr:`st_ino`, :attr:`st_dev`, :attr:`st_nlink`,
2088 :attr:`st_uid`, :attr:`st_gid`, :attr:`st_size`, :attr:`st_atime`,
2089 :attr:`st_mtime`, :attr:`st_ctime`. More items may be added at the end by
Victor Stinner6d4f4fe2014-07-24 12:42:16 +02002090 some implementations. For compatibility with older Python versions,
2091 accessing :class:`stat_result` as a tuple always returns integers.
Georg Brandl116aa622007-08-15 14:28:22 +00002092
Larry Hastings6fe20b32012-04-19 15:07:49 -07002093 .. versionadded:: 3.3
Victor Stinner6d4f4fe2014-07-24 12:42:16 +02002094 Added the :attr:`st_atime_ns`, :attr:`st_mtime_ns`, and
2095 :attr:`st_ctime_ns` members.
Larry Hastings6fe20b32012-04-19 15:07:49 -07002096
Zachary Ware63f277b2014-06-19 09:46:37 -05002097 .. versionadded:: 3.5
2098 Added the :attr:`st_file_attributes` member on Windows.
2099
Georg Brandl116aa622007-08-15 14:28:22 +00002100
2101.. function:: stat_float_times([newvalue])
2102
2103 Determine whether :class:`stat_result` represents time stamps as float objects.
R. David Murray7b1aae92011-01-24 19:34:58 +00002104 If *newvalue* is ``True``, future calls to :func:`~os.stat` return floats, if it is
Georg Brandl116aa622007-08-15 14:28:22 +00002105 ``False``, future calls return ints. If *newvalue* is omitted, return the
2106 current setting.
2107
2108 For compatibility with older Python versions, accessing :class:`stat_result` as
2109 a tuple always returns integers.
2110
Georg Brandl55ac8f02007-09-01 13:51:09 +00002111 Python now returns float values by default. Applications which do not work
2112 correctly with floating point time stamps can use this function to restore the
2113 old behaviour.
Georg Brandl116aa622007-08-15 14:28:22 +00002114
2115 The resolution of the timestamps (that is the smallest possible fraction)
2116 depends on the system. Some systems only support second resolution; on these
2117 systems, the fraction will always be zero.
2118
2119 It is recommended that this setting is only changed at program startup time in
2120 the *__main__* module; libraries should never change this setting. If an
2121 application uses a library that works incorrectly if floating point time stamps
2122 are processed, this application should turn the feature off until the library
2123 has been corrected.
2124
Victor Stinner034d0aa2012-06-05 01:22:15 +02002125 .. deprecated:: 3.3
2126
Georg Brandl116aa622007-08-15 14:28:22 +00002127
2128.. function:: statvfs(path)
2129
Georg Brandl60203b42010-10-06 10:11:56 +00002130 Perform a :c:func:`statvfs` system call on the given path. The return value is
Georg Brandl116aa622007-08-15 14:28:22 +00002131 an object whose attributes describe the filesystem on the given path, and
Georg Brandl60203b42010-10-06 10:11:56 +00002132 correspond to the members of the :c:type:`statvfs` structure, namely:
Georg Brandl116aa622007-08-15 14:28:22 +00002133 :attr:`f_bsize`, :attr:`f_frsize`, :attr:`f_blocks`, :attr:`f_bfree`,
2134 :attr:`f_bavail`, :attr:`f_files`, :attr:`f_ffree`, :attr:`f_favail`,
Benjamin Petersonf650e462010-05-06 23:03:05 +00002135 :attr:`f_flag`, :attr:`f_namemax`.
2136
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00002137 Two module-level constants are defined for the :attr:`f_flag` attribute's
2138 bit-flags: if :const:`ST_RDONLY` is set, the filesystem is mounted
2139 read-only, and if :const:`ST_NOSUID` is set, the semantics of
2140 setuid/setgid bits are disabled or not supported.
2141
doko@ubuntu.comca616a22013-12-08 15:23:07 +01002142 Additional module-level constants are defined for GNU/glibc based systems.
2143 These are :const:`ST_NODEV` (disallow access to device special files),
2144 :const:`ST_NOEXEC` (disallow program execution), :const:`ST_SYNCHRONOUS`
2145 (writes are synced at once), :const:`ST_MANDLOCK` (allow mandatory locks on an FS),
2146 :const:`ST_WRITE` (write on file/directory/symlink), :const:`ST_APPEND`
2147 (append-only file), :const:`ST_IMMUTABLE` (immutable file), :const:`ST_NOATIME`
2148 (do not update access times), :const:`ST_NODIRATIME` (do not update directory access
2149 times), :const:`ST_RELATIME` (update atime relative to mtime/ctime).
2150
Georg Brandl50c40002012-06-24 11:45:20 +02002151 This function can support :ref:`specifying a file descriptor <path_fd>`.
Larry Hastings9cf065c2012-06-22 16:30:09 -07002152
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00002153 .. versionchanged:: 3.2
2154 The :const:`ST_RDONLY` and :const:`ST_NOSUID` constants were added.
2155
doko@ubuntu.comca616a22013-12-08 15:23:07 +01002156 .. versionchanged:: 3.4
2157 The :const:`ST_NODEV`, :const:`ST_NOEXEC`, :const:`ST_SYNCHRONOUS`,
2158 :const:`ST_MANDLOCK`, :const:`ST_WRITE`, :const:`ST_APPEND`,
2159 :const:`ST_IMMUTABLE`, :const:`ST_NOATIME`, :const:`ST_NODIRATIME`,
2160 and :const:`ST_RELATIME` constants were added.
2161
Benjamin Petersonf650e462010-05-06 23:03:05 +00002162 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002163
Larry Hastings9cf065c2012-06-22 16:30:09 -07002164 .. versionadded:: 3.3
2165 Added support for specifying an open file descriptor for *path*.
Georg Brandl116aa622007-08-15 14:28:22 +00002166
Larry Hastings9cf065c2012-06-22 16:30:09 -07002167
2168.. data:: supports_dir_fd
2169
Serhiy Storchakadab83542013-10-13 20:12:43 +03002170 A :class:`~collections.abc.Set` object indicating which functions in the
Georg Brandlaceaf902012-06-25 08:33:56 +02002171 :mod:`os` module permit use of their *dir_fd* parameter. Different platforms
Larry Hastings9cf065c2012-06-22 16:30:09 -07002172 provide different functionality, and an option that might work on one might
2173 be unsupported on another. For consistency's sakes, functions that support
Andrew Svetlov5b898402012-12-18 21:26:36 +02002174 *dir_fd* always allow specifying the parameter, but will raise an exception
Larry Hastings9cf065c2012-06-22 16:30:09 -07002175 if the functionality is not actually available.
2176
2177 To check whether a particular function permits use of its *dir_fd*
2178 parameter, use the ``in`` operator on ``supports_dir_fd``. As an example,
2179 this expression determines whether the *dir_fd* parameter of :func:`os.stat`
2180 is locally available::
2181
2182 os.stat in os.supports_dir_fd
2183
Georg Brandlf62445a2012-06-24 13:31:20 +02002184 Currently *dir_fd* parameters only work on Unix platforms; none of them work
2185 on Windows.
Larry Hastings9cf065c2012-06-22 16:30:09 -07002186
2187 .. versionadded:: 3.3
2188
Georg Brandl8ccadaa2012-06-24 12:50:06 +02002189
Larry Hastings9cf065c2012-06-22 16:30:09 -07002190.. data:: supports_effective_ids
2191
Serhiy Storchakadab83542013-10-13 20:12:43 +03002192 A :class:`~collections.abc.Set` object indicating which functions in the
Georg Brandlaceaf902012-06-25 08:33:56 +02002193 :mod:`os` module permit use of the *effective_ids* parameter for
2194 :func:`os.access`. If the local platform supports it, the collection will
2195 contain :func:`os.access`, otherwise it will be empty.
Larry Hastings9cf065c2012-06-22 16:30:09 -07002196
Georg Brandl50c40002012-06-24 11:45:20 +02002197 To check whether you can use the *effective_ids* parameter for
Larry Hastings9cf065c2012-06-22 16:30:09 -07002198 :func:`os.access`, use the ``in`` operator on ``supports_dir_fd``, like so::
2199
2200 os.access in os.supports_effective_ids
2201
Georg Brandl50c40002012-06-24 11:45:20 +02002202 Currently *effective_ids* only works on Unix platforms; it does not work on
2203 Windows.
Larry Hastings9cf065c2012-06-22 16:30:09 -07002204
2205 .. versionadded:: 3.3
2206
Georg Brandl8ccadaa2012-06-24 12:50:06 +02002207
Larry Hastings9cf065c2012-06-22 16:30:09 -07002208.. data:: supports_fd
2209
Serhiy Storchakadab83542013-10-13 20:12:43 +03002210 A :class:`~collections.abc.Set` object indicating which functions in the
Georg Brandlaceaf902012-06-25 08:33:56 +02002211 :mod:`os` module permit specifying their *path* parameter as an open file
Larry Hastings9cf065c2012-06-22 16:30:09 -07002212 descriptor. Different platforms provide different functionality, and an
2213 option that might work on one might be unsupported on another. For
2214 consistency's sakes, functions that support *fd* always allow specifying
Andrew Svetlov5b898402012-12-18 21:26:36 +02002215 the parameter, but will raise an exception if the functionality is not
Larry Hastings9cf065c2012-06-22 16:30:09 -07002216 actually available.
2217
2218 To check whether a particular function permits specifying an open file
2219 descriptor for its *path* parameter, use the ``in`` operator on
2220 ``supports_fd``. As an example, this expression determines whether
2221 :func:`os.chdir` accepts open file descriptors when called on your local
2222 platform::
2223
2224 os.chdir in os.supports_fd
2225
2226 .. versionadded:: 3.3
2227
Georg Brandl8ccadaa2012-06-24 12:50:06 +02002228
Larry Hastings9cf065c2012-06-22 16:30:09 -07002229.. data:: supports_follow_symlinks
2230
Serhiy Storchakadab83542013-10-13 20:12:43 +03002231 A :class:`~collections.abc.Set` object indicating which functions in the
Georg Brandlaceaf902012-06-25 08:33:56 +02002232 :mod:`os` module permit use of their *follow_symlinks* parameter. Different
Larry Hastings9cf065c2012-06-22 16:30:09 -07002233 platforms provide different functionality, and an option that might work on
2234 one might be unsupported on another. For consistency's sakes, functions that
2235 support *follow_symlinks* always allow specifying the parameter, but will
Andrew Svetlov5b898402012-12-18 21:26:36 +02002236 raise an exception if the functionality is not actually available.
Larry Hastings9cf065c2012-06-22 16:30:09 -07002237
2238 To check whether a particular function permits use of its *follow_symlinks*
2239 parameter, use the ``in`` operator on ``supports_follow_symlinks``. As an
2240 example, this expression determines whether the *follow_symlinks* parameter
2241 of :func:`os.stat` is locally available::
2242
2243 os.stat in os.supports_follow_symlinks
2244
2245 .. versionadded:: 3.3
2246
Georg Brandl8ccadaa2012-06-24 12:50:06 +02002247
Larry Hastings9cf065c2012-06-22 16:30:09 -07002248.. function:: symlink(source, link_name, target_is_directory=False, *, dir_fd=None)
Georg Brandl116aa622007-08-15 14:28:22 +00002249
Brian Curtinc7395692010-07-09 15:15:09 +00002250 Create a symbolic link pointing to *source* named *link_name*.
2251
Larry Hastings9cf065c2012-06-22 16:30:09 -07002252 On Windows, a symlink represents either a file or a directory, and does not
Jason R. Coombs3a092862013-05-27 23:21:28 -04002253 morph to the target dynamically. If the target is present, the type of the
2254 symlink will be created to match. Otherwise, the symlink will be created
2255 as a directory if *target_is_directory* is ``True`` or a file symlink (the
2256 default) otherwise. On non-Window platforms, *target_is_directory* is ignored.
Brian Curtind40e6f72010-07-08 21:39:08 +00002257
Georg Brandl64a41ed2010-10-06 08:52:48 +00002258 Symbolic link support was introduced in Windows 6.0 (Vista). :func:`symlink`
2259 will raise a :exc:`NotImplementedError` on Windows versions earlier than 6.0.
Brian Curtin52173d42010-12-02 18:29:18 +00002260
Georg Brandl50c40002012-06-24 11:45:20 +02002261 This function can support :ref:`paths relative to directory descriptors
2262 <dir_fd>`.
Larry Hastings9cf065c2012-06-22 16:30:09 -07002263
Brian Curtin52173d42010-12-02 18:29:18 +00002264 .. note::
2265
Larry Hastings9cf065c2012-06-22 16:30:09 -07002266 On Windows, the *SeCreateSymbolicLinkPrivilege* is required in order to
2267 successfully create symlinks. This privilege is not typically granted to
2268 regular users but is available to accounts which can escalate privileges
2269 to the administrator level. Either obtaining the privilege or running your
Brian Curtin96245592010-12-28 17:08:22 +00002270 application as an administrator are ways to successfully create symlinks.
2271
Jason R. Coombs3a092862013-05-27 23:21:28 -04002272
Brian Curtin96245592010-12-28 17:08:22 +00002273 :exc:`OSError` is raised when the function is called by an unprivileged
2274 user.
Brian Curtind40e6f72010-07-08 21:39:08 +00002275
Georg Brandl64a41ed2010-10-06 08:52:48 +00002276 Availability: Unix, Windows.
Brian Curtinc7395692010-07-09 15:15:09 +00002277
Georg Brandlb3823372010-07-10 08:58:37 +00002278 .. versionchanged:: 3.2
2279 Added support for Windows 6.0 (Vista) symbolic links.
Georg Brandl116aa622007-08-15 14:28:22 +00002280
Larry Hastings9cf065c2012-06-22 16:30:09 -07002281 .. versionadded:: 3.3
2282 Added the *dir_fd* argument, and now allow *target_is_directory*
2283 on non-Windows platforms.
2284
Georg Brandl116aa622007-08-15 14:28:22 +00002285
Ross Lagerwall7807c352011-03-17 20:20:30 +02002286.. function:: sync()
2287
2288 Force write of everything to disk.
2289
2290 Availability: Unix.
2291
2292 .. versionadded:: 3.3
2293
2294
2295.. function:: truncate(path, length)
2296
2297 Truncate the file corresponding to *path*, so that it is at most
2298 *length* bytes in size.
2299
Georg Brandl306336b2012-06-24 12:55:33 +02002300 This function can support :ref:`specifying a file descriptor <path_fd>`.
2301
Ross Lagerwall7807c352011-03-17 20:20:30 +02002302 Availability: Unix.
2303
2304 .. versionadded:: 3.3
2305
2306
Larry Hastingsb698d8e2012-06-23 16:55:07 -07002307.. function:: unlink(path, *, dir_fd=None)
Georg Brandl116aa622007-08-15 14:28:22 +00002308
Larry Hastingsb698d8e2012-06-23 16:55:07 -07002309 Remove (delete) the file *path*. This function is identical to
Georg Brandl50c40002012-06-24 11:45:20 +02002310 :func:`remove`; the ``unlink`` name is its traditional Unix
Larry Hastings9cf065c2012-06-22 16:30:09 -07002311 name. Please see the documentation for :func:`remove` for
2312 further information.
Benjamin Petersonf650e462010-05-06 23:03:05 +00002313
2314 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00002315
Larry Hastings9cf065c2012-06-22 16:30:09 -07002316 .. versionadded:: 3.3
Larry Hastingsb698d8e2012-06-23 16:55:07 -07002317 The *dir_fd* parameter.
Georg Brandl116aa622007-08-15 14:28:22 +00002318
Larry Hastings9cf065c2012-06-22 16:30:09 -07002319
2320.. function:: utime(path, times=None, *, ns=None, dir_fd=None, follow_symlinks=True)
Georg Brandl116aa622007-08-15 14:28:22 +00002321
Larry Hastings76ad59b2012-05-03 00:30:07 -07002322 Set the access and modified times of the file specified by *path*.
2323
2324 :func:`utime` takes two optional parameters, *times* and *ns*.
2325 These specify the times set on *path* and are used as follows:
2326
Larry Hastings9cf065c2012-06-22 16:30:09 -07002327 - If *ns* is not ``None``,
Larry Hastings76ad59b2012-05-03 00:30:07 -07002328 it must be a 2-tuple of the form ``(atime_ns, mtime_ns)``
2329 where each member is an int expressing nanoseconds.
Larry Hastings9cf065c2012-06-22 16:30:09 -07002330 - If *times* is not ``None``,
Larry Hastings76ad59b2012-05-03 00:30:07 -07002331 it must be a 2-tuple of the form ``(atime, mtime)``
2332 where each member is an int or float expressing seconds.
Larry Hastings9cf065c2012-06-22 16:30:09 -07002333 - If *times* and *ns* are both ``None``,
2334 this is equivalent to specifying ``ns=(atime_ns, mtime_ns)``
Larry Hastings76ad59b2012-05-03 00:30:07 -07002335 where both times are the current time.
Larry Hastings76ad59b2012-05-03 00:30:07 -07002336
Larry Hastings9cf065c2012-06-22 16:30:09 -07002337 It is an error to specify tuples for both *times* and *ns*.
Larry Hastings76ad59b2012-05-03 00:30:07 -07002338
2339 Whether a directory can be given for *path*
Brian Curtin52fbea12011-11-06 13:41:17 -06002340 depends on whether the operating system implements directories as files
2341 (for example, Windows does not). Note that the exact times you set here may
2342 not be returned by a subsequent :func:`~os.stat` call, depending on the
2343 resolution with which your operating system records access and modification
Larry Hastings76ad59b2012-05-03 00:30:07 -07002344 times; see :func:`~os.stat`. The best way to preserve exact times is to
2345 use the *st_atime_ns* and *st_mtime_ns* fields from the :func:`os.stat`
2346 result object with the *ns* parameter to `utime`.
Georg Brandl116aa622007-08-15 14:28:22 +00002347
Georg Brandl50c40002012-06-24 11:45:20 +02002348 This function can support :ref:`specifying a file descriptor <path_fd>`,
2349 :ref:`paths relative to directory descriptors <dir_fd>` and :ref:`not
2350 following symlinks <follow_symlinks>`.
Larry Hastings9cf065c2012-06-22 16:30:09 -07002351
Georg Brandlc575c902008-09-13 17:46:05 +00002352 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00002353
Larry Hastings76ad59b2012-05-03 00:30:07 -07002354 .. versionadded:: 3.3
Larry Hastings9cf065c2012-06-22 16:30:09 -07002355 Added support for specifying an open file descriptor for *path*,
2356 and the *dir_fd*, *follow_symlinks*, and *ns* parameters.
Larry Hastings76ad59b2012-05-03 00:30:07 -07002357
Georg Brandl116aa622007-08-15 14:28:22 +00002358
Georg Brandl18244152009-09-02 20:34:52 +00002359.. function:: walk(top, topdown=True, onerror=None, followlinks=False)
Georg Brandl116aa622007-08-15 14:28:22 +00002360
2361 .. index::
2362 single: directory; walking
2363 single: directory; traversal
2364
Christian Heimesfaf2f632008-01-06 16:59:19 +00002365 Generate the file names in a directory tree by walking the tree
2366 either top-down or bottom-up. For each directory in the tree rooted at directory
Georg Brandl116aa622007-08-15 14:28:22 +00002367 *top* (including *top* itself), it yields a 3-tuple ``(dirpath, dirnames,
2368 filenames)``.
2369
2370 *dirpath* is a string, the path to the directory. *dirnames* is a list of the
2371 names of the subdirectories in *dirpath* (excluding ``'.'`` and ``'..'``).
2372 *filenames* is a list of the names of the non-directory files in *dirpath*.
2373 Note that the names in the lists contain no path components. To get a full path
2374 (which begins with *top*) to a file or directory in *dirpath*, do
2375 ``os.path.join(dirpath, name)``.
2376
Christian Heimesfaf2f632008-01-06 16:59:19 +00002377 If optional argument *topdown* is ``True`` or not specified, the triple for a
Georg Brandl116aa622007-08-15 14:28:22 +00002378 directory is generated before the triples for any of its subdirectories
Benjamin Petersone58e0c72014-06-15 20:51:12 -07002379 (directories are generated top-down). If *topdown* is ``False``, the triple
2380 for a directory is generated after the triples for all of its subdirectories
2381 (directories are generated bottom-up). No matter the value of *topdown*, the
2382 list of subdirectories is retrieved before the tuples for the directory and
2383 its subdirectories are generated.
Georg Brandl116aa622007-08-15 14:28:22 +00002384
Christian Heimesfaf2f632008-01-06 16:59:19 +00002385 When *topdown* is ``True``, the caller can modify the *dirnames* list in-place
Georg Brandl116aa622007-08-15 14:28:22 +00002386 (perhaps using :keyword:`del` or slice assignment), and :func:`walk` will only
2387 recurse into the subdirectories whose names remain in *dirnames*; this can be
2388 used to prune the search, impose a specific order of visiting, or even to inform
2389 :func:`walk` about directories the caller creates or renames before it resumes
Christian Heimesfaf2f632008-01-06 16:59:19 +00002390 :func:`walk` again. Modifying *dirnames* when *topdown* is ``False`` is
Georg Brandl116aa622007-08-15 14:28:22 +00002391 ineffective, because in bottom-up mode the directories in *dirnames* are
2392 generated before *dirpath* itself is generated.
2393
Ezio Melotti67494f22011-10-18 12:59:39 +03002394 By default, errors from the :func:`listdir` call are ignored. If optional
Georg Brandl116aa622007-08-15 14:28:22 +00002395 argument *onerror* is specified, it should be a function; it will be called with
2396 one argument, an :exc:`OSError` instance. It can report the error to continue
2397 with the walk, or raise the exception to abort the walk. Note that the filename
2398 is available as the ``filename`` attribute of the exception object.
2399
2400 By default, :func:`walk` will not walk down into symbolic links that resolve to
Christian Heimesfaf2f632008-01-06 16:59:19 +00002401 directories. Set *followlinks* to ``True`` to visit directories pointed to by
Georg Brandl116aa622007-08-15 14:28:22 +00002402 symlinks, on systems that support them.
2403
Georg Brandl116aa622007-08-15 14:28:22 +00002404 .. note::
2405
Georg Brandl50c40002012-06-24 11:45:20 +02002406 Be aware that setting *followlinks* to ``True`` can lead to infinite
2407 recursion if a link points to a parent directory of itself. :func:`walk`
2408 does not keep track of the directories it visited already.
Georg Brandl116aa622007-08-15 14:28:22 +00002409
2410 .. note::
2411
2412 If you pass a relative pathname, don't change the current working directory
2413 between resumptions of :func:`walk`. :func:`walk` never changes the current
2414 directory, and assumes that its caller doesn't either.
2415
2416 This example displays the number of bytes taken by non-directory files in each
2417 directory under the starting directory, except that it doesn't look under any
2418 CVS subdirectory::
2419
2420 import os
2421 from os.path import join, getsize
2422 for root, dirs, files in os.walk('python/Lib/email'):
Georg Brandl6911e3c2007-09-04 07:15:32 +00002423 print(root, "consumes", end=" ")
2424 print(sum(getsize(join(root, name)) for name in files), end=" ")
2425 print("bytes in", len(files), "non-directory files")
Georg Brandl116aa622007-08-15 14:28:22 +00002426 if 'CVS' in dirs:
2427 dirs.remove('CVS') # don't visit CVS directories
2428
Christian Heimesfaf2f632008-01-06 16:59:19 +00002429 In the next example, walking the tree bottom-up is essential: :func:`rmdir`
Georg Brandl116aa622007-08-15 14:28:22 +00002430 doesn't allow deleting a directory before the directory is empty::
2431
Christian Heimesfaf2f632008-01-06 16:59:19 +00002432 # Delete everything reachable from the directory named in "top",
Georg Brandl116aa622007-08-15 14:28:22 +00002433 # assuming there are no symbolic links.
2434 # CAUTION: This is dangerous! For example, if top == '/', it
2435 # could delete all your disk files.
2436 import os
2437 for root, dirs, files in os.walk(top, topdown=False):
2438 for name in files:
2439 os.remove(os.path.join(root, name))
2440 for name in dirs:
2441 os.rmdir(os.path.join(root, name))
2442
Georg Brandl116aa622007-08-15 14:28:22 +00002443
Larry Hastingsb4038062012-07-15 10:57:38 -07002444.. function:: fwalk(top='.', topdown=True, onerror=None, *, follow_symlinks=False, dir_fd=None)
Charles-François Natali7372b062012-02-05 15:15:38 +01002445
2446 .. index::
2447 single: directory; walking
2448 single: directory; traversal
2449
Eli Benderskyd049d5c2012-02-11 09:52:29 +02002450 This behaves exactly like :func:`walk`, except that it yields a 4-tuple
Larry Hastingsc48fe982012-06-25 04:49:05 -07002451 ``(dirpath, dirnames, filenames, dirfd)``, and it supports ``dir_fd``.
Charles-François Natali7372b062012-02-05 15:15:38 +01002452
2453 *dirpath*, *dirnames* and *filenames* are identical to :func:`walk` output,
2454 and *dirfd* is a file descriptor referring to the directory *dirpath*.
2455
Larry Hastingsc48fe982012-06-25 04:49:05 -07002456 This function always supports :ref:`paths relative to directory descriptors
Larry Hastingsb4038062012-07-15 10:57:38 -07002457 <dir_fd>` and :ref:`not following symlinks <follow_symlinks>`. Note however
Larry Hastings950b76a2012-07-15 17:32:36 -07002458 that, unlike other functions, the :func:`fwalk` default value for
Larry Hastingsb4038062012-07-15 10:57:38 -07002459 *follow_symlinks* is ``False``.
Larry Hastingsc48fe982012-06-25 04:49:05 -07002460
Charles-François Natali7372b062012-02-05 15:15:38 +01002461 .. note::
2462
2463 Since :func:`fwalk` yields file descriptors, those are only valid until
2464 the next iteration step, so you should duplicate them (e.g. with
2465 :func:`dup`) if you want to keep them longer.
2466
2467 This example displays the number of bytes taken by non-directory files in each
2468 directory under the starting directory, except that it doesn't look under any
2469 CVS subdirectory::
2470
2471 import os
2472 for root, dirs, files, rootfd in os.fwalk('python/Lib/email'):
2473 print(root, "consumes", end="")
Hynek Schlawack1729b8f2012-06-24 16:11:08 +02002474 print(sum([os.stat(name, dir_fd=rootfd).st_size for name in files]),
Charles-François Natali7372b062012-02-05 15:15:38 +01002475 end="")
2476 print("bytes in", len(files), "non-directory files")
2477 if 'CVS' in dirs:
2478 dirs.remove('CVS') # don't visit CVS directories
2479
2480 In the next example, walking the tree bottom-up is essential:
Victor Stinner69a6ca52012-08-05 15:18:02 +02002481 :func:`rmdir` doesn't allow deleting a directory before the directory is
Charles-François Natali7372b062012-02-05 15:15:38 +01002482 empty::
2483
2484 # Delete everything reachable from the directory named in "top",
2485 # assuming there are no symbolic links.
2486 # CAUTION: This is dangerous! For example, if top == '/', it
2487 # could delete all your disk files.
2488 import os
2489 for root, dirs, files, rootfd in os.fwalk(top, topdown=False):
2490 for name in files:
Victor Stinner69a6ca52012-08-05 15:18:02 +02002491 os.unlink(name, dir_fd=rootfd)
Charles-François Natali7372b062012-02-05 15:15:38 +01002492 for name in dirs:
Victor Stinner69a6ca52012-08-05 15:18:02 +02002493 os.rmdir(name, dir_fd=rootfd)
Charles-François Natali7372b062012-02-05 15:15:38 +01002494
2495 Availability: Unix.
2496
2497 .. versionadded:: 3.3
2498
2499
Georg Brandlb9831ab2012-06-24 11:57:07 +02002500Linux extended attributes
2501~~~~~~~~~~~~~~~~~~~~~~~~~
2502
2503.. versionadded:: 3.3
2504
2505These functions are all available on Linux only.
2506
2507.. function:: getxattr(path, attribute, *, follow_symlinks=True)
2508
2509 Return the value of the extended filesystem attribute *attribute* for
2510 *path*. *attribute* can be bytes or str. If it is str, it is encoded
2511 with the filesystem encoding.
2512
2513 This function can support :ref:`specifying a file descriptor <path_fd>` and
2514 :ref:`not following symlinks <follow_symlinks>`.
2515
2516
2517.. function:: listxattr(path=None, *, follow_symlinks=True)
2518
2519 Return a list of the extended filesystem attributes on *path*. The
2520 attributes in the list are represented as strings decoded with the filesystem
2521 encoding. If *path* is ``None``, :func:`listxattr` will examine the current
2522 directory.
2523
2524 This function can support :ref:`specifying a file descriptor <path_fd>` and
2525 :ref:`not following symlinks <follow_symlinks>`.
2526
2527
2528.. function:: removexattr(path, attribute, *, follow_symlinks=True)
2529
2530 Removes the extended filesystem attribute *attribute* from *path*.
2531 *attribute* should be bytes or str. If it is a string, it is encoded
2532 with the filesystem encoding.
2533
2534 This function can support :ref:`specifying a file descriptor <path_fd>` and
2535 :ref:`not following symlinks <follow_symlinks>`.
2536
2537
2538.. function:: setxattr(path, attribute, value, flags=0, *, follow_symlinks=True)
2539
2540 Set the extended filesystem attribute *attribute* on *path* to *value*.
2541 *attribute* must be a bytes or str with no embedded NULs. If it is a str,
2542 it is encoded with the filesystem encoding. *flags* may be
2543 :data:`XATTR_REPLACE` or :data:`XATTR_CREATE`. If :data:`XATTR_REPLACE` is
2544 given and the attribute does not exist, ``EEXISTS`` will be raised.
2545 If :data:`XATTR_CREATE` is given and the attribute already exists, the
2546 attribute will not be created and ``ENODATA`` will be raised.
2547
2548 This function can support :ref:`specifying a file descriptor <path_fd>` and
2549 :ref:`not following symlinks <follow_symlinks>`.
2550
2551 .. note::
2552
2553 A bug in Linux kernel versions less than 2.6.39 caused the flags argument
2554 to be ignored on some filesystems.
2555
2556
2557.. data:: XATTR_SIZE_MAX
2558
2559 The maximum size the value of an extended attribute can be. Currently, this
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002560 is 64 KiB on Linux.
Georg Brandlb9831ab2012-06-24 11:57:07 +02002561
2562
2563.. data:: XATTR_CREATE
2564
2565 This is a possible value for the flags argument in :func:`setxattr`. It
2566 indicates the operation must create an attribute.
2567
2568
2569.. data:: XATTR_REPLACE
2570
2571 This is a possible value for the flags argument in :func:`setxattr`. It
2572 indicates the operation must replace an existing attribute.
2573
2574
Georg Brandl116aa622007-08-15 14:28:22 +00002575.. _os-process:
2576
2577Process Management
2578------------------
2579
2580These functions may be used to create and manage processes.
2581
Serhiy Storchakadab83542013-10-13 20:12:43 +03002582The various :func:`exec\* <execl>` functions take a list of arguments for the new
Georg Brandl116aa622007-08-15 14:28:22 +00002583program loaded into the process. In each case, the first of these arguments is
2584passed to the new program as its own name rather than as an argument a user may
2585have typed on a command line. For the C programmer, this is the ``argv[0]``
Georg Brandl60203b42010-10-06 10:11:56 +00002586passed to a program's :c:func:`main`. For example, ``os.execv('/bin/echo',
Georg Brandl116aa622007-08-15 14:28:22 +00002587['foo', 'bar'])`` will only print ``bar`` on standard output; ``foo`` will seem
2588to be ignored.
2589
2590
2591.. function:: abort()
2592
2593 Generate a :const:`SIGABRT` signal to the current process. On Unix, the default
2594 behavior is to produce a core dump; on Windows, the process immediately returns
Victor Stinner6e2e3b92011-07-08 02:26:39 +02002595 an exit code of ``3``. Be aware that calling this function will not call the
2596 Python signal handler registered for :const:`SIGABRT` with
2597 :func:`signal.signal`.
Benjamin Petersonf650e462010-05-06 23:03:05 +00002598
Georg Brandlc575c902008-09-13 17:46:05 +00002599 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00002600
2601
2602.. function:: execl(path, arg0, arg1, ...)
2603 execle(path, arg0, arg1, ..., env)
2604 execlp(file, arg0, arg1, ...)
2605 execlpe(file, arg0, arg1, ..., env)
2606 execv(path, args)
2607 execve(path, args, env)
2608 execvp(file, args)
2609 execvpe(file, args, env)
2610
2611 These functions all execute a new program, replacing the current process; they
2612 do not return. On Unix, the new executable is loaded into the current process,
Christian Heimesfaf2f632008-01-06 16:59:19 +00002613 and will have the same process id as the caller. Errors will be reported as
Georg Brandl48310cd2009-01-03 21:18:54 +00002614 :exc:`OSError` exceptions.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00002615
2616 The current process is replaced immediately. Open file objects and
2617 descriptors are not flushed, so if there may be data buffered
2618 on these open files, you should flush them using
2619 :func:`sys.stdout.flush` or :func:`os.fsync` before calling an
Serhiy Storchakadab83542013-10-13 20:12:43 +03002620 :func:`exec\* <execl>` function.
Georg Brandl116aa622007-08-15 14:28:22 +00002621
Serhiy Storchakadab83542013-10-13 20:12:43 +03002622 The "l" and "v" variants of the :func:`exec\* <execl>` functions differ in how
Christian Heimesfaf2f632008-01-06 16:59:19 +00002623 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00002624 to work with if the number of parameters is fixed when the code is written; the
2625 individual parameters simply become additional parameters to the :func:`execl\*`
Christian Heimesfaf2f632008-01-06 16:59:19 +00002626 functions. The "v" variants are good when the number of parameters is
Georg Brandl116aa622007-08-15 14:28:22 +00002627 variable, with the arguments being passed in a list or tuple as the *args*
2628 parameter. In either case, the arguments to the child process should start with
2629 the name of the command being run, but this is not enforced.
2630
Christian Heimesfaf2f632008-01-06 16:59:19 +00002631 The variants which include a "p" near the end (:func:`execlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00002632 :func:`execlpe`, :func:`execvp`, and :func:`execvpe`) will use the
2633 :envvar:`PATH` environment variable to locate the program *file*. When the
Serhiy Storchakadab83542013-10-13 20:12:43 +03002634 environment is being replaced (using one of the :func:`exec\*e <execl>` variants,
Georg Brandl116aa622007-08-15 14:28:22 +00002635 discussed in the next paragraph), the new environment is used as the source of
2636 the :envvar:`PATH` variable. The other variants, :func:`execl`, :func:`execle`,
2637 :func:`execv`, and :func:`execve`, will not use the :envvar:`PATH` variable to
2638 locate the executable; *path* must contain an appropriate absolute or relative
2639 path.
2640
2641 For :func:`execle`, :func:`execlpe`, :func:`execve`, and :func:`execvpe` (note
Christian Heimesfaf2f632008-01-06 16:59:19 +00002642 that these all end in "e"), the *env* parameter must be a mapping which is
Christian Heimesa342c012008-04-20 21:01:16 +00002643 used to define the environment variables for the new process (these are used
2644 instead of the current process' environment); the functions :func:`execl`,
Georg Brandl116aa622007-08-15 14:28:22 +00002645 :func:`execlp`, :func:`execv`, and :func:`execvp` all cause the new process to
Georg Brandl48310cd2009-01-03 21:18:54 +00002646 inherit the environment of the current process.
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00002647
Larry Hastings9cf065c2012-06-22 16:30:09 -07002648 For :func:`execve` on some platforms, *path* may also be specified as an open
2649 file descriptor. This functionality may not be supported on your platform;
2650 you can check whether or not it is available using :data:`os.supports_fd`.
2651 If it is unavailable, using it will raise a :exc:`NotImplementedError`.
2652
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00002653 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00002654
Larry Hastings9cf065c2012-06-22 16:30:09 -07002655 .. versionadded:: 3.3
2656 Added support for specifying an open file descriptor for *path*
2657 for :func:`execve`.
Georg Brandl116aa622007-08-15 14:28:22 +00002658
2659.. function:: _exit(n)
2660
Georg Brandl6f4e68d2010-10-17 10:51:45 +00002661 Exit the process with status *n*, without calling cleanup handlers, flushing
Benjamin Petersonf650e462010-05-06 23:03:05 +00002662 stdio buffers, etc.
2663
2664 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00002665
2666 .. note::
2667
Georg Brandl6f4e68d2010-10-17 10:51:45 +00002668 The standard way to exit is ``sys.exit(n)``. :func:`_exit` should
2669 normally only be used in the child process after a :func:`fork`.
Georg Brandl116aa622007-08-15 14:28:22 +00002670
Christian Heimesfaf2f632008-01-06 16:59:19 +00002671The following exit codes are defined and can be used with :func:`_exit`,
Georg Brandl116aa622007-08-15 14:28:22 +00002672although they are not required. These are typically used for system programs
2673written in Python, such as a mail server's external command delivery program.
2674
2675.. note::
2676
2677 Some of these may not be available on all Unix platforms, since there is some
2678 variation. These constants are defined where they are defined by the underlying
2679 platform.
2680
2681
2682.. data:: EX_OK
2683
Benjamin Petersonf650e462010-05-06 23:03:05 +00002684 Exit code that means no error occurred.
2685
2686 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002687
Georg Brandl116aa622007-08-15 14:28:22 +00002688
2689.. data:: EX_USAGE
2690
2691 Exit code that means the command was used incorrectly, such as when the wrong
Benjamin Petersonf650e462010-05-06 23:03:05 +00002692 number of arguments are given.
2693
2694 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002695
Georg Brandl116aa622007-08-15 14:28:22 +00002696
2697.. data:: EX_DATAERR
2698
Benjamin Petersonf650e462010-05-06 23:03:05 +00002699 Exit code that means the input data was incorrect.
2700
2701 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002702
Georg Brandl116aa622007-08-15 14:28:22 +00002703
2704.. data:: EX_NOINPUT
2705
2706 Exit code that means an input file did not exist or was not readable.
Benjamin Petersonf650e462010-05-06 23:03:05 +00002707
Georg Brandlc575c902008-09-13 17:46:05 +00002708 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002709
Georg Brandl116aa622007-08-15 14:28:22 +00002710
2711.. data:: EX_NOUSER
2712
Benjamin Petersonf650e462010-05-06 23:03:05 +00002713 Exit code that means a specified user did not exist.
2714
2715 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002716
Georg Brandl116aa622007-08-15 14:28:22 +00002717
2718.. data:: EX_NOHOST
2719
Benjamin Petersonf650e462010-05-06 23:03:05 +00002720 Exit code that means a specified host did not exist.
2721
2722 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002723
Georg Brandl116aa622007-08-15 14:28:22 +00002724
2725.. data:: EX_UNAVAILABLE
2726
Benjamin Petersonf650e462010-05-06 23:03:05 +00002727 Exit code that means that a required service is unavailable.
2728
2729 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002730
Georg Brandl116aa622007-08-15 14:28:22 +00002731
2732.. data:: EX_SOFTWARE
2733
Benjamin Petersonf650e462010-05-06 23:03:05 +00002734 Exit code that means an internal software error was detected.
2735
2736 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002737
Georg Brandl116aa622007-08-15 14:28:22 +00002738
2739.. data:: EX_OSERR
2740
2741 Exit code that means an operating system error was detected, such as the
Benjamin Petersonf650e462010-05-06 23:03:05 +00002742 inability to fork or create a pipe.
2743
2744 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002745
Georg Brandl116aa622007-08-15 14:28:22 +00002746
2747.. data:: EX_OSFILE
2748
2749 Exit code that means some system file did not exist, could not be opened, or had
Benjamin Petersonf650e462010-05-06 23:03:05 +00002750 some other kind of error.
2751
2752 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002753
Georg Brandl116aa622007-08-15 14:28:22 +00002754
2755.. data:: EX_CANTCREAT
2756
2757 Exit code that means a user specified output file could not be created.
Benjamin Petersonf650e462010-05-06 23:03:05 +00002758
Georg Brandlc575c902008-09-13 17:46:05 +00002759 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002760
Georg Brandl116aa622007-08-15 14:28:22 +00002761
2762.. data:: EX_IOERR
2763
2764 Exit code that means that an error occurred while doing I/O on some file.
Benjamin Petersonf650e462010-05-06 23:03:05 +00002765
Georg Brandlc575c902008-09-13 17:46:05 +00002766 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002767
Georg Brandl116aa622007-08-15 14:28:22 +00002768
2769.. data:: EX_TEMPFAIL
2770
2771 Exit code that means a temporary failure occurred. This indicates something
2772 that may not really be an error, such as a network connection that couldn't be
Benjamin Petersonf650e462010-05-06 23:03:05 +00002773 made during a retryable operation.
2774
2775 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002776
Georg Brandl116aa622007-08-15 14:28:22 +00002777
2778.. data:: EX_PROTOCOL
2779
2780 Exit code that means that a protocol exchange was illegal, invalid, or not
Benjamin Petersonf650e462010-05-06 23:03:05 +00002781 understood.
2782
2783 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002784
Georg Brandl116aa622007-08-15 14:28:22 +00002785
2786.. data:: EX_NOPERM
2787
2788 Exit code that means that there were insufficient permissions to perform the
Benjamin Petersonf650e462010-05-06 23:03:05 +00002789 operation (but not intended for file system problems).
2790
2791 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002792
Georg Brandl116aa622007-08-15 14:28:22 +00002793
2794.. data:: EX_CONFIG
2795
2796 Exit code that means that some kind of configuration error occurred.
Benjamin Petersonf650e462010-05-06 23:03:05 +00002797
Georg Brandlc575c902008-09-13 17:46:05 +00002798 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002799
Georg Brandl116aa622007-08-15 14:28:22 +00002800
2801.. data:: EX_NOTFOUND
2802
Benjamin Petersonf650e462010-05-06 23:03:05 +00002803 Exit code that means something like "an entry was not found".
2804
2805 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002806
Georg Brandl116aa622007-08-15 14:28:22 +00002807
2808.. function:: fork()
2809
Christian Heimesfaf2f632008-01-06 16:59:19 +00002810 Fork a child process. Return ``0`` in the child and the child's process id in the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002811 parent. If an error occurs :exc:`OSError` is raised.
Benjamin Petersonbcd8ac32008-10-10 22:20:52 +00002812
Larry Hastings3732ed22014-03-15 21:13:56 -07002813 Note that some platforms including FreeBSD <= 6.3 and Cygwin have
Benjamin Petersonbcd8ac32008-10-10 22:20:52 +00002814 known issues when using fork() from a thread.
2815
Christian Heimes3046fe42013-10-29 21:08:56 +01002816 .. warning::
2817
2818 See :mod:`ssl` for applications that use the SSL module with fork().
2819
Georg Brandlc575c902008-09-13 17:46:05 +00002820 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002821
2822
2823.. function:: forkpty()
2824
2825 Fork a child process, using a new pseudo-terminal as the child's controlling
2826 terminal. Return a pair of ``(pid, fd)``, where *pid* is ``0`` in the child, the
2827 new child's process id in the parent, and *fd* is the file descriptor of the
2828 master end of the pseudo-terminal. For a more portable approach, use the
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002829 :mod:`pty` module. If an error occurs :exc:`OSError` is raised.
Benjamin Petersonf650e462010-05-06 23:03:05 +00002830
Georg Brandlc575c902008-09-13 17:46:05 +00002831 Availability: some flavors of Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002832
2833
2834.. function:: kill(pid, sig)
2835
2836 .. index::
2837 single: process; killing
2838 single: process; signalling
2839
2840 Send signal *sig* to the process *pid*. Constants for the specific signals
2841 available on the host platform are defined in the :mod:`signal` module.
Brian Curtineb24d742010-04-12 17:16:38 +00002842
2843 Windows: The :data:`signal.CTRL_C_EVENT` and
2844 :data:`signal.CTRL_BREAK_EVENT` signals are special signals which can
2845 only be sent to console processes which share a common console window,
2846 e.g., some subprocesses. Any other value for *sig* will cause the process
2847 to be unconditionally killed by the TerminateProcess API, and the exit code
2848 will be set to *sig*. The Windows version of :func:`kill` additionally takes
2849 process handles to be killed.
Georg Brandl116aa622007-08-15 14:28:22 +00002850
Victor Stinnerb3e72192011-05-08 01:46:11 +02002851 See also :func:`signal.pthread_kill`.
2852
Georg Brandl67b21b72010-08-17 15:07:14 +00002853 .. versionadded:: 3.2
2854 Windows support.
Brian Curtin904bd392010-04-20 15:28:06 +00002855
Georg Brandl116aa622007-08-15 14:28:22 +00002856
2857.. function:: killpg(pgid, sig)
2858
2859 .. index::
2860 single: process; killing
2861 single: process; signalling
2862
Benjamin Petersonf650e462010-05-06 23:03:05 +00002863 Send the signal *sig* to the process group *pgid*.
2864
2865 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002866
Georg Brandl116aa622007-08-15 14:28:22 +00002867
2868.. function:: nice(increment)
2869
2870 Add *increment* to the process's "niceness". Return the new niceness.
Benjamin Petersonf650e462010-05-06 23:03:05 +00002871
Georg Brandlc575c902008-09-13 17:46:05 +00002872 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002873
2874
2875.. function:: plock(op)
2876
2877 Lock program segments into memory. The value of *op* (defined in
Benjamin Petersonf650e462010-05-06 23:03:05 +00002878 ``<sys/lock.h>``) determines which segments are locked.
2879
2880 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00002881
2882
Andrew Kuchlingf5a42922014-04-16 09:10:53 -04002883.. function:: popen(command, mode='r', buffering=-1)
Georg Brandl116aa622007-08-15 14:28:22 +00002884
Andrew Kuchlingf5a42922014-04-16 09:10:53 -04002885 Open a pipe to or from *command*. The return value is an open file object
2886 connected to the pipe, which can be read or written depending on whether *mode*
2887 is ``'r'`` (default) or ``'w'``. The *buffering* argument has the same meaning as
2888 the corresponding argument to the built-in :func:`open` function. The
2889 returned file object reads or writes text strings rather than bytes.
2890
2891 The ``close`` method returns :const:`None` if the subprocess exited
2892 successfully, or the subprocess's return code if there was an
2893 error. On POSIX systems, if the return code is positive it
2894 represents the return value of the process left-shifted by one
2895 byte. If the return code is negative, the process was terminated
2896 by the signal given by the negated value of the return code. (For
2897 example, the return value might be ``- signal.SIGKILL`` if the
2898 subprocess was killed.) On Windows systems, the return value
2899 contains the signed integer return code from the child process.
2900
2901 This is implemented using :class:`subprocess.Popen`; see that class's
2902 documentation for more powerful ways to manage and communicate with
2903 subprocesses.
Georg Brandl116aa622007-08-15 14:28:22 +00002904
2905
2906.. function:: spawnl(mode, path, ...)
2907 spawnle(mode, path, ..., env)
2908 spawnlp(mode, file, ...)
2909 spawnlpe(mode, file, ..., env)
2910 spawnv(mode, path, args)
2911 spawnve(mode, path, args, env)
2912 spawnvp(mode, file, args)
2913 spawnvpe(mode, file, args, env)
2914
2915 Execute the program *path* in a new process.
2916
2917 (Note that the :mod:`subprocess` module provides more powerful facilities for
2918 spawning new processes and retrieving their results; using that module is
Benjamin Peterson87c8d872009-06-11 22:54:11 +00002919 preferable to using these functions. Check especially the
2920 :ref:`subprocess-replacements` section.)
Georg Brandl116aa622007-08-15 14:28:22 +00002921
Christian Heimesfaf2f632008-01-06 16:59:19 +00002922 If *mode* is :const:`P_NOWAIT`, this function returns the process id of the new
Georg Brandl116aa622007-08-15 14:28:22 +00002923 process; if *mode* is :const:`P_WAIT`, returns the process's exit code if it
2924 exits normally, or ``-signal``, where *signal* is the signal that killed the
Christian Heimesfaf2f632008-01-06 16:59:19 +00002925 process. On Windows, the process id will actually be the process handle, so can
Georg Brandl116aa622007-08-15 14:28:22 +00002926 be used with the :func:`waitpid` function.
2927
Serhiy Storchakadab83542013-10-13 20:12:43 +03002928 The "l" and "v" variants of the :func:`spawn\* <spawnl>` functions differ in how
Christian Heimesfaf2f632008-01-06 16:59:19 +00002929 command-line arguments are passed. The "l" variants are perhaps the easiest
Georg Brandl116aa622007-08-15 14:28:22 +00002930 to work with if the number of parameters is fixed when the code is written; the
2931 individual parameters simply become additional parameters to the
Christian Heimesfaf2f632008-01-06 16:59:19 +00002932 :func:`spawnl\*` functions. The "v" variants are good when the number of
Georg Brandl116aa622007-08-15 14:28:22 +00002933 parameters is variable, with the arguments being passed in a list or tuple as
2934 the *args* parameter. In either case, the arguments to the child process must
2935 start with the name of the command being run.
2936
Christian Heimesfaf2f632008-01-06 16:59:19 +00002937 The variants which include a second "p" near the end (:func:`spawnlp`,
Georg Brandl116aa622007-08-15 14:28:22 +00002938 :func:`spawnlpe`, :func:`spawnvp`, and :func:`spawnvpe`) will use the
2939 :envvar:`PATH` environment variable to locate the program *file*. When the
Serhiy Storchakadab83542013-10-13 20:12:43 +03002940 environment is being replaced (using one of the :func:`spawn\*e <spawnl>` variants,
Georg Brandl116aa622007-08-15 14:28:22 +00002941 discussed in the next paragraph), the new environment is used as the source of
2942 the :envvar:`PATH` variable. The other variants, :func:`spawnl`,
2943 :func:`spawnle`, :func:`spawnv`, and :func:`spawnve`, will not use the
2944 :envvar:`PATH` variable to locate the executable; *path* must contain an
2945 appropriate absolute or relative path.
2946
2947 For :func:`spawnle`, :func:`spawnlpe`, :func:`spawnve`, and :func:`spawnvpe`
Christian Heimesfaf2f632008-01-06 16:59:19 +00002948 (note that these all end in "e"), the *env* parameter must be a mapping
Christian Heimesa342c012008-04-20 21:01:16 +00002949 which is used to define the environment variables for the new process (they are
2950 used instead of the current process' environment); the functions
Georg Brandl116aa622007-08-15 14:28:22 +00002951 :func:`spawnl`, :func:`spawnlp`, :func:`spawnv`, and :func:`spawnvp` all cause
Benjamin Petersond23f8222009-04-05 19:13:16 +00002952 the new process to inherit the environment of the current process. Note that
2953 keys and values in the *env* dictionary must be strings; invalid keys or
2954 values will cause the function to fail, with a return value of ``127``.
Georg Brandl116aa622007-08-15 14:28:22 +00002955
2956 As an example, the following calls to :func:`spawnlp` and :func:`spawnvpe` are
2957 equivalent::
2958
2959 import os
2960 os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
2961
2962 L = ['cp', 'index.html', '/dev/null']
2963 os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
2964
2965 Availability: Unix, Windows. :func:`spawnlp`, :func:`spawnlpe`, :func:`spawnvp`
Antoine Pitrou0e752dd2011-07-19 01:26:58 +02002966 and :func:`spawnvpe` are not available on Windows. :func:`spawnle` and
2967 :func:`spawnve` are not thread-safe on Windows; we advise you to use the
2968 :mod:`subprocess` module instead.
Georg Brandl116aa622007-08-15 14:28:22 +00002969
Georg Brandl116aa622007-08-15 14:28:22 +00002970
2971.. data:: P_NOWAIT
2972 P_NOWAITO
2973
Serhiy Storchakadab83542013-10-13 20:12:43 +03002974 Possible values for the *mode* parameter to the :func:`spawn\* <spawnl>` family of
Georg Brandl116aa622007-08-15 14:28:22 +00002975 functions. If either of these values is given, the :func:`spawn\*` functions
Christian Heimesfaf2f632008-01-06 16:59:19 +00002976 will return as soon as the new process has been created, with the process id as
Benjamin Petersonf650e462010-05-06 23:03:05 +00002977 the return value.
2978
2979 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00002980
Georg Brandl116aa622007-08-15 14:28:22 +00002981
2982.. data:: P_WAIT
2983
Serhiy Storchakadab83542013-10-13 20:12:43 +03002984 Possible value for the *mode* parameter to the :func:`spawn\* <spawnl>` family of
Georg Brandl116aa622007-08-15 14:28:22 +00002985 functions. If this is given as *mode*, the :func:`spawn\*` functions will not
2986 return until the new process has run to completion and will return the exit code
2987 of the process the run is successful, or ``-signal`` if a signal kills the
Benjamin Petersonf650e462010-05-06 23:03:05 +00002988 process.
2989
2990 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00002991
Georg Brandl116aa622007-08-15 14:28:22 +00002992
2993.. data:: P_DETACH
2994 P_OVERLAY
2995
Serhiy Storchakadab83542013-10-13 20:12:43 +03002996 Possible values for the *mode* parameter to the :func:`spawn\* <spawnl>` family of
Georg Brandl116aa622007-08-15 14:28:22 +00002997 functions. These are less portable than those listed above. :const:`P_DETACH`
2998 is similar to :const:`P_NOWAIT`, but the new process is detached from the
2999 console of the calling process. If :const:`P_OVERLAY` is used, the current
Serhiy Storchakadab83542013-10-13 20:12:43 +03003000 process will be replaced; the :func:`spawn\* <spawnl>` function will not return.
Benjamin Petersonf650e462010-05-06 23:03:05 +00003001
Georg Brandl116aa622007-08-15 14:28:22 +00003002 Availability: Windows.
3003
Georg Brandl116aa622007-08-15 14:28:22 +00003004
3005.. function:: startfile(path[, operation])
3006
3007 Start a file with its associated application.
3008
3009 When *operation* is not specified or ``'open'``, this acts like double-clicking
3010 the file in Windows Explorer, or giving the file name as an argument to the
3011 :program:`start` command from the interactive command shell: the file is opened
3012 with whatever application (if any) its extension is associated.
3013
3014 When another *operation* is given, it must be a "command verb" that specifies
3015 what should be done with the file. Common verbs documented by Microsoft are
3016 ``'print'`` and ``'edit'`` (to be used on files) as well as ``'explore'`` and
3017 ``'find'`` (to be used on directories).
3018
3019 :func:`startfile` returns as soon as the associated application is launched.
3020 There is no option to wait for the application to close, and no way to retrieve
3021 the application's exit status. The *path* parameter is relative to the current
3022 directory. If you want to use an absolute path, make sure the first character
Georg Brandl60203b42010-10-06 10:11:56 +00003023 is not a slash (``'/'``); the underlying Win32 :c:func:`ShellExecute` function
Georg Brandl116aa622007-08-15 14:28:22 +00003024 doesn't work if it is. Use the :func:`os.path.normpath` function to ensure that
Benjamin Petersonf650e462010-05-06 23:03:05 +00003025 the path is properly encoded for Win32.
3026
3027 Availability: Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00003028
Georg Brandl116aa622007-08-15 14:28:22 +00003029
3030.. function:: system(command)
3031
3032 Execute the command (a string) in a subshell. This is implemented by calling
Georg Brandl60203b42010-10-06 10:11:56 +00003033 the Standard C function :c:func:`system`, and has the same limitations.
Georg Brandl8f7b4272010-10-14 06:35:53 +00003034 Changes to :data:`sys.stdin`, etc. are not reflected in the environment of
3035 the executed command. If *command* generates any output, it will be sent to
3036 the interpreter standard output stream.
Georg Brandl116aa622007-08-15 14:28:22 +00003037
3038 On Unix, the return value is the exit status of the process encoded in the
Georg Brandl8f7b4272010-10-14 06:35:53 +00003039 format specified for :func:`wait`. Note that POSIX does not specify the
3040 meaning of the return value of the C :c:func:`system` function, so the return
3041 value of the Python function is system-dependent.
Georg Brandl116aa622007-08-15 14:28:22 +00003042
Georg Brandl8f7b4272010-10-14 06:35:53 +00003043 On Windows, the return value is that returned by the system shell after
3044 running *command*. The shell is given by the Windows environment variable
3045 :envvar:`COMSPEC`: it is usually :program:`cmd.exe`, which returns the exit
3046 status of the command run; on systems using a non-native shell, consult your
3047 shell documentation.
Georg Brandl116aa622007-08-15 14:28:22 +00003048
Georg Brandl8f7b4272010-10-14 06:35:53 +00003049 The :mod:`subprocess` module provides more powerful facilities for spawning
3050 new processes and retrieving their results; using that module is preferable
3051 to using this function. See the :ref:`subprocess-replacements` section in
3052 the :mod:`subprocess` documentation for some helpful recipes.
Georg Brandl116aa622007-08-15 14:28:22 +00003053
Benjamin Petersonf650e462010-05-06 23:03:05 +00003054 Availability: Unix, Windows.
3055
Georg Brandl116aa622007-08-15 14:28:22 +00003056
3057.. function:: times()
3058
Larry Hastings605a62d2012-06-24 04:33:36 -07003059 Returns the current global process times.
3060 The return value is an object with five attributes:
3061
3062 * :attr:`user` - user time
3063 * :attr:`system` - system time
3064 * :attr:`children_user` - user time of all child processes
3065 * :attr:`children_system` - system time of all child processes
3066 * :attr:`elapsed` - elapsed real time since a fixed point in the past
3067
3068 For backwards compatibility, this object also behaves like a five-tuple
3069 containing :attr:`user`, :attr:`system`, :attr:`children_user`,
3070 :attr:`children_system`, and :attr:`elapsed` in that order.
3071
3072 See the Unix manual page
Benjamin Petersonf650e462010-05-06 23:03:05 +00003073 :manpage:`times(2)` or the corresponding Windows Platform API documentation.
Larry Hastings605a62d2012-06-24 04:33:36 -07003074 On Windows, only :attr:`user` and :attr:`system` are known; the other
3075 attributes are zero.
Benjamin Petersonf650e462010-05-06 23:03:05 +00003076
Georg Brandl8a5555f2012-06-24 13:29:09 +02003077 Availability: Unix, Windows.
Georg Brandl116aa622007-08-15 14:28:22 +00003078
Larry Hastings605a62d2012-06-24 04:33:36 -07003079 .. versionchanged:: 3.3
3080 Return type changed from a tuple to a tuple-like object
3081 with named attributes.
3082
Georg Brandl116aa622007-08-15 14:28:22 +00003083
3084.. function:: wait()
3085
3086 Wait for completion of a child process, and return a tuple containing its pid
3087 and exit status indication: a 16-bit number, whose low byte is the signal number
3088 that killed the process, and whose high byte is the exit status (if the signal
3089 number is zero); the high bit of the low byte is set if a core file was
Benjamin Petersonf650e462010-05-06 23:03:05 +00003090 produced.
3091
3092 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00003093
Ross Lagerwall7807c352011-03-17 20:20:30 +02003094.. function:: waitid(idtype, id, options)
3095
3096 Wait for the completion of one or more child processes.
3097 *idtype* can be :data:`P_PID`, :data:`P_PGID` or :data:`P_ALL`.
3098 *id* specifies the pid to wait on.
3099 *options* is constructed from the ORing of one or more of :data:`WEXITED`,
3100 :data:`WSTOPPED` or :data:`WCONTINUED` and additionally may be ORed with
3101 :data:`WNOHANG` or :data:`WNOWAIT`. The return value is an object
3102 representing the data contained in the :c:type:`siginfo_t` structure, namely:
3103 :attr:`si_pid`, :attr:`si_uid`, :attr:`si_signo`, :attr:`si_status`,
3104 :attr:`si_code` or ``None`` if :data:`WNOHANG` is specified and there are no
3105 children in a waitable state.
3106
3107 Availability: Unix.
3108
3109 .. versionadded:: 3.3
3110
3111.. data:: P_PID
3112 P_PGID
3113 P_ALL
3114
3115 These are the possible values for *idtype* in :func:`waitid`. They affect
3116 how *id* is interpreted.
3117
3118 Availability: Unix.
3119
3120 .. versionadded:: 3.3
3121
3122.. data:: WEXITED
3123 WSTOPPED
3124 WNOWAIT
3125
3126 Flags that can be used in *options* in :func:`waitid` that specify what
3127 child signal to wait for.
3128
3129 Availability: Unix.
3130
3131 .. versionadded:: 3.3
3132
3133
3134.. data:: CLD_EXITED
3135 CLD_DUMPED
3136 CLD_TRAPPED
3137 CLD_CONTINUED
3138
3139 These are the possible values for :attr:`si_code` in the result returned by
3140 :func:`waitid`.
3141
3142 Availability: Unix.
3143
3144 .. versionadded:: 3.3
3145
Georg Brandl116aa622007-08-15 14:28:22 +00003146
3147.. function:: waitpid(pid, options)
3148
3149 The details of this function differ on Unix and Windows.
3150
3151 On Unix: Wait for completion of a child process given by process id *pid*, and
3152 return a tuple containing its process id and exit status indication (encoded as
3153 for :func:`wait`). The semantics of the call are affected by the value of the
3154 integer *options*, which should be ``0`` for normal operation.
3155
3156 If *pid* is greater than ``0``, :func:`waitpid` requests status information for
3157 that specific process. If *pid* is ``0``, the request is for the status of any
3158 child in the process group of the current process. If *pid* is ``-1``, the
3159 request pertains to any child of the current process. If *pid* is less than
3160 ``-1``, status is requested for any process in the process group ``-pid`` (the
3161 absolute value of *pid*).
3162
Benjamin Peterson4cd6a952008-08-17 20:23:46 +00003163 An :exc:`OSError` is raised with the value of errno when the syscall
3164 returns -1.
3165
Georg Brandl116aa622007-08-15 14:28:22 +00003166 On Windows: Wait for completion of a process given by process handle *pid*, and
3167 return a tuple containing *pid*, and its exit status shifted left by 8 bits
3168 (shifting makes cross-platform use of the function easier). A *pid* less than or
3169 equal to ``0`` has no special meaning on Windows, and raises an exception. The
3170 value of integer *options* has no effect. *pid* can refer to any process whose
Serhiy Storchakadab83542013-10-13 20:12:43 +03003171 id is known, not necessarily a child process. The :func:`spawn\* <spawnl>`
3172 functions called with :const:`P_NOWAIT` return suitable process handles.
Georg Brandl116aa622007-08-15 14:28:22 +00003173
3174
Ezio Melottiba4d8ed2012-11-23 19:45:52 +02003175.. function:: wait3(options)
Georg Brandl116aa622007-08-15 14:28:22 +00003176
3177 Similar to :func:`waitpid`, except no process id argument is given and a
3178 3-element tuple containing the child's process id, exit status indication, and
3179 resource usage information is returned. Refer to :mod:`resource`.\
Serhiy Storchakadab83542013-10-13 20:12:43 +03003180 :func:`~resource.getrusage` for details on resource usage information. The
3181 option argument is the same as that provided to :func:`waitpid` and
3182 :func:`wait4`.
Benjamin Petersonf650e462010-05-06 23:03:05 +00003183
Georg Brandl116aa622007-08-15 14:28:22 +00003184 Availability: Unix.
3185
Georg Brandl116aa622007-08-15 14:28:22 +00003186
Victor Stinner4195b5c2012-02-08 23:03:19 +01003187.. function:: wait4(pid, options)
Georg Brandl116aa622007-08-15 14:28:22 +00003188
3189 Similar to :func:`waitpid`, except a 3-element tuple, containing the child's
3190 process id, exit status indication, and resource usage information is returned.
Serhiy Storchakadab83542013-10-13 20:12:43 +03003191 Refer to :mod:`resource`.\ :func:`~resource.getrusage` for details on
3192 resource usage information. The arguments to :func:`wait4` are the same
3193 as those provided to :func:`waitpid`.
Benjamin Petersonf650e462010-05-06 23:03:05 +00003194
3195 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00003196
Georg Brandl116aa622007-08-15 14:28:22 +00003197
3198.. data:: WNOHANG
3199
3200 The option for :func:`waitpid` to return immediately if no child process status
3201 is available immediately. The function returns ``(0, 0)`` in this case.
Benjamin Petersonf650e462010-05-06 23:03:05 +00003202
Georg Brandlc575c902008-09-13 17:46:05 +00003203 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00003204
3205
3206.. data:: WCONTINUED
3207
3208 This option causes child processes to be reported if they have been continued
Benjamin Petersonf650e462010-05-06 23:03:05 +00003209 from a job control stop since their status was last reported.
3210
Georg Brandl8a5555f2012-06-24 13:29:09 +02003211 Availability: some Unix systems.
Georg Brandl116aa622007-08-15 14:28:22 +00003212
Georg Brandl116aa622007-08-15 14:28:22 +00003213
3214.. data:: WUNTRACED
3215
3216 This option causes child processes to be reported if they have been stopped but
Benjamin Petersonf650e462010-05-06 23:03:05 +00003217 their current state has not been reported since they were stopped.
3218
3219 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00003220
Georg Brandl116aa622007-08-15 14:28:22 +00003221
3222The following functions take a process status code as returned by
3223:func:`system`, :func:`wait`, or :func:`waitpid` as a parameter. They may be
3224used to determine the disposition of a process.
3225
Georg Brandl116aa622007-08-15 14:28:22 +00003226.. function:: WCOREDUMP(status)
3227
Christian Heimesfaf2f632008-01-06 16:59:19 +00003228 Return ``True`` if a core dump was generated for the process, otherwise
Benjamin Petersonf650e462010-05-06 23:03:05 +00003229 return ``False``.
3230
3231 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00003232
Georg Brandl116aa622007-08-15 14:28:22 +00003233
3234.. function:: WIFCONTINUED(status)
3235
Christian Heimesfaf2f632008-01-06 16:59:19 +00003236 Return ``True`` if the process has been continued from a job control stop,
Benjamin Petersonf650e462010-05-06 23:03:05 +00003237 otherwise return ``False``.
3238
3239 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00003240
Georg Brandl116aa622007-08-15 14:28:22 +00003241
3242.. function:: WIFSTOPPED(status)
3243
Christian Heimesfaf2f632008-01-06 16:59:19 +00003244 Return ``True`` if the process has been stopped, otherwise return
Benjamin Petersonf650e462010-05-06 23:03:05 +00003245 ``False``.
3246
3247 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00003248
3249
3250.. function:: WIFSIGNALED(status)
3251
Christian Heimesfaf2f632008-01-06 16:59:19 +00003252 Return ``True`` if the process exited due to a signal, otherwise return
Benjamin Petersonf650e462010-05-06 23:03:05 +00003253 ``False``.
3254
3255 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00003256
3257
3258.. function:: WIFEXITED(status)
3259
Christian Heimesfaf2f632008-01-06 16:59:19 +00003260 Return ``True`` if the process exited using the :manpage:`exit(2)` system call,
Benjamin Petersonf650e462010-05-06 23:03:05 +00003261 otherwise return ``False``.
3262
3263 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00003264
3265
3266.. function:: WEXITSTATUS(status)
3267
3268 If ``WIFEXITED(status)`` is true, return the integer parameter to the
3269 :manpage:`exit(2)` system call. Otherwise, the return value is meaningless.
Benjamin Petersonf650e462010-05-06 23:03:05 +00003270
Georg Brandlc575c902008-09-13 17:46:05 +00003271 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00003272
3273
3274.. function:: WSTOPSIG(status)
3275
Benjamin Petersonf650e462010-05-06 23:03:05 +00003276 Return the signal which caused the process to stop.
3277
3278 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00003279
3280
3281.. function:: WTERMSIG(status)
3282
Benjamin Petersonf650e462010-05-06 23:03:05 +00003283 Return the signal which caused the process to exit.
3284
3285 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00003286
3287
Benjamin Peterson94b580d2011-08-02 17:30:04 -05003288Interface to the scheduler
3289--------------------------
3290
3291These functions control how a process is allocated CPU time by the operating
3292system. They are only available on some Unix platforms. For more detailed
3293information, consult your Unix manpages.
3294
3295.. versionadded:: 3.3
3296
Andrew Kuchling4921a082013-06-21 11:49:57 -04003297The following scheduling policies are exposed if they are supported by the
Benjamin Peterson94b580d2011-08-02 17:30:04 -05003298operating system.
3299
3300.. data:: SCHED_OTHER
3301
3302 The default scheduling policy.
3303
3304.. data:: SCHED_BATCH
3305
3306 Scheduling policy for CPU-intensive processes that tries to preserve
3307 interactivity on the rest of the computer.
3308
3309.. data:: SCHED_IDLE
3310
3311 Scheduling policy for extremely low priority background tasks.
3312
3313.. data:: SCHED_SPORADIC
3314
3315 Scheduling policy for sporadic server programs.
3316
3317.. data:: SCHED_FIFO
3318
3319 A First In First Out scheduling policy.
3320
3321.. data:: SCHED_RR
3322
3323 A round-robin scheduling policy.
3324
3325.. data:: SCHED_RESET_ON_FORK
3326
3327 This flag can OR'ed with any other scheduling policy. When a process with
3328 this flag set forks, its child's scheduling policy and priority are reset to
3329 the default.
3330
3331
3332.. class:: sched_param(sched_priority)
3333
3334 This class represents tunable scheduling parameters used in
3335 :func:`sched_setparam`, :func:`sched_setscheduler`, and
3336 :func:`sched_getparam`. It is immutable.
3337
3338 At the moment, there is only one possible parameter:
3339
3340 .. attribute:: sched_priority
3341
3342 The scheduling priority for a scheduling policy.
3343
3344
3345.. function:: sched_get_priority_min(policy)
3346
3347 Get the minimum priority value for *policy*. *policy* is one of the
3348 scheduling policy constants above.
3349
3350
3351.. function:: sched_get_priority_max(policy)
3352
3353 Get the maximum priority value for *policy*. *policy* is one of the
3354 scheduling policy constants above.
3355
3356
3357.. function:: sched_setscheduler(pid, policy, param)
3358
3359 Set the scheduling policy for the process with PID *pid*. A *pid* of 0 means
3360 the calling process. *policy* is one of the scheduling policy constants
3361 above. *param* is a :class:`sched_param` instance.
3362
3363
3364.. function:: sched_getscheduler(pid)
3365
3366 Return the scheduling policy for the process with PID *pid*. A *pid* of 0
3367 means the calling process. The result is one of the scheduling policy
3368 constants above.
3369
3370
3371.. function:: sched_setparam(pid, param)
3372
3373 Set a scheduling parameters for the process with PID *pid*. A *pid* of 0 means
3374 the calling process. *param* is a :class:`sched_param` instance.
3375
3376
3377.. function:: sched_getparam(pid)
3378
3379 Return the scheduling parameters as a :class:`sched_param` instance for the
3380 process with PID *pid*. A *pid* of 0 means the calling process.
3381
3382
3383.. function:: sched_rr_get_interval(pid)
3384
3385 Return the round-robin quantum in seconds for the process with PID *pid*. A
3386 *pid* of 0 means the calling process.
3387
3388
3389.. function:: sched_yield()
3390
3391 Voluntarily relinquish the CPU.
3392
3393
Benjamin Peterson94b580d2011-08-02 17:30:04 -05003394.. function:: sched_setaffinity(pid, mask)
3395
Antoine Pitrou84869872012-08-04 16:16:35 +02003396 Restrict the process with PID *pid* (or the current process if zero) to a
3397 set of CPUs. *mask* is an iterable of integers representing the set of
3398 CPUs to which the process should be restricted.
Benjamin Peterson94b580d2011-08-02 17:30:04 -05003399
3400
Antoine Pitrou84869872012-08-04 16:16:35 +02003401.. function:: sched_getaffinity(pid)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05003402
Antoine Pitrou84869872012-08-04 16:16:35 +02003403 Return the set of CPUs the process with PID *pid* (or the current process
3404 if zero) is restricted to.
Benjamin Peterson94b580d2011-08-02 17:30:04 -05003405
3406
Georg Brandl116aa622007-08-15 14:28:22 +00003407.. _os-path:
3408
3409Miscellaneous System Information
3410--------------------------------
3411
3412
3413.. function:: confstr(name)
3414
3415 Return string-valued system configuration values. *name* specifies the
3416 configuration value to retrieve; it may be a string which is the name of a
3417 defined system value; these names are specified in a number of standards (POSIX,
3418 Unix 95, Unix 98, and others). Some platforms define additional names as well.
3419 The names known to the host operating system are given as the keys of the
3420 ``confstr_names`` dictionary. For configuration variables not included in that
Benjamin Petersonf650e462010-05-06 23:03:05 +00003421 mapping, passing an integer for *name* is also accepted.
Georg Brandl116aa622007-08-15 14:28:22 +00003422
3423 If the configuration value specified by *name* isn't defined, ``None`` is
3424 returned.
3425
3426 If *name* is a string and is not known, :exc:`ValueError` is raised. If a
3427 specific value for *name* is not supported by the host system, even if it is
3428 included in ``confstr_names``, an :exc:`OSError` is raised with
3429 :const:`errno.EINVAL` for the error number.
3430
Georg Brandl8a5555f2012-06-24 13:29:09 +02003431 Availability: Unix.
Benjamin Petersonf650e462010-05-06 23:03:05 +00003432
Georg Brandl116aa622007-08-15 14:28:22 +00003433
3434.. data:: confstr_names
3435
3436 Dictionary mapping names accepted by :func:`confstr` to the integer values
3437 defined for those names by the host operating system. This can be used to
Benjamin Petersonf650e462010-05-06 23:03:05 +00003438 determine the set of names known to the system.
3439
3440 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00003441
3442
Charles-Francois Natali44feda32013-05-20 14:40:46 +02003443.. function:: cpu_count()
3444
3445 Return the number of CPUs in the system. Returns None if undetermined.
3446
3447 .. versionadded:: 3.4
3448
3449
Georg Brandl116aa622007-08-15 14:28:22 +00003450.. function:: getloadavg()
3451
Christian Heimesa62da1d2008-01-12 19:39:10 +00003452 Return the number of processes in the system run queue averaged over the last
3453 1, 5, and 15 minutes or raises :exc:`OSError` if the load average was
Benjamin Petersonf650e462010-05-06 23:03:05 +00003454 unobtainable.
3455
3456 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00003457
Georg Brandl116aa622007-08-15 14:28:22 +00003458
3459.. function:: sysconf(name)
3460
3461 Return integer-valued system configuration values. If the configuration value
3462 specified by *name* isn't defined, ``-1`` is returned. The comments regarding
3463 the *name* parameter for :func:`confstr` apply here as well; the dictionary that
3464 provides information on the known names is given by ``sysconf_names``.
Benjamin Petersonf650e462010-05-06 23:03:05 +00003465
Georg Brandlc575c902008-09-13 17:46:05 +00003466 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00003467
3468
3469.. data:: sysconf_names
3470
3471 Dictionary mapping names accepted by :func:`sysconf` to the integer values
3472 defined for those names by the host operating system. This can be used to
Benjamin Petersonf650e462010-05-06 23:03:05 +00003473 determine the set of names known to the system.
3474
3475 Availability: Unix.
Georg Brandl116aa622007-08-15 14:28:22 +00003476
Christian Heimesfaf2f632008-01-06 16:59:19 +00003477The following data values are used to support path manipulation operations. These
Georg Brandl116aa622007-08-15 14:28:22 +00003478are defined for all platforms.
3479
3480Higher-level operations on pathnames are defined in the :mod:`os.path` module.
3481
3482
3483.. data:: curdir
3484
3485 The constant string used by the operating system to refer to the current
Georg Brandlc575c902008-09-13 17:46:05 +00003486 directory. This is ``'.'`` for Windows and POSIX. Also available via
3487 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00003488
3489
3490.. data:: pardir
3491
3492 The constant string used by the operating system to refer to the parent
Georg Brandlc575c902008-09-13 17:46:05 +00003493 directory. This is ``'..'`` for Windows and POSIX. Also available via
3494 :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00003495
3496
3497.. data:: sep
3498
Georg Brandlc575c902008-09-13 17:46:05 +00003499 The character used by the operating system to separate pathname components.
3500 This is ``'/'`` for POSIX and ``'\\'`` for Windows. Note that knowing this
3501 is not sufficient to be able to parse or concatenate pathnames --- use
Georg Brandl116aa622007-08-15 14:28:22 +00003502 :func:`os.path.split` and :func:`os.path.join` --- but it is occasionally
3503 useful. Also available via :mod:`os.path`.
3504
3505
3506.. data:: altsep
3507
3508 An alternative character used by the operating system to separate pathname
3509 components, or ``None`` if only one separator character exists. This is set to
3510 ``'/'`` on Windows systems where ``sep`` is a backslash. Also available via
3511 :mod:`os.path`.
3512
3513
3514.. data:: extsep
3515
3516 The character which separates the base filename from the extension; for example,
3517 the ``'.'`` in :file:`os.py`. Also available via :mod:`os.path`.
3518
Georg Brandl116aa622007-08-15 14:28:22 +00003519
3520.. data:: pathsep
3521
3522 The character conventionally used by the operating system to separate search
3523 path components (as in :envvar:`PATH`), such as ``':'`` for POSIX or ``';'`` for
3524 Windows. Also available via :mod:`os.path`.
3525
3526
3527.. data:: defpath
3528
Serhiy Storchakadab83542013-10-13 20:12:43 +03003529 The default search path used by :func:`exec\*p\* <execl>` and
3530 :func:`spawn\*p\* <spawnl>` if the environment doesn't have a ``'PATH'``
3531 key. Also available via :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00003532
3533
3534.. data:: linesep
3535
3536 The string used to separate (or, rather, terminate) lines on the current
Georg Brandlc575c902008-09-13 17:46:05 +00003537 platform. This may be a single character, such as ``'\n'`` for POSIX, or
3538 multiple characters, for example, ``'\r\n'`` for Windows. Do not use
3539 *os.linesep* as a line terminator when writing files opened in text mode (the
3540 default); use a single ``'\n'`` instead, on all platforms.
Georg Brandl116aa622007-08-15 14:28:22 +00003541
3542
3543.. data:: devnull
3544
Georg Brandl850a9902010-05-21 22:04:32 +00003545 The file path of the null device. For example: ``'/dev/null'`` for
3546 POSIX, ``'nul'`` for Windows. Also available via :mod:`os.path`.
Georg Brandl116aa622007-08-15 14:28:22 +00003547
Andrew Kuchling4921a082013-06-21 11:49:57 -04003548.. data:: RTLD_LAZY
3549 RTLD_NOW
3550 RTLD_GLOBAL
3551 RTLD_LOCAL
3552 RTLD_NODELETE
3553 RTLD_NOLOAD
3554 RTLD_DEEPBIND
3555
3556 Flags for use with the :func:`~sys.setdlopenflags` and
3557 :func:`~sys.getdlopenflags` functions. See the Unix manual page
3558 :manpage:`dlopen(3)` for what the different flags mean.
3559
3560 .. versionadded:: 3.3
Georg Brandl116aa622007-08-15 14:28:22 +00003561
3562.. _os-miscfunc:
3563
3564Miscellaneous Functions
3565-----------------------
3566
3567
3568.. function:: urandom(n)
3569
3570 Return a string of *n* random bytes suitable for cryptographic use.
3571
3572 This function returns random bytes from an OS-specific randomness source. The
3573 returned data should be unpredictable enough for cryptographic applications,
Georg Brandlf62445a2012-06-24 13:31:20 +02003574 though its exact quality depends on the OS implementation. On a Unix-like
Georg Brandlc6a2c9b2013-10-06 18:43:19 +02003575 system this will query ``/dev/urandom``, and on Windows it will use
3576 ``CryptGenRandom()``. If a randomness source is not found,
3577 :exc:`NotImplementedError` will be raised.
Andrew Svetlov03cb99c2012-10-16 13:15:06 +03003578
Andrew Svetlov2bfe3862012-10-16 13:52:25 +03003579 For an easy-to-use interface to the random number generator
3580 provided by your platform, please see :class:`random.SystemRandom`.