| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 1 :mod:`os` --- Miscellaneous operating system interfaces | 1 :mod:`os` --- Miscellaneous operating system interfaces |
| 2 ======================================================= | 2 ======================================================= |
| 3 | 3 |
| 4 .. module:: os | 4 .. module:: os |
| 5 :synopsis: Miscellaneous operating system interfaces. | 5 :synopsis: Miscellaneous operating system interfaces. |
| 6 | 6 |
| 7 | 7 |
| 8 This module provides a portable way of using operating system dependent | 8 This module provides a portable way of using operating system dependent |
| 9 functionality. If you just want to read or write a file see :func:`open`, if | 9 functionality. If you just want to read or write a file see :func:`open`, if |
| 10 you want to manipulate paths, see the :mod:`os.path` module, and if you want to | 10 you want to manipulate paths, see the :mod:`os.path` module, and if you want to |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 with the POSIX interface). | 22 with the POSIX interface). |
| 23 | 23 |
| 24 * Extensions peculiar to a particular operating system are also available | 24 * 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 | 25 through the :mod:`os` module, but using them is of course a threat to |
| 26 portability. | 26 portability. |
| 27 | 27 |
| 28 * All functions accepting path or file names accept both bytes and string | 28 * 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 | 29 objects, and result in an object of the same type, if a path or file name is |
| 30 returned. | 30 returned. |
| 31 | 31 |
| 32 .. note:: | |
| 33 | |
| 34 If not separately noted, all functions that claim "Availability: Unix" are | |
| 35 supported on Mac OS X, which builds on a Unix core. | |
| 36 | |
| 37 * An "Availability: Unix" note means that this function is commonly found on | 32 * An "Availability: Unix" note means that this function is commonly found on |
| 38 Unix systems. It does not make any claims about its existence on a specific | 33 Unix systems. It does not make any claims about its existence on a specific |
| 39 operating system. | 34 operating system. |
| 40 | 35 |
| 41 * If not separately noted, all functions that claim "Availability: Unix" are | 36 * If not separately noted, all functions that claim "Availability: Unix" are |
| 42 supported on Mac OS X, which builds on a Unix core. | 37 supported on Mac OS X, which builds on a Unix core. |
| 43 | 38 |
| 44 .. Availability notes get their own line and occur at the end of the function | 39 .. Availability notes get their own line and occur at the end of the function |
| 45 .. documentation. | 40 .. documentation. |
| 46 | 41 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 94 | 89 |
| 95 .. _os-procinfo: | 90 .. _os-procinfo: |
| 96 | 91 |
| 97 Process Parameters | 92 Process Parameters |
| 98 ------------------ | 93 ------------------ |
| 99 | 94 |
| 100 These functions and data items provide information and operate on the current | 95 These functions and data items provide information and operate on the current |
| 101 process and user. | 96 process and user. |
| 102 | 97 |
| 103 | 98 |
| 99 .. function:: ctermid() | |
| 100 | |
| 101 Return the filename corresponding to the controlling terminal of the process. | |
| 102 | |
| 103 Availability: Unix. | |
| 104 | |
| 105 | |
| 104 .. data:: environ | 106 .. data:: environ |
| 105 | 107 |
| 106 A mapping object representing the string environment. For example, | 108 A mapping object representing the string environment. For example, |
| 107 ``environ['HOME']`` is the pathname of your home directory (on some platforms ), | 109 ``environ['HOME']`` is the pathname of your home directory (on some platforms ), |
| 108 and is equivalent to ``getenv("HOME")`` in C. | 110 and is equivalent to ``getenv("HOME")`` in C. |
| 109 | 111 |
| 110 This mapping is captured the first time the :mod:`os` module is imported, | 112 This mapping is captured the first time the :mod:`os` module is imported, |
| 111 typically during Python startup as part of processing :file:`site.py`. Chang es | 113 typically during Python startup as part of processing :file:`site.py`. Chang es |
| 112 to the environment made after this time are not reflected in ``os.environ``, | 114 to the environment made after this time are not reflected in ``os.environ``, |
| 113 except for changes made by modifying ``os.environ`` directly. | 115 except for changes made by modifying ``os.environ`` directly. |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 171 | 173 |
| 172 .. versionadded:: 3.2 | 174 .. versionadded:: 3.2 |
| 173 | 175 |
| 174 | 176 |
| 175 .. function:: fsdecode(filename) | 177 .. function:: fsdecode(filename) |
| 176 | 178 |
| 177 Decode *filename* from the filesystem encoding with ``'surrogateescape'`` | 179 Decode *filename* from the filesystem encoding with ``'surrogateescape'`` |
| 178 error handler, or ``'strict'`` on Windows; return :class:`str` unchanged. | 180 error handler, or ``'strict'`` on Windows; return :class:`str` unchanged. |
| 179 | 181 |
| 180 :func:`fsencode` is the reverse function. | 182 :func:`fsencode` is the reverse function. |
| 183 | |
| 184 .. versionadded:: 3.2 | |
| 185 | |
| 186 | |
| 187 .. function:: getenv(key, default=None) | |
| 188 | |
| 189 Return the value of the environment variable *key* if it exists, or | |
| 190 *default* if it doesn't. *key*, *default* and the result are str. | |
| 191 | |
| 192 On Unix, keys and values are decoded with :func:`sys.getfilesystemencoding` | |
| 193 and ``'surrogateescape'`` error handler. Use :func:`os.getenvb` if you | |
| 194 would like to use a different encoding. | |
| 195 | |
| 196 Availability: most flavors of Unix, Windows. | |
| 197 | |
| 198 | |
| 199 .. function:: getenvb(key, default=None) | |
| 200 | |
| 201 Return the value of the environment variable *key* if it exists, or | |
| 202 *default* if it doesn't. *key*, *default* and the result are bytes. | |
| 203 | |
| 204 Availability: most flavors of Unix. | |
| 181 | 205 |
| 182 .. versionadded:: 3.2 | 206 .. versionadded:: 3.2 |
| 183 | 207 |
| 184 | 208 |
| 185 .. function:: get_exec_path(env=None) | 209 .. function:: get_exec_path(env=None) |
| 186 | 210 |
| 187 Returns the list of directories that will be searched for a named | 211 Returns the list of directories that will be searched for a named |
| 188 executable, similar to a shell, when launching a process. | 212 executable, similar to a shell, when launching a process. |
| 189 *env*, when specified, should be an environment variable dictionary | 213 *env*, when specified, should be an environment variable dictionary |
| 190 to lookup the PATH in. | 214 to lookup the PATH in. |
| 191 By default, when *env* is None, :data:`environ` is used. | 215 By default, when *env* is None, :data:`environ` is used. |
| 192 | 216 |
| 193 .. versionadded:: 3.2 | 217 .. versionadded:: 3.2 |
| 194 | 218 |
| 195 | 219 |
| 196 .. function:: ctermid() | |
| 197 | |
| 198 Return the filename corresponding to the controlling terminal of the process. | |
| 199 | |
| 200 Availability: Unix. | |
| 201 | |
| 202 | |
| 203 .. function:: getegid() | 220 .. function:: getegid() |
| 204 | 221 |
| 205 Return the effective group id of the current process. This corresponds to th e | 222 Return the effective group id of the current process. This corresponds to th e |
| 206 "set id" bit on the file being executed in the current process. | 223 "set id" bit on the file being executed in the current process. |
| 207 | 224 |
| 208 Availability: Unix. | 225 Availability: Unix. |
| 209 | 226 |
| 210 | 227 |
| 211 .. function:: geteuid() | 228 .. function:: geteuid() |
| 212 | 229 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 236 | 253 |
| 237 .. versionadded:: 3.3 | 254 .. versionadded:: 3.3 |
| 238 | 255 |
| 239 | 256 |
| 240 .. function:: getgroups() | 257 .. function:: getgroups() |
| 241 | 258 |
| 242 Return list of supplemental group ids associated with the current process. | 259 Return list of supplemental group ids associated with the current process. |
| 243 | 260 |
| 244 Availability: Unix. | 261 Availability: Unix. |
| 245 | 262 |
| 246 | 263 .. note:: On Mac OS X, :func:`getgroups` behavior differs somewhat from |
| 247 .. function:: initgroups(username, gid) | 264 other Unix platforms. If the Python interpreter was built with a |
| 248 | 265 deployment target of :const:`10.5` or earlier, :func:`getgroups` returns |
| 249 Call the system initgroups() to initialize the group access list with all of | 266 the list of effective group ids associated with the current user process; |
| 250 the groups of which the specified username is a member, plus the specified | 267 this list is limited to a system-defined number of entries, typically 16, |
| 251 group id. | 268 and may be modified by calls to :func:`setgroups` if suitably privileged. |
| 252 | 269 If built with a deployment target greater than :const:`10.5`, |
| 253 Availability: Unix. | 270 :func:`getgroups` returns the current group access list for the user |
| 254 | 271 associated with the effective user id of the process; the group access |
| 255 .. versionadded:: 3.2 | 272 list may change over the lifetime of the process, it is not affected by |
| 273 calls to :func:`setgroups`, and its length is not limited to 16. The | |
| 274 deployment target value, :const:`MACOSX_DEPLOYMENT_TARGET`, can be | |
| 275 obtained with :func:`sysconfig.get_config_var`. | |
| 256 | 276 |
| 257 | 277 |
| 258 .. function:: getlogin() | 278 .. function:: getlogin() |
| 259 | 279 |
| 260 Return the name of the user logged in on the controlling terminal of the | 280 Return the name of the user logged in on the controlling terminal of the |
| 261 process. For most purposes, it is more useful to use the environment variabl es | 281 process. For most purposes, it is more useful to use the environment variabl es |
| 262 :envvar:`LOGNAME` or :envvar:`USERNAME` to find out who the user is, or | 282 :envvar:`LOGNAME` or :envvar:`USERNAME` to find out who the user is, or |
| 263 ``pwd.getpwuid(os.getuid())[0]`` to get the login name of the currently | 283 ``pwd.getpwuid(os.getuid())[0]`` to get the login name of the currently |
| 264 effective user id. | 284 effective user id. |
| 265 | 285 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 292 | 312 |
| 293 | 313 |
| 294 .. function:: getppid() | 314 .. function:: getppid() |
| 295 | 315 |
| 296 .. index:: single: process; id of parent | 316 .. index:: single: process; id of parent |
| 297 | 317 |
| 298 Return the parent's process id. When the parent process has exited, on Unix | 318 Return the parent's process id. When the parent process has exited, on Unix |
| 299 the id returned is the one of the init process (1), on Windows it is still | 319 the id returned is the one of the init process (1), on Windows it is still |
| 300 the same id, which may be already reused by another process. | 320 the same id, which may be already reused by another process. |
| 301 | 321 |
| 302 Availability: Unix, Windows | 322 Availability: Unix, Windows. |
| 303 | 323 |
| 304 .. versionchanged:: 3.2 | 324 .. versionchanged:: 3.2 |
| 305 Added support for Windows. | 325 Added support for Windows. |
| 306 | 326 |
| 327 | |
| 307 .. function:: getpriority(which, who) | 328 .. function:: getpriority(which, who) |
| 308 | 329 |
| 309 .. index:: single: process; scheduling priority | 330 .. index:: single: process; scheduling priority |
| 310 | 331 |
| 311 Get program scheduling priority. The value *which* is one of | 332 Get program scheduling priority. The value *which* is one of |
| 312 :const:`PRIO_PROCESS`, :const:`PRIO_PGRP`, or :const:`PRIO_USER`, and *who* | 333 :const:`PRIO_PROCESS`, :const:`PRIO_PGRP`, or :const:`PRIO_USER`, and *who* |
| 313 is interpreted relative to *which* (a process identifier for | 334 is interpreted relative to *which* (a process identifier for |
| 314 :const:`PRIO_PROCESS`, process group identifier for :const:`PRIO_PGRP`, and a | 335 :const:`PRIO_PROCESS`, process group identifier for :const:`PRIO_PGRP`, and a |
| 315 user ID for :const:`PRIO_USER`). A zero value for *who* denotes | 336 user ID for :const:`PRIO_USER`). A zero value for *who* denotes |
| 316 (respectively) the calling process, the process group of the calling process, | 337 (respectively) the calling process, the process group of the calling process, |
| 317 or the real user ID of the calling process. | 338 or the real user ID of the calling process. |
| 318 | 339 |
| 319 Availability: Unix | 340 Availability: Unix. |
| 320 | 341 |
| 321 .. versionadded:: 3.3 | 342 .. versionadded:: 3.3 |
| 322 | 343 |
| 323 .. function:: getresuid() | |
| 324 | |
| 325 Return a tuple (ruid, euid, suid) denoting the current process's | |
| 326 real, effective, and saved user ids. | |
| 327 | |
| 328 Availability: Unix. | |
| 329 | |
| 330 .. versionadded:: 3.2 | |
| 331 | |
| 332 | |
| 333 .. function:: getresgid() | |
| 334 | |
| 335 Return a tuple (rgid, egid, sgid) denoting the current process's | |
| 336 real, effective, and saved group ids. | |
| 337 | |
| 338 Availability: Unix. | |
| 339 | |
| 340 .. versionadded:: 3.2 | |
| 341 | |
| 342 | |
| 343 .. function:: getuid() | |
| 344 | |
| 345 .. index:: single: user; id | |
| 346 | |
| 347 Return the current process's user id. | |
| 348 | |
| 349 Availability: Unix. | |
| 350 | |
| 351 | |
| 352 .. function:: getenv(key, default=None) | |
| 353 | |
| 354 Return the value of the environment variable *key* if it exists, or | |
| 355 *default* if it doesn't. *key*, *default* and the result are str. | |
| 356 | |
| 357 On Unix, keys and values are decoded with :func:`sys.getfilesystemencoding` | |
| 358 and ``'surrogateescape'`` error handler. Use :func:`os.getenvb` if you | |
| 359 would like to use a different encoding. | |
| 360 | |
| 361 Availability: most flavors of Unix, Windows. | |
| 362 | |
| 363 | |
| 364 .. function:: getenvb(key, default=None) | |
| 365 | |
| 366 Return the value of the environment variable *key* if it exists, or | |
| 367 *default* if it doesn't. *key*, *default* and the result are bytes. | |
| 368 | |
| 369 Availability: most flavors of Unix. | |
| 370 | |
| 371 .. versionadded:: 3.2 | |
| 372 | 344 |
| 373 .. data:: PRIO_PROCESS | 345 .. data:: PRIO_PROCESS |
| 374 PRIO_PGRP | 346 PRIO_PGRP |
| 375 PRIO_USER | 347 PRIO_USER |
| 376 | 348 |
| 377 Parameters for :func:`getpriority` and :func:`setpriority` functions. | 349 Parameters for the :func:`getpriority` and :func:`setpriority` functions. |
| 378 | 350 |
| 379 Availability: Unix. | 351 Availability: Unix. |
| 380 | 352 |
| 381 .. versionadded:: 3.3 | 353 .. versionadded:: 3.3 |
| 354 | |
| 355 | |
| 356 .. function:: getresuid() | |
| 357 | |
| 358 Return a tuple (ruid, euid, suid) denoting the current process's | |
| 359 real, effective, and saved user ids. | |
| 360 | |
| 361 Availability: Unix. | |
| 362 | |
| 363 .. versionadded:: 3.2 | |
| 364 | |
| 365 | |
| 366 .. function:: getresgid() | |
| 367 | |
| 368 Return a tuple (rgid, egid, sgid) denoting the current process's | |
| 369 real, effective, and saved group ids. | |
| 370 | |
| 371 Availability: Unix. | |
| 372 | |
| 373 .. versionadded:: 3.2 | |
| 374 | |
| 375 | |
| 376 .. function:: getuid() | |
| 377 | |
| 378 .. index:: single: user; id | |
| 379 | |
| 380 Return the current process's user id. | |
| 381 | |
| 382 Availability: Unix. | |
| 383 | |
| 384 | |
| 385 .. function:: initgroups(username, gid) | |
| 386 | |
| 387 Call the system initgroups() to initialize the group access list with all of | |
| 388 the groups of which the specified username is a member, plus the specified | |
| 389 group id. | |
| 390 | |
| 391 Availability: Unix. | |
| 392 | |
| 393 .. versionadded:: 3.2 | |
| 394 | |
| 382 | 395 |
| 383 .. function:: putenv(key, value) | 396 .. function:: putenv(key, value) |
| 384 | 397 |
| 385 .. index:: single: environment variables; setting | 398 .. index:: single: environment variables; setting |
| 386 | 399 |
| 387 Set the environment variable named *key* to the string *value*. Such | 400 Set the environment variable named *key* to the string *value*. Such |
| 388 changes to the environment affect subprocesses started with :func:`os.system` , | 401 changes to the environment affect subprocesses started with :func:`os.system` , |
| 389 :func:`popen` or :func:`fork` and :func:`execv`. | 402 :func:`popen` or :func:`fork` and :func:`execv`. |
| 390 | 403 |
| 391 Availability: most flavors of Unix, Windows. | 404 Availability: most flavors of Unix, Windows. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 423 | 436 |
| 424 | 437 |
| 425 .. function:: setgroups(groups) | 438 .. function:: setgroups(groups) |
| 426 | 439 |
| 427 Set the list of supplemental group ids associated with the current process to | 440 Set the list of supplemental group ids associated with the current process to |
| 428 *groups*. *groups* must be a sequence, and each element must be an integer | 441 *groups*. *groups* must be a sequence, and each element must be an integer |
| 429 identifying a group. This operation is typically available only to the superu ser. | 442 identifying a group. This operation is typically available only to the superu ser. |
| 430 | 443 |
| 431 Availability: Unix. | 444 Availability: Unix. |
| 432 | 445 |
| 446 .. note:: On Mac OS X, the length of *groups* may not exceed the | |
| 447 system-defined maximum number of effective group ids, typically 16. | |
| 448 See the documentation for :func:`getgroups` for cases where it may not | |
| 449 return the same group list set by calling setgroups(). | |
| 433 | 450 |
| 434 .. function:: setpgrp() | 451 .. function:: setpgrp() |
| 435 | 452 |
| 436 Call the system call :c:func:`setpgrp` or :c:func:`setpgrp(0, 0)` depending o n | 453 Call the system call :c:func:`setpgrp` or :c:func:`setpgrp(0, 0)` depending o n |
| 437 which version is implemented (if any). See the Unix manual for the semantics . | 454 which version is implemented (if any). See the Unix manual for the semantics . |
| 438 | 455 |
| 439 Availability: Unix. | 456 Availability: Unix. |
| 440 | 457 |
| 441 | 458 |
| 442 .. function:: setpgid(pid, pgrp) | 459 .. function:: setpgid(pid, pgrp) |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 546 | 563 |
| 547 Availability: Unix, Windows. | 564 Availability: Unix, Windows. |
| 548 | 565 |
| 549 | 566 |
| 550 .. function:: uname() | 567 .. function:: uname() |
| 551 | 568 |
| 552 .. index:: | 569 .. index:: |
| 553 single: gethostname() (in module socket) | 570 single: gethostname() (in module socket) |
| 554 single: gethostbyaddr() (in module socket) | 571 single: gethostbyaddr() (in module socket) |
| 555 | 572 |
| 556 Return a 5-tuple containing information identifying the current operating | 573 Returns information identifying the current operating system. |
| 557 system. The tuple contains 5 strings: ``(sysname, nodename, release, version , | 574 The return value is an object with five attributes: |
| 558 machine)``. Some systems truncate the nodename to 8 characters or to the | 575 |
| 576 * :attr:`sysname` - operating system name | |
| 577 * :attr:`nodename` - name of machine on network (implementation-defined) | |
| 578 * :attr:`release` - operating system release | |
| 579 * :attr:`version` - operating system version | |
| 580 * :attr:`machine` - hardware identifier | |
| 581 | |
| 582 For backwards compatibility, this object is also iterable, behaving | |
| 583 like a five-tuple containing :attr:`sysname`, :attr:`nodename`, | |
| 584 :attr:`release`, :attr:`version`, and :attr:`machine` | |
| 585 in that order. | |
| 586 | |
| 587 Some systems truncate :attr:`nodename` to 8 characters or to the | |
| 559 leading component; a better way to get the hostname is | 588 leading component; a better way to get the hostname is |
| 560 :func:`socket.gethostname` or even | 589 :func:`socket.gethostname` or even |
| 561 ``socket.gethostbyaddr(socket.gethostname())``. | 590 ``socket.gethostbyaddr(socket.gethostname())``. |
| 562 | 591 |
| 563 Availability: recent flavors of Unix. | 592 Availability: recent flavors of Unix. |
| 593 | |
| 594 .. versionchanged:: 3.3 | |
| 595 Return type changed from a tuple to a tuple-like object | |
| 596 with named attributes. | |
| 564 | 597 |
| 565 | 598 |
| 566 .. function:: unsetenv(key) | 599 .. function:: unsetenv(key) |
| 567 | 600 |
| 568 .. index:: single: environment variables; deleting | 601 .. index:: single: environment variables; deleting |
| 569 | 602 |
| 570 Unset (delete) the environment variable named *key*. Such changes to the | 603 Unset (delete) the environment variable named *key*. Such changes to the |
| 571 environment affect subprocesses started with :func:`os.system`, :func:`popen` or | 604 environment affect subprocesses started with :func:`os.system`, :func:`popen` or |
| 572 :func:`fork` and :func:`execv`. | 605 :func:`fork` and :func:`execv`. |
| 573 | 606 |
| 574 When :func:`unsetenv` is supported, deletion of items in ``os.environ`` is | 607 When :func:`unsetenv` is supported, deletion of items in ``os.environ`` is |
| 575 automatically translated into a corresponding call to :func:`unsetenv`; howev er, | 608 automatically translated into a corresponding call to :func:`unsetenv`; howev er, |
| 576 calls to :func:`unsetenv` don't update ``os.environ``, so it is actually | 609 calls to :func:`unsetenv` don't update ``os.environ``, so it is actually |
| 577 preferable to delete items of ``os.environ``. | 610 preferable to delete items of ``os.environ``. |
| 578 | 611 |
| 579 Availability: most flavors of Unix, Windows. | 612 Availability: most flavors of Unix, Windows. |
| 580 | 613 |
| 581 | 614 |
| 582 .. _os-newstreams: | 615 .. _os-newstreams: |
| 583 | 616 |
| 584 File Object Creation | 617 File Object Creation |
| 585 -------------------- | 618 -------------------- |
| 586 | 619 |
| 587 These functions create new :term:`file objects <file object>`. (See also :func:` open`.) | 620 This function creates new :term:`file objects <file object>`. (See also |
| 588 | 621 :func:`~os.open` for opening file descriptors.) |
| 589 | 622 |
| 590 .. function:: fdopen(fd[, mode[, bufsize]]) | 623 |
| 591 | 624 .. function:: fdopen(fd, *args, **kwargs) |
| 592 .. index:: single: I/O control; buffering | 625 |
| 593 | 626 Return an open file object connected to the file descriptor *fd*. This is an |
| 594 Return an open file object connected to the file descriptor *fd*. The *mode* | 627 alias of the :func:`open` built-in function and accepts the same arguments. |
| 595 and *bufsize* arguments have the same meaning as the corresponding arguments to | 628 The only difference is that the first argument of :func:`fdopen` must always |
| 596 the built-in :func:`open` function. | 629 be an integer. |
| 597 | |
| 598 When specified, the *mode* argument must start with one of the letters | |
| 599 ``'r'``, ``'w'``, or ``'a'``, otherwise a :exc:`ValueError` is raised. | |
| 600 | |
| 601 On Unix, when the *mode* argument starts with ``'a'``, the *O_APPEND* flag is | |
| 602 set on the file descriptor (which the :c:func:`fdopen` implementation already | |
| 603 does on most platforms). | |
| 604 | |
| 605 Availability: Unix, Windows. | |
| 606 | 630 |
| 607 | 631 |
| 608 .. _os-fd-ops: | 632 .. _os-fd-ops: |
| 609 | 633 |
| 610 File Descriptor Operations | 634 File Descriptor Operations |
| 611 -------------------------- | 635 -------------------------- |
| 612 | 636 |
| 613 These functions operate on I/O streams referenced using file descriptors. | 637 These functions operate on I/O streams referenced using file descriptors. |
| 614 | 638 |
| 615 File descriptors are small integers corresponding to a file that has been opened | 639 File descriptors are small integers corresponding to a file that has been opened |
| 616 by the current process. For example, standard input is usually file descriptor | 640 by the current process. For example, standard input is usually file descriptor |
| 617 0, standard output is 1, and standard error is 2. Further files opened by a | 641 0, standard output is 1, and standard error is 2. Further files opened by a |
| 618 process will then be assigned 3, 4, 5, and so forth. The name "file descriptor" | 642 process will then be assigned 3, 4, 5, and so forth. The name "file descriptor" |
| 619 is slightly deceptive; on Unix platforms, sockets and pipes are also referenced | 643 is slightly deceptive; on Unix platforms, sockets and pipes are also referenced |
| 620 by file descriptors. | 644 by file descriptors. |
| 621 | 645 |
| 622 The :meth:`~file.fileno` method can be used to obtain the file descriptor | 646 The :meth:`~file.fileno` method can be used to obtain the file descriptor |
| 623 associated with a :term:`file object` when required. Note that using the file | 647 associated with a :term:`file object` when required. Note that using the file |
| 624 descriptor directly will bypass the file object methods, ignoring aspects such | 648 descriptor directly will bypass the file object methods, ignoring aspects such |
| 625 as internal buffering of data. | 649 as internal buffering of data. |
| 626 | 650 |
| 627 .. data:: AT_SYMLINK_NOFOLLOW | |
| 628 AT_EACCESS | |
| 629 AT_FDCWD | |
| 630 AT_REMOVEDIR | |
| 631 AT_SYMLINK_FOLLOW | |
| 632 UTIME_NOW | |
| 633 UTIME_OMIT | |
| 634 | |
| 635 These parameters are used as flags to the \*at family of functions. | |
| 636 | |
| 637 Availability: Unix. | |
| 638 | |
| 639 .. versionadded:: 3.3 | |
| 640 | |
| 641 | 651 |
| 642 .. function:: close(fd) | 652 .. function:: close(fd) |
| 643 | 653 |
| 644 Close file descriptor *fd*. | 654 Close file descriptor *fd*. |
| 645 | 655 |
| 646 Availability: Unix, Windows. | 656 Availability: Unix, Windows. |
| 647 | 657 |
| 648 .. note:: | 658 .. note:: |
| 649 | 659 |
| 650 This function is intended for low-level I/O and must be applied to a file | 660 This function is intended for low-level I/O and must be applied to a file |
| 651 descriptor as returned by :func:`os.open` or :func:`pipe`. To close a "fi le | 661 descriptor as returned by :func:`os.open` or :func:`pipe`. To close a "fi le |
| 652 object" returned by the built-in function :func:`open` or by :func:`popen` or | 662 object" returned by the built-in function :func:`open` or by :func:`popen` or |
| 653 :func:`fdopen`, use its :meth:`~file.close` method. | 663 :func:`fdopen`, use its :meth:`~file.close` method. |
| 654 | 664 |
| 655 | 665 |
| 656 .. function:: closerange(fd_low, fd_high) | 666 .. function:: closerange(fd_low, fd_high) |
| 657 | 667 |
| 658 Close all file descriptors from *fd_low* (inclusive) to *fd_high* (exclusive) , | 668 Close all file descriptors from *fd_low* (inclusive) to *fd_high* (exclusive) , |
| 659 ignoring errors. Equivalent to:: | 669 ignoring errors. Equivalent to (but much faster than):: |
| 660 | 670 |
| 661 for fd in range(fd_low, fd_high): | 671 for fd in range(fd_low, fd_high): |
| 662 try: | 672 try: |
| 663 os.close(fd) | 673 os.close(fd) |
| 664 except OSError: | 674 except OSError: |
| 665 pass | 675 pass |
| 666 | 676 |
| 667 Availability: Unix, Windows. | 677 Availability: Unix, Windows. |
| 668 | 678 |
| 669 | 679 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 680 Availability: Unix, Windows. | 690 Availability: Unix, Windows. |
| 681 | 691 |
| 682 | 692 |
| 683 .. function:: dup2(fd, fd2) | 693 .. function:: dup2(fd, fd2) |
| 684 | 694 |
| 685 Duplicate file descriptor *fd* to *fd2*, closing the latter first if necessar y. | 695 Duplicate file descriptor *fd* to *fd2*, closing the latter first if necessar y. |
| 686 | 696 |
| 687 Availability: Unix, Windows. | 697 Availability: Unix, Windows. |
| 688 | 698 |
| 689 | 699 |
| 690 .. function:: faccessat(dirfd, path, mode, flags=0) | |
| 691 | |
| 692 Like :func:`access` but if *path* is relative, it is taken as relative to *di rfd*. | |
| 693 *flags* is optional and can be constructed by ORing together zero or more | |
| 694 of these values: :data:`AT_SYMLINK_NOFOLLOW`, :data:`AT_EACCESS`. | |
| 695 If *path* is relative and *dirfd* is the special value :data:`AT_FDCWD`, then *path* | |
| 696 is interpreted relative to the current working directory. | |
| 697 | |
| 698 Availability: Unix. | |
| 699 | |
| 700 .. versionadded:: 3.3 | |
| 701 | |
| 702 | |
| 703 .. function:: fchmod(fd, mode) | 700 .. function:: fchmod(fd, mode) |
| 704 | 701 |
| 705 Change the mode of the file given by *fd* to the numeric *mode*. See the doc s | 702 Change the mode of the file given by *fd* to the numeric *mode*. See the |
| 706 for :func:`chmod` for possible values of *mode*. | 703 docs for :func:`chmod` for possible values of *mode*. As of Python 3.3, this |
| 707 | 704 is equivalent to ``os.chmod(fd, mode)``. |
| 708 Availability: Unix. | 705 |
| 709 | 706 Availability: Unix. |
| 710 | |
| 711 .. function:: fchmodat(dirfd, path, mode, flags=0) | |
| 712 | |
| 713 Like :func:`chmod` but if *path* is relative, it is taken as relative to *dir fd*. | |
| 714 *flags* is optional and may be 0 or :data:`AT_SYMLINK_NOFOLLOW`. | |
| 715 If *path* is relative and *dirfd* is the special value :data:`AT_FDCWD`, then *path* | |
| 716 is interpreted relative to the current working directory. | |
| 717 | |
| 718 Availability: Unix. | |
| 719 | |
| 720 .. versionadded:: 3.3 | |
| 721 | 707 |
| 722 | 708 |
| 723 .. function:: fchown(fd, uid, gid) | 709 .. function:: fchown(fd, uid, gid) |
| 724 | 710 |
| 725 Change the owner and group id of the file given by *fd* to the numeric *uid* | 711 Change the owner and group id of the file given by *fd* to the numeric *uid* |
| 726 and *gid*. To leave one of the ids unchanged, set it to -1. | 712 and *gid*. To leave one of the ids unchanged, set it to -1. See |
| 727 | 713 :func:`chown`. As of Python 3.3, this is equivalent to ``os.chown(fd, uid, |
| 728 Availability: Unix. | 714 gid)``. |
| 729 | 715 |
| 730 | 716 Availability: Unix. |
| 731 .. function:: fchownat(dirfd, path, uid, gid, flags=0) | |
| 732 | |
| 733 Like :func:`chown` but if *path* is relative, it is taken as relative to *dir fd*. | |
| 734 *flags* is optional and may be 0 or :data:`AT_SYMLINK_NOFOLLOW`. | |
| 735 If *path* is relative and *dirfd* is the special value :data:`AT_FDCWD`, then *path* | |
| 736 is interpreted relative to the current working directory. | |
| 737 | |
| 738 Availability: Unix. | |
| 739 | |
| 740 .. versionadded:: 3.3 | |
| 741 | 717 |
| 742 | 718 |
| 743 .. function:: fdatasync(fd) | 719 .. function:: fdatasync(fd) |
| 744 | 720 |
| 745 Force write of file with filedescriptor *fd* to disk. Does not force update o f | 721 Force write of file with filedescriptor *fd* to disk. Does not force update o f |
| 746 metadata. | 722 metadata. |
| 747 | 723 |
| 748 Availability: Unix. | 724 Availability: Unix. |
| 749 | 725 |
| 750 .. note:: | 726 .. note:: |
| 751 This function is not available on MacOS. | 727 This function is not available on MacOS. |
| 752 | |
| 753 | |
| 754 .. function:: fdlistdir(fd) | |
| 755 | |
| 756 Like :func:`listdir`, but uses a file descriptor instead and always returns | |
| 757 strings. After execution of this function, *fd* will be closed. | |
| 758 | |
| 759 Availability: Unix. | |
| 760 | |
| 761 .. versionadded:: 3.3 | |
| 762 | |
| 763 | |
| 764 .. function:: fexecve(fd, args, env) | |
| 765 | |
| 766 Execute the program specified by a file descriptor *fd* with arguments given | |
| 767 by *args* and environment given by *env*, replacing the current process. | |
| 768 *args* and *env* are given as in :func:`execve`. | |
| 769 | |
| 770 Availability: Unix. | |
| 771 | |
| 772 .. versionadded:: 3.3 | |
| 773 | 728 |
| 774 | 729 |
| 775 .. function:: fpathconf(fd, name) | 730 .. function:: fpathconf(fd, name) |
| 776 | 731 |
| 777 Return system configuration information relevant to an open file. *name* | 732 Return system configuration information relevant to an open file. *name* |
| 778 specifies the configuration value to retrieve; it may be a string which is th e | 733 specifies the configuration value to retrieve; it may be a string which is th e |
| 779 name of a defined system value; these names are specified in a number of | 734 name of a defined system value; these names are specified in a number of |
| 780 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define | 735 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define |
| 781 additional names as well. The names known to the host operating system are | 736 additional names as well. The names known to the host operating system are |
| 782 given in the ``pathconf_names`` dictionary. For configuration variables not | 737 given in the ``pathconf_names`` dictionary. For configuration variables not |
| 783 included in that mapping, passing an integer for *name* is also accepted. | 738 included in that mapping, passing an integer for *name* is also accepted. |
| 784 | 739 |
| 785 If *name* is a string and is not known, :exc:`ValueError` is raised. If a | 740 If *name* is a string and is not known, :exc:`ValueError` is raised. If a |
| 786 specific value for *name* is not supported by the host system, even if it is | 741 specific value for *name* is not supported by the host system, even if it is |
| 787 included in ``pathconf_names``, an :exc:`OSError` is raised with | 742 included in ``pathconf_names``, an :exc:`OSError` is raised with |
| 788 :const:`errno.EINVAL` for the error number. | 743 :const:`errno.EINVAL` for the error number. |
| 789 | 744 |
| 745 As of Python 3.3, this is equivalent to ``os.pathconf(fd, name)``. | |
| 746 | |
| 790 Availability: Unix. | 747 Availability: Unix. |
| 791 | 748 |
| 792 | 749 |
| 793 .. function:: fstat(fd) | 750 .. function:: fstat(fd) |
| 794 | 751 |
| 795 Return status for file descriptor *fd*, like :func:`~os.stat`. | 752 Return status for file descriptor *fd*, like :func:`~os.stat`. As of Python |
| 796 | 753 3.3, this is equivalent to ``os.stat(fd)``. |
| 797 Availability: Unix, Windows. | 754 |
| 798 | 755 Availability: Unix, Windows. |
| 799 .. function:: fstatat(dirfd, path, flags=0) | |
| 800 | |
| 801 Like :func:`stat` but if *path* is relative, it is taken as relative to *dirf d*. | |
| 802 *flags* is optional and may be 0 or :data:`AT_SYMLINK_NOFOLLOW`. | |
| 803 If *path* is relative and *dirfd* is the special value :data:`AT_FDCWD`, then *path* | |
| 804 is interpreted relative to the current working directory. | |
| 805 | |
| 806 Availability: Unix. | |
| 807 | |
| 808 .. versionadded:: 3.3 | |
| 809 | 756 |
| 810 | 757 |
| 811 .. function:: fstatvfs(fd) | 758 .. function:: fstatvfs(fd) |
| 812 | 759 |
| 813 Return information about the filesystem containing the file associated with f ile | 760 Return information about the filesystem containing the file associated with |
| 814 descriptor *fd*, like :func:`statvfs`. | 761 file descriptor *fd*, like :func:`statvfs`. As of Python 3.3, this is |
| 762 equivalent to ``os.statvfs(fd)``. | |
| 815 | 763 |
| 816 Availability: Unix. | 764 Availability: Unix. |
| 817 | 765 |
| 818 | 766 |
| 819 .. function:: fsync(fd) | 767 .. function:: fsync(fd) |
| 820 | 768 |
| 821 Force write of file with filedescriptor *fd* to disk. On Unix, this calls th e | 769 Force write of file with filedescriptor *fd* to disk. On Unix, this calls th e |
| 822 native :c:func:`fsync` function; on Windows, the MS :c:func:`_commit` functio n. | 770 native :c:func:`fsync` function; on Windows, the MS :c:func:`_commit` functio n. |
| 823 | 771 |
| 824 If you're starting with a buffered Python :term:`file object` *f*, first do | 772 If you're starting with a buffered Python :term:`file object` *f*, first do |
| 825 ``f.flush()``, and then do ``os.fsync(f.fileno())``, to ensure that all inter nal | 773 ``f.flush()``, and then do ``os.fsync(f.fileno())``, to ensure that all inter nal |
| 826 buffers associated with *f* are written to disk. | 774 buffers associated with *f* are written to disk. |
| 827 | 775 |
| 828 Availability: Unix, and Windows. | 776 Availability: Unix, Windows. |
| 829 | 777 |
| 830 | 778 |
| 831 .. function:: ftruncate(fd, length) | 779 .. function:: ftruncate(fd, length) |
| 832 | 780 |
| 833 Truncate the file corresponding to file descriptor *fd*, so that it is at mos t | 781 Truncate the file corresponding to file descriptor *fd*, so that it is at |
| 834 *length* bytes in size. | 782 most *length* bytes in size. As of Python 3.3, this is equivalent to |
| 835 | 783 ``os.truncate(fd, length)``. |
| 836 Availability: Unix. | 784 |
| 837 | 785 Availability: Unix. |
| 838 | |
| 839 .. function:: futimesat(dirfd, path, (atime, mtime)) | |
| 840 futimesat(dirfd, path, None) | |
| 841 | |
| 842 Like :func:`utime` but if *path* is relative, it is taken as relative to *dir fd*. | |
| 843 If *path* is relative and *dirfd* is the special value :data:`AT_FDCWD`, then *path* | |
| 844 is interpreted relative to the current working directory. | |
| 845 | |
| 846 Availability: Unix. | |
| 847 | |
| 848 .. versionadded:: 3.3 | |
| 849 | |
| 850 | |
| 851 .. function:: futimens(fd, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec)) | |
| 852 futimens(fd, None, None) | |
| 853 | |
| 854 Updates the timestamps of a file specified by the file descriptor *fd*, with | |
| 855 nanosecond precision. | |
| 856 The second form sets *atime* and *mtime* to the current time. | |
| 857 If *atime_nsec* or *mtime_nsec* is specified as :data:`UTIME_NOW`, the corres ponding | |
| 858 timestamp is updated to the current time. | |
| 859 If *atime_nsec* or *mtime_nsec* is specified as :data:`UTIME_OMIT`, the corre sponding | |
| 860 timestamp is not updated. | |
| 861 | |
| 862 Availability: Unix. | |
| 863 | |
| 864 .. versionadded:: 3.3 | |
| 865 | |
| 866 | |
| 867 .. data:: UTIME_NOW | |
| 868 UTIME_OMIT | |
| 869 | |
| 870 Flags used with :func:`futimens` to specify that the timestamp must be | |
| 871 updated either to the current time or not updated at all. | |
| 872 | |
| 873 Availability: Unix. | |
| 874 | |
| 875 .. versionadded:: 3.3 | |
| 876 | |
| 877 | |
| 878 .. function:: futimes(fd, (atime, mtime)) | |
| 879 futimes(fd, None) | |
| 880 | |
| 881 Set the access and modified time of the file specified by the file | |
| 882 descriptor *fd* to the given values. If the second form is used, set the | |
| 883 access and modified times to the current time. | |
| 884 | |
| 885 Availability: Unix. | |
| 886 | |
| 887 .. versionadded:: 3.3 | |
| 888 | 786 |
| 889 | 787 |
| 890 .. function:: isatty(fd) | 788 .. function:: isatty(fd) |
| 891 | 789 |
| 892 Return ``True`` if the file descriptor *fd* is open and connected to a | 790 Return ``True`` if the file descriptor *fd* is open and connected to a |
| 893 tty(-like) device, else ``False``. | 791 tty(-like) device, else ``False``. |
| 894 | 792 |
| 895 Availability: Unix. | 793 Availability: Unix. |
| 896 | |
| 897 | |
| 898 .. function:: linkat(srcfd, srcpath, dstfd, dstpath, flags=0) | |
| 899 | |
| 900 Like :func:`link` but if *srcpath* is relative, it is taken as relative to *s rcfd* | |
| 901 and if *dstpath* is relative, it is taken as relative to *dstfd*. | |
| 902 *flags* is optional and may be 0 or :data:`AT_SYMLINK_FOLLOW`. | |
| 903 If *srcpath* is relative and *srcfd* is the special value :data:`AT_FDCWD`, t hen | |
| 904 *srcpath* is interpreted relative to the current working directory. This | |
| 905 also applies for *dstpath*. | |
| 906 | |
| 907 Availability: Unix. | |
| 908 | |
| 909 .. versionadded:: 3.3 | |
| 910 | 794 |
| 911 | 795 |
| 912 .. function:: lockf(fd, cmd, len) | 796 .. function:: lockf(fd, cmd, len) |
| 913 | 797 |
| 914 Apply, test or remove a POSIX lock on an open file descriptor. | 798 Apply, test or remove a POSIX lock on an open file descriptor. |
| 915 *fd* is an open file descriptor. | 799 *fd* is an open file descriptor. |
| 916 *cmd* specifies the command to use - one of :data:`F_LOCK`, :data:`F_TLOCK`, | 800 *cmd* specifies the command to use - one of :data:`F_LOCK`, :data:`F_TLOCK`, |
| 917 :data:`F_ULOCK` or :data:`F_TEST`. | 801 :data:`F_ULOCK` or :data:`F_TEST`. |
| 918 *len* specifies the section of the file to lock. | 802 *len* specifies the section of the file to lock. |
| 919 | 803 |
| 920 Availability: Unix. | 804 Availability: Unix. |
| 921 | 805 |
| 922 .. versionadded:: 3.3 | 806 .. versionadded:: 3.3 |
| 923 | 807 |
| 924 | 808 |
| 925 .. data:: F_LOCK | 809 .. data:: F_LOCK |
| 926 F_TLOCK | 810 F_TLOCK |
| 927 F_ULOCK | 811 F_ULOCK |
| 928 F_TEST | 812 F_TEST |
| 929 | 813 |
| 930 Flags that specify what action :func:`lockf` will take. | 814 Flags that specify what action :func:`lockf` will take. |
| 931 | 815 |
| 932 Availability: Unix. | 816 Availability: Unix. |
| 933 | 817 |
| 934 .. versionadded:: 3.3 | 818 .. versionadded:: 3.3 |
| 935 | 819 |
| 820 | |
| 936 .. function:: lseek(fd, pos, how) | 821 .. function:: lseek(fd, pos, how) |
| 937 | 822 |
| 938 Set the current position of file descriptor *fd* to position *pos*, modified | 823 Set the current position of file descriptor *fd* to position *pos*, modified |
| 939 by *how*: :const:`SEEK_SET` or ``0`` to set the position relative to the | 824 by *how*: :const:`SEEK_SET` or ``0`` to set the position relative to the |
| 940 beginning of the file; :const:`SEEK_CUR` or ``1`` to set it relative to the | 825 beginning of the file; :const:`SEEK_CUR` or ``1`` to set it relative to the |
| 941 current position; :const:`os.SEEK_END` or ``2`` to set it relative to the end of | 826 current position; :const:`os.SEEK_END` or ``2`` to set it relative to the end of |
| 942 the file. | 827 the file. Return the new cursor position in bytes, starting from the beginnin g. |
| 943 | 828 |
| 944 Availability: Unix, Windows. | 829 Availability: Unix, Windows. |
| 945 | 830 |
| 946 | 831 |
| 947 .. data:: SEEK_SET | 832 .. data:: SEEK_SET |
| 948 SEEK_CUR | 833 SEEK_CUR |
| 949 SEEK_END | 834 SEEK_END |
| 950 | 835 |
| 951 Parameters to the :func:`lseek` function. Their values are 0, 1, and 2, | 836 Parameters to the :func:`lseek` function. Their values are 0, 1, and 2, |
| 952 respectively. Availability: Windows, Unix. | 837 respectively. |
| 953 | 838 |
| 954 | 839 Availability: Unix, Windows. |
| 955 .. function:: mkdirat(dirfd, path, mode=0o777) | 840 |
| 956 | 841 .. versionadded:: 3.3 |
| 957 Like :func:`mkdir` but if *path* is relative, it is taken as relative to *dir fd*. | 842 Some operating systems could support additional values, like |
| 958 If *path* is relative and *dirfd* is the special value :data:`AT_FDCWD`, then *path* | 843 :data:`os.SEEK_HOLE` or :data:`os.SEEK_DATA`. |
| 959 is interpreted relative to the current working directory. | 844 |
| 960 | 845 |
| 961 Availability: Unix. | 846 .. function:: open(file, flags, mode=0o777, *, dir_fd=None) |
| 962 | |
| 963 .. versionadded:: 3.3 | |
| 964 | |
| 965 | |
| 966 .. function:: mkfifoat(dirfd, path, mode=0o666) | |
| 967 | |
| 968 Like :func:`mkfifo` but if *path* is relative, it is taken as relative to *di rfd*. | |
| 969 If *path* is relative and *dirfd* is the special value :data:`AT_FDCWD`, then *path* | |
| 970 is interpreted relative to the current working directory. | |
| 971 | |
| 972 Availability: Unix. | |
| 973 | |
| 974 .. versionadded:: 3.3 | |
| 975 | |
| 976 | |
| 977 .. function:: mknodat(dirfd, path, mode=0o600, device=0) | |
| 978 | |
| 979 Like :func:`mknod` but if *path* is relative, it is taken as relative to *dir fd*. | |
| 980 If *path* is relative and *dirfd* is the special value :data:`AT_FDCWD`, then *path* | |
| 981 is interpreted relative to the current working directory. | |
| 982 | |
| 983 Availability: Unix. | |
| 984 | |
| 985 .. versionadded:: 3.3 | |
| 986 | |
| 987 | |
| 988 .. function:: open(file, flags[, mode]) | |
| 989 | 847 |
| 990 Open the file *file* and set various flags according to *flags* and possibly | 848 Open the file *file* and set various flags according to *flags* and possibly |
| 991 its mode according to *mode*. The default *mode* is ``0o777`` (octal), and | 849 its mode according to *mode*. When computing *mode*, the current umask value |
| 992 the current umask value is first masked out. Return the file descriptor for | 850 is first masked out. Return the file descriptor for the newly opened file. |
| 993 the newly opened file. | |
| 994 | 851 |
| 995 For a description of the flag and mode values, see the C run-time documentati on; | 852 For a description of the flag and mode values, see the C run-time documentati on; |
| 996 flag constants (like :const:`O_RDONLY` and :const:`O_WRONLY`) are defined in | 853 flag constants (like :const:`O_RDONLY` and :const:`O_WRONLY`) are defined in |
| 997 this module too (see :ref:`open-constants`). In particular, on Windows addin g | 854 this module too (see :ref:`open-constants`). In particular, on Windows addin g |
| 998 :const:`O_BINARY` is needed to open files in binary mode. | 855 :const:`O_BINARY` is needed to open files in binary mode. |
| 999 | 856 |
| 857 This function can support :ref:`paths relative to directory descriptors | |
| 858 <dir_fd>`. | |
| 859 | |
| 1000 Availability: Unix, Windows. | 860 Availability: Unix, Windows. |
| 1001 | 861 |
| 1002 .. note:: | 862 .. note:: |
| 1003 | 863 |
| 1004 This function is intended for low-level I/O. For normal usage, use the | 864 This function is intended for low-level I/O. For normal usage, use the |
| 1005 built-in function :func:`open`, which returns a :term:`file object` with | 865 built-in function :func:`open`, which returns a :term:`file object` with |
| 1006 :meth:`~file.read` and :meth:`~file.write` methods (and many more). To | 866 :meth:`~file.read` and :meth:`~file.write` methods (and many more). To |
| 1007 wrap a file descriptor in a file object, use :func:`fdopen`. | 867 wrap a file descriptor in a file object, use :func:`fdopen`. |
| 1008 | 868 |
| 1009 | 869 .. versionadded:: 3.3 |
| 1010 .. function:: openat(dirfd, path, flags, mode=0o777) | 870 The *dir_fd* argument. |
| 1011 | |
| 1012 Like :func:`open` but if *path* is relative, it is taken as relative to *dirf d*. | |
| 1013 If *path* is relative and *dirfd* is the special value :data:`AT_FDCWD`, then *path* | |
| 1014 is interpreted relative to the current working directory. | |
| 1015 | |
| 1016 Availability: Unix. | |
| 1017 | |
| 1018 .. versionadded:: 3.3 | |
| 1019 | 871 |
| 1020 | 872 |
| 1021 .. function:: openpty() | 873 .. function:: openpty() |
| 1022 | 874 |
| 1023 .. index:: module: pty | 875 .. index:: module: pty |
| 1024 | 876 |
| 1025 Open a new pseudo-terminal pair. Return a pair of file descriptors ``(master, | 877 Open a new pseudo-terminal pair. Return a pair of file descriptors ``(master, |
| 1026 slave)`` for the pty and the tty, respectively. For a (slightly) more portabl e | 878 slave)`` for the pty and the tty, respectively. For a (slightly) more portabl e |
| 1027 approach, use the :mod:`pty` module. | 879 approach, use the :mod:`pty` module. |
| 1028 | 880 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1114 | 966 |
| 1115 Read at most *n* bytes from file descriptor *fd*. Return a bytestring contain ing the | 967 Read at most *n* bytes from file descriptor *fd*. Return a bytestring contain ing the |
| 1116 bytes read. If the end of the file referred to by *fd* has been reached, an | 968 bytes read. If the end of the file referred to by *fd* has been reached, an |
| 1117 empty bytes object is returned. | 969 empty bytes object is returned. |
| 1118 | 970 |
| 1119 Availability: Unix, Windows. | 971 Availability: Unix, Windows. |
| 1120 | 972 |
| 1121 .. note:: | 973 .. note:: |
| 1122 | 974 |
| 1123 This function is intended for low-level I/O and must be applied to a file | 975 This function is intended for low-level I/O and must be applied to a file |
| 1124 descriptor as returned by :func:`os.open` or :func:`pipe`. To read a "fil e object" | 976 descriptor as returned by :func:`os.open` or :func:`pipe`. To read a |
| 1125 returned by the built-in function :func:`open` or by :func:`popen` or | 977 "file object" returned by the built-in function :func:`open` or by |
| 1126 :func:`fdopen`, or :data:`sys.stdin`, use its :meth:`~file.read` or | 978 :func:`popen` or :func:`fdopen`, or :data:`sys.stdin`, use its |
| 1127 :meth:`~file.readline` methods. | 979 :meth:`~file.read` or :meth:`~file.readline` methods. |
| 1128 | 980 |
| 1129 | 981 |
| 1130 .. function:: sendfile(out, in, offset, nbytes) | 982 .. function:: sendfile(out, in, offset, nbytes) |
| 1131 sendfile(out, in, offset, nbytes, headers=None, trailers=None, fla gs=0) | 983 sendfile(out, in, offset, nbytes, headers=None, trailers=None, fla gs=0) |
| 1132 | 984 |
| 1133 Copy *nbytes* bytes from file descriptor *in* to file descriptor *out* | 985 Copy *nbytes* bytes from file descriptor *in* to file descriptor *out* |
| 1134 starting at *offset*. | 986 starting at *offset*. |
| 1135 Return the number of bytes sent. When EOF is reached return 0. | 987 Return the number of bytes sent. When EOF is reached return 0. |
| 1136 | 988 |
| 1137 The first function notation is supported by all platforms that define | 989 The first function notation is supported by all platforms that define |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 1161 SF_SYNC | 1013 SF_SYNC |
| 1162 | 1014 |
| 1163 Parameters to the :func:`sendfile` function, if the implementation supports | 1015 Parameters to the :func:`sendfile` function, if the implementation supports |
| 1164 them. | 1016 them. |
| 1165 | 1017 |
| 1166 Availability: Unix. | 1018 Availability: Unix. |
| 1167 | 1019 |
| 1168 .. versionadded:: 3.3 | 1020 .. versionadded:: 3.3 |
| 1169 | 1021 |
| 1170 | 1022 |
| 1171 .. function:: readlinkat(dirfd, path) | |
| 1172 | |
| 1173 Like :func:`readlink` but if *path* is relative, it is taken as relative to * dirfd*. | |
| 1174 If *path* is relative and *dirfd* is the special value :data:`AT_FDCWD`, then *path* | |
| 1175 is interpreted relative to the current working directory. | |
| 1176 | |
| 1177 Availability: Unix. | |
| 1178 | |
| 1179 .. versionadded:: 3.3 | |
| 1180 | |
| 1181 | |
| 1182 .. function:: renameat(olddirfd, oldpath, newdirfd, newpath) | |
| 1183 | |
| 1184 Like :func:`rename` but if *oldpath* is relative, it is taken as relative to | |
| 1185 *olddirfd* and if *newpath* is relative, it is taken as relative to *newdirfd *. | |
| 1186 If *oldpath* is relative and *olddirfd* is the special value :data:`AT_FDCWD` , then | |
| 1187 *oldpath* is interpreted relative to the current working directory. This | |
| 1188 also applies for *newpath*. | |
| 1189 | |
| 1190 Availability: Unix. | |
| 1191 | |
| 1192 .. versionadded:: 3.3 | |
| 1193 | |
| 1194 | |
| 1195 .. function:: symlinkat(src, dstfd, dst) | |
| 1196 | |
| 1197 Like :func:`symlink` but if *dst* is relative, it is taken as relative to *ds tfd*. | |
| 1198 If *dst* is relative and *dstfd* is the special value :data:`AT_FDCWD`, then *dst* | |
| 1199 is interpreted relative to the current working directory. | |
| 1200 | |
| 1201 Availability: Unix. | |
| 1202 | |
| 1203 .. versionadded:: 3.3 | |
| 1204 | |
| 1205 | |
| 1206 .. function:: readv(fd, buffers) | 1023 .. function:: readv(fd, buffers) |
| 1207 | 1024 |
| 1208 Read from a file descriptor into a number of writable buffers. *buffers* is | 1025 Read from a file descriptor into a number of writable buffers. *buffers* is |
| 1209 an arbitrary sequence of writable buffers. Returns the total number of bytes | 1026 an arbitrary sequence of writable buffers. Returns the total number of bytes |
| 1210 read. | 1027 read. |
| 1211 | 1028 |
| 1212 Availability: Unix. | 1029 Availability: Unix. |
| 1213 | 1030 |
| 1214 .. versionadded:: 3.3 | 1031 .. versionadded:: 3.3 |
| 1215 | 1032 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 1230 Availability: Unix. | 1047 Availability: Unix. |
| 1231 | 1048 |
| 1232 | 1049 |
| 1233 .. function:: ttyname(fd) | 1050 .. function:: ttyname(fd) |
| 1234 | 1051 |
| 1235 Return a string which specifies the terminal device associated with | 1052 Return a string which specifies the terminal device associated with |
| 1236 file descriptor *fd*. If *fd* is not associated with a terminal device, an | 1053 file descriptor *fd*. If *fd* is not associated with a terminal device, an |
| 1237 exception is raised. | 1054 exception is raised. |
| 1238 | 1055 |
| 1239 Availability: Unix. | 1056 Availability: Unix. |
| 1240 | |
| 1241 | |
| 1242 .. function:: unlinkat(dirfd, path, flags=0) | |
| 1243 | |
| 1244 Like :func:`unlink` but if *path* is relative, it is taken as relative to *di rfd*. | |
| 1245 *flags* is optional and may be 0 or :data:`AT_REMOVEDIR`. If :data:`AT_REMOVE DIR` is | |
| 1246 specified, :func:`unlinkat` behaves like :func:`rmdir`. | |
| 1247 If *path* is relative and *dirfd* is the special value :data:`AT_FDCWD`, then *path* | |
| 1248 is interpreted relative to the current working directory. | |
| 1249 | |
| 1250 Availability: Unix. | |
| 1251 | |
| 1252 .. versionadded:: 3.3 | |
| 1253 | |
| 1254 | |
| 1255 .. function:: utimensat(dirfd, path, (atime_sec, atime_nsec), (mtime_sec, mtime_ nsec), flags) | |
| 1256 utimensat(dirfd, path, None, None, flags) | |
| 1257 | |
| 1258 Updates the timestamps of a file with nanosecond precision. | |
| 1259 The second form sets *atime* and *mtime* to the current time. | |
| 1260 If *atime_nsec* or *mtime_nsec* is specified as :data:`UTIME_NOW`, the corres ponding | |
| 1261 timestamp is updated to the current time. | |
| 1262 If *atime_nsec* or *mtime_nsec* is specified as :data:`UTIME_OMIT`, the corre sponding | |
| 1263 timestamp is not updated. | |
| 1264 If *path* is relative, it is taken as relative to *dirfd*. | |
| 1265 *flags* is optional and may be 0 or :data:`AT_SYMLINK_NOFOLLOW`. | |
| 1266 If *path* is relative and *dirfd* is the special value :data:`AT_FDCWD`, then *path* | |
| 1267 is interpreted relative to the current working directory. | |
| 1268 | |
| 1269 Availability: Unix. | |
| 1270 | |
| 1271 .. versionadded:: 3.3 | |
| 1272 | 1057 |
| 1273 | 1058 |
| 1274 .. function:: write(fd, str) | 1059 .. function:: write(fd, str) |
| 1275 | 1060 |
| 1276 Write the bytestring in *str* to file descriptor *fd*. Return the number of | 1061 Write the bytestring in *str* to file descriptor *fd*. Return the number of |
| 1277 bytes actually written. | 1062 bytes actually written. |
| 1278 | 1063 |
| 1279 Availability: Unix, Windows. | 1064 Availability: Unix, Windows. |
| 1280 | 1065 |
| 1281 .. note:: | 1066 .. note:: |
| 1282 | 1067 |
| 1283 This function is intended for low-level I/O and must be applied to a file | 1068 This function is intended for low-level I/O and must be applied to a file |
| 1284 descriptor as returned by :func:`os.open` or :func:`pipe`. To write a "fi le | 1069 descriptor as returned by :func:`os.open` or :func:`pipe`. To write a "fi le |
| 1285 object" returned by the built-in function :func:`open` or by :func:`popen` or | 1070 object" returned by the built-in function :func:`open` or by :func:`popen` or |
| 1286 :func:`fdopen`, or :data:`sys.stdout` or :data:`sys.stderr`, use its | 1071 :func:`fdopen`, or :data:`sys.stdout` or :data:`sys.stderr`, use its |
| 1287 :meth:`~file.write` method. | 1072 :meth:`~file.write` method. |
| 1288 | 1073 |
| 1289 | 1074 |
| 1290 .. function:: writev(fd, buffers) | 1075 .. function:: writev(fd, buffers) |
| 1291 | 1076 |
| 1292 Write the the contents of *buffers* to file descriptor *fd*, where *buffers* | 1077 Write the contents of *buffers* to file descriptor *fd*, where *buffers* |
| 1293 is an arbitrary sequence of buffers. | 1078 is an arbitrary sequence of buffers. |
| 1294 Returns the total number of bytes written. | 1079 Returns the total number of bytes written. |
| 1295 | 1080 |
| 1296 Availability: Unix. | 1081 Availability: Unix. |
| 1297 | 1082 |
| 1298 .. versionadded:: 3.3 | 1083 .. versionadded:: 3.3 |
| 1299 | 1084 |
| 1300 | 1085 |
| 1301 .. _open-constants: | 1086 .. _open-constants: |
| 1302 | 1087 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 1326 O_SYNC | 1111 O_SYNC |
| 1327 O_NDELAY | 1112 O_NDELAY |
| 1328 O_NONBLOCK | 1113 O_NONBLOCK |
| 1329 O_NOCTTY | 1114 O_NOCTTY |
| 1330 O_SHLOCK | 1115 O_SHLOCK |
| 1331 O_EXLOCK | 1116 O_EXLOCK |
| 1332 O_CLOEXEC | 1117 O_CLOEXEC |
| 1333 | 1118 |
| 1334 These constants are only available on Unix. | 1119 These constants are only available on Unix. |
| 1335 | 1120 |
| 1121 .. versionchanged:: 3.3 | |
| 1122 Add :data:`O_CLOEXEC` constant. | |
| 1336 | 1123 |
| 1337 .. data:: O_BINARY | 1124 .. data:: O_BINARY |
| 1338 O_NOINHERIT | 1125 O_NOINHERIT |
| 1339 O_SHORT_LIVED | 1126 O_SHORT_LIVED |
| 1340 O_TEMPORARY | 1127 O_TEMPORARY |
| 1341 O_RANDOM | 1128 O_RANDOM |
| 1342 O_SEQUENTIAL | 1129 O_SEQUENTIAL |
| 1343 O_TEXT | 1130 O_TEXT |
| 1344 | 1131 |
| 1345 These constants are only available on Windows. | 1132 These constants are only available on Windows. |
| 1346 | 1133 |
| 1347 | 1134 |
| 1348 .. data:: O_ASYNC | 1135 .. data:: O_ASYNC |
| 1349 O_DIRECT | 1136 O_DIRECT |
| 1350 O_DIRECTORY | 1137 O_DIRECTORY |
| 1351 O_NOFOLLOW | 1138 O_NOFOLLOW |
| 1352 O_NOATIME | 1139 O_NOATIME |
| 1353 | 1140 |
| 1354 These constants are GNU extensions and not present if they are not defined by | 1141 These constants are GNU extensions and not present if they are not defined by |
| 1355 the C library. | 1142 the C library. |
| 1356 | 1143 |
| 1357 | 1144 |
| 1145 .. data:: RTLD_LAZY | |
| 1146 RTLD_NOW | |
| 1147 RTLD_GLOBAL | |
| 1148 RTLD_LOCAL | |
| 1149 RTLD_NODELETE | |
| 1150 RTLD_NOLOAD | |
| 1151 RTLD_DEEPBIND | |
| 1152 | |
| 1153 See the Unix manual page :manpage:`dlopen(3)`. | |
| 1154 | |
| 1155 .. versionadded:: 3.3 | |
| 1156 | |
| 1157 | |
| 1158 .. _terminal-size: | |
| 1159 | |
| 1160 Querying the size of a terminal | |
| 1161 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
| 1162 | |
| 1163 .. versionadded:: 3.3 | |
| 1164 | |
| 1165 .. function:: get_terminal_size(fd=STDOUT_FILENO) | |
| 1166 | |
| 1167 Return the size of the terminal window as ``(columns, lines)``, | |
| 1168 tuple of type :class:`terminal_size`. | |
| 1169 | |
| 1170 The optional argument ``fd`` (default ``STDOUT_FILENO``, or standard | |
| 1171 output) specifies which file descriptor should be queried. | |
| 1172 | |
| 1173 If the file descriptor is not connected to a terminal, an :exc:`OSError` | |
| 1174 is thrown. | |
| 1175 | |
| 1176 :func:`shutil.get_terminal_size` is the high-level function which | |
| 1177 should normally be used, ``os.get_terminal_size`` is the low-level | |
| 1178 implementation. | |
| 1179 | |
| 1180 Availability: Unix, Windows. | |
| 1181 | |
| 1182 .. class:: terminal_size | |
| 1183 | |
| 1184 A subclass of tuple, holding ``(columns, lines)`` of the terminal window size . | |
| 1185 | |
| 1186 .. attribute:: columns | |
| 1187 | |
| 1188 Width of the terminal window in characters. | |
| 1189 | |
| 1190 .. attribute:: lines | |
| 1191 | |
| 1192 Height of the terminal window in characters. | |
| 1193 | |
| 1194 | |
| 1358 .. _os-file-dir: | 1195 .. _os-file-dir: |
| 1359 | 1196 |
| 1360 Files and Directories | 1197 Files and Directories |
| 1361 --------------------- | 1198 --------------------- |
| 1362 | 1199 |
| 1363 .. function:: access(path, mode) | 1200 On some Unix platforms, many of these functions support one or more of these |
| 1201 features: | |
| 1202 | |
| 1203 .. _path_fd: | |
| 1204 | |
| 1205 * **specifying a file descriptor:** | |
| 1206 For some functions, the *path* argument can be not only a string giving a path | |
| 1207 name, but also a file descriptor. The function will then operate on the file | |
| 1208 referred to by the descriptor. (For POSIX systems, Python will call the | |
| 1209 ``f...`` version of the function.) | |
| 1210 | |
| 1211 You can check whether or not *path* can be specified as a file descriptor on | |
| 1212 your platform using :data:`os.supports_fd`. If it is unavailable, using it | |
| 1213 will raise a :exc:`NotImplementedError`. | |
| 1214 | |
| 1215 If the function also supports *dir_fd* or *follow_symlinks* arguments, it is | |
| 1216 an error to specify one of those when supplying *path* as a file descriptor. | |
| 1217 | |
| 1218 .. _dir_fd: | |
| 1219 | |
| 1220 * **paths relative to directory descriptors:** If *dir_fd* is not ``None``, it | |
| 1221 should be a file descriptor referring to a directory, and the path to operate | |
| 1222 on should be relative; path will then be relative to that directory. If the | |
| 1223 path is absolute, *dir_fd* is ignored. (For POSIX systems, Python will call | |
| 1224 the ``...at`` or ``f...at`` version of the function.) | |
| 1225 | |
| 1226 You can check whether or not *dir_fd* is supported on your platform using | |
| 1227 :data:`os.supports_dir_fd`. If it is unavailable, using it will raise a | |
| 1228 :exc:`NotImplementedError`. | |
| 1229 | |
| 1230 .. _follow_symlinks: | |
| 1231 | |
| 1232 * **not following symlinks:** If *follow_symlinks* is | |
| 1233 ``False``, and the last element of the path to operate on is a symbolic link, | |
| 1234 the function will operate on the symbolic link itself instead of the file the | |
| 1235 link points to. (For POSIX systems, Python will call the ``l...`` version of | |
| 1236 the function.) | |
| 1237 | |
| 1238 You can check whether or not *follow_symlinks* is supported on your platform | |
| 1239 using :data:`os.supports_follow_symlinks`. If it is unavailable, using it | |
| 1240 will raise a :exc:`NotImplementedError`. | |
| 1241 | |
| 1242 | |
| 1243 | |
| 1244 .. function:: access(path, mode, *, dir_fd=None, effective_ids=False, follow_sym links=True) | |
| 1364 | 1245 |
| 1365 Use the real uid/gid to test for access to *path*. Note that most operations | 1246 Use the real uid/gid to test for access to *path*. Note that most operations |
| 1366 will use the effective uid/gid, therefore this routine can be used in a | 1247 will use the effective uid/gid, therefore this routine can be used in a |
| 1367 suid/sgid environment to test if the invoking user has the specified access t o | 1248 suid/sgid environment to test if the invoking user has the specified access t o |
| 1368 *path*. *mode* should be :const:`F_OK` to test the existence of *path*, or i t | 1249 *path*. *mode* should be :const:`F_OK` to test the existence of *path*, or i t |
| 1369 can be the inclusive OR of one or more of :const:`R_OK`, :const:`W_OK`, and | 1250 can be the inclusive OR of one or more of :const:`R_OK`, :const:`W_OK`, and |
| 1370 :const:`X_OK` to test permissions. Return :const:`True` if access is allowed , | 1251 :const:`X_OK` to test permissions. Return :const:`True` if access is allowed , |
| 1371 :const:`False` if not. See the Unix man page :manpage:`access(2)` for more | 1252 :const:`False` if not. See the Unix man page :manpage:`access(2)` for more |
| 1372 information. | 1253 information. |
| 1373 | 1254 |
| 1255 This function can support specifying :ref:`paths relative to directory | |
| 1256 descriptors <dir_fd>` and :ref:`not following symlinks <follow_symlinks>`. | |
| 1257 | |
| 1258 If *effective_ids* is ``True``, :func:`access` will perform its access | |
| 1259 checks using the effective uid/gid instead of the real uid/gid. | |
| 1260 *effective_ids* may not be supported on your platform; you can check whether | |
| 1261 or not it is available using :data:`os.supports_effective_ids`. If it is | |
| 1262 unavailable, using it will raise a :exc:`NotImplementedError`. | |
| 1263 | |
| 1374 Availability: Unix, Windows. | 1264 Availability: Unix, Windows. |
| 1375 | 1265 |
| 1376 .. note:: | 1266 .. note:: |
| 1377 | 1267 |
| 1378 Using :func:`access` to check if a user is authorized to e.g. open a file | 1268 Using :func:`access` to check if a user is authorized to e.g. open a file |
| 1379 before actually doing so using :func:`open` creates a security hole, | 1269 before actually doing so using :func:`open` creates a security hole, |
| 1380 because the user might exploit the short time interval between checking | 1270 because the user might exploit the short time interval between checking |
| 1381 and opening the file to manipulate it. It's preferable to use :term:`EAFP` | 1271 and opening the file to manipulate it. It's preferable to use :term:`EAFP` |
| 1382 techniques. For example:: | 1272 techniques. For example:: |
| 1383 | 1273 |
| 1384 if os.access("myfile", os.R_OK): | 1274 if os.access("myfile", os.R_OK): |
| 1385 with open("myfile") as fp: | 1275 with open("myfile") as fp: |
| 1386 return fp.read() | 1276 return fp.read() |
| 1387 return "some default data" | 1277 return "some default data" |
| 1388 | 1278 |
| 1389 is better written as:: | 1279 is better written as:: |
| 1390 | 1280 |
| 1391 try: | 1281 try: |
| 1392 fp = open("myfile") | 1282 fp = open("myfile") |
| 1393 except IOError as e: | 1283 except PermissionError: |
| 1394 if e.errno == errno.EACCESS: | 1284 return "some default data" |
| 1395 return "some default data" | |
| 1396 # Not a permission error. | |
| 1397 raise | |
| 1398 else: | 1285 else: |
| 1399 with fp: | 1286 with fp: |
| 1400 return fp.read() | 1287 return fp.read() |
| 1401 | 1288 |
| 1402 .. note:: | 1289 .. note:: |
| 1403 | 1290 |
| 1404 I/O operations may fail even when :func:`access` indicates that they would | 1291 I/O operations may fail even when :func:`access` indicates that they would |
| 1405 succeed, particularly for operations on network filesystems which may have | 1292 succeed, particularly for operations on network filesystems which may have |
| 1406 permissions semantics beyond the usual POSIX permission-bit model. | 1293 permissions semantics beyond the usual POSIX permission-bit model. |
| 1407 | 1294 |
| 1295 .. versionchanged:: 3.3 | |
| 1296 Added the *dir_fd*, *effective_ids*, and *follow_symlinks* parameters. | |
| 1297 | |
| 1408 | 1298 |
| 1409 .. data:: F_OK | 1299 .. data:: F_OK |
| 1410 | 1300 R_OK |
| 1411 Value to pass as the *mode* parameter of :func:`access` to test the existence of | 1301 W_OK |
| 1412 *path*. | 1302 X_OK |
| 1413 | 1303 |
| 1414 | 1304 Values to pass as the *mode* parameter of :func:`access` to test the |
| 1415 .. data:: R_OK | 1305 existence, readability, writability and executability of *path*, |
| 1416 | 1306 respectively. |
| 1417 Value to include in the *mode* parameter of :func:`access` to test the | |
| 1418 readability of *path*. | |
| 1419 | |
| 1420 | |
| 1421 .. data:: W_OK | |
| 1422 | |
| 1423 Value to include in the *mode* parameter of :func:`access` to test the | |
| 1424 writability of *path*. | |
| 1425 | |
| 1426 | |
| 1427 .. data:: X_OK | |
| 1428 | |
| 1429 Value to include in the *mode* parameter of :func:`access` to determine if | |
| 1430 *path* can be executed. | |
| 1431 | 1307 |
| 1432 | 1308 |
| 1433 .. function:: chdir(path) | 1309 .. function:: chdir(path) |
| 1434 | 1310 |
| 1435 .. index:: single: directory; changing | 1311 .. index:: single: directory; changing |
| 1436 | 1312 |
| 1437 Change the current working directory to *path*. | 1313 Change the current working directory to *path*. |
| 1438 | 1314 |
| 1439 Availability: Unix, Windows. | 1315 This function can support :ref:`specifying a file descriptor <path_fd>`. The |
| 1440 | 1316 descriptor must refer to an opened directory, not an open file. |
| 1441 | 1317 |
| 1442 .. function:: fchdir(fd) | 1318 Availability: Unix, Windows. |
| 1443 | 1319 |
| 1444 Change the current working directory to the directory represented by the file | 1320 .. versionadded:: 3.3 |
| 1445 descriptor *fd*. The descriptor must refer to an opened directory, not an op en | 1321 Added support for specifying *path* as a file descriptor |
| 1446 file. | 1322 on some platforms. |
| 1447 | 1323 |
| 1448 Availability: Unix. | 1324 |
| 1449 | 1325 .. function:: chflags(path, flags, *, follow_symlinks=True) |
| 1450 | |
| 1451 .. function:: getcwd() | |
| 1452 | |
| 1453 Return a string representing the current working directory. | |
| 1454 | |
| 1455 Availability: Unix, Windows. | |
| 1456 | |
| 1457 | |
| 1458 .. function:: getcwdb() | |
| 1459 | |
| 1460 Return a bytestring representing the current working directory. | |
| 1461 | |
| 1462 Availability: Unix, Windows. | |
| 1463 | |
| 1464 | |
| 1465 .. function:: chflags(path, flags) | |
| 1466 | 1326 |
| 1467 Set the flags of *path* to the numeric *flags*. *flags* may take a combinatio n | 1327 Set the flags of *path* to the numeric *flags*. *flags* may take a combinatio n |
| 1468 (bitwise OR) of the following values (as defined in the :mod:`stat` module): | 1328 (bitwise OR) of the following values (as defined in the :mod:`stat` module): |
| 1469 | 1329 |
| 1470 * :data:`stat.UF_NODUMP` | 1330 * :data:`stat.UF_NODUMP` |
| 1471 * :data:`stat.UF_IMMUTABLE` | 1331 * :data:`stat.UF_IMMUTABLE` |
| 1472 * :data:`stat.UF_APPEND` | 1332 * :data:`stat.UF_APPEND` |
| 1473 * :data:`stat.UF_OPAQUE` | 1333 * :data:`stat.UF_OPAQUE` |
| 1474 * :data:`stat.UF_NOUNLINK` | 1334 * :data:`stat.UF_NOUNLINK` |
| 1475 * :data:`stat.UF_COMPRESSED` | 1335 * :data:`stat.UF_COMPRESSED` |
| 1476 * :data:`stat.UF_HIDDEN` | 1336 * :data:`stat.UF_HIDDEN` |
| 1477 * :data:`stat.SF_ARCHIVED` | 1337 * :data:`stat.SF_ARCHIVED` |
| 1478 * :data:`stat.SF_IMMUTABLE` | 1338 * :data:`stat.SF_IMMUTABLE` |
| 1479 * :data:`stat.SF_APPEND` | 1339 * :data:`stat.SF_APPEND` |
| 1480 * :data:`stat.SF_NOUNLINK` | 1340 * :data:`stat.SF_NOUNLINK` |
| 1481 * :data:`stat.SF_SNAPSHOT` | 1341 * :data:`stat.SF_SNAPSHOT` |
| 1482 | 1342 |
| 1483 Availability: Unix. | 1343 This function can support :ref:`not following symlinks <follow_symlinks>`. |
| 1484 | 1344 |
| 1485 | 1345 Availability: Unix. |
| 1486 .. function:: chroot(path) | 1346 |
| 1487 | 1347 .. versionadded:: 3.3 |
| 1488 Change the root directory of the current process to *path*. Availability: | 1348 The *follow_symlinks* argument. |
| 1489 Unix. | 1349 |
| 1490 | 1350 |
| 1491 | 1351 .. function:: chmod(path, mode, *, dir_fd=None, follow_symlinks=True) |
| 1492 .. function:: chmod(path, mode) | |
| 1493 | 1352 |
| 1494 Change the mode of *path* to the numeric *mode*. *mode* may take one of the | 1353 Change the mode of *path* to the numeric *mode*. *mode* may take one of the |
| 1495 following values (as defined in the :mod:`stat` module) or bitwise ORed | 1354 following values (as defined in the :mod:`stat` module) or bitwise ORed |
| 1496 combinations of them: | 1355 combinations of them: |
| 1497 | 1356 |
| 1498 * :data:`stat.S_ISUID` | 1357 * :data:`stat.S_ISUID` |
| 1499 * :data:`stat.S_ISGID` | 1358 * :data:`stat.S_ISGID` |
| 1500 * :data:`stat.S_ENFMT` | 1359 * :data:`stat.S_ENFMT` |
| 1501 * :data:`stat.S_ISVTX` | 1360 * :data:`stat.S_ISVTX` |
| 1502 * :data:`stat.S_IREAD` | 1361 * :data:`stat.S_IREAD` |
| 1503 * :data:`stat.S_IWRITE` | 1362 * :data:`stat.S_IWRITE` |
| 1504 * :data:`stat.S_IEXEC` | 1363 * :data:`stat.S_IEXEC` |
| 1505 * :data:`stat.S_IRWXU` | 1364 * :data:`stat.S_IRWXU` |
| 1506 * :data:`stat.S_IRUSR` | 1365 * :data:`stat.S_IRUSR` |
| 1507 * :data:`stat.S_IWUSR` | 1366 * :data:`stat.S_IWUSR` |
| 1508 * :data:`stat.S_IXUSR` | 1367 * :data:`stat.S_IXUSR` |
| 1509 * :data:`stat.S_IRWXG` | 1368 * :data:`stat.S_IRWXG` |
| 1510 * :data:`stat.S_IRGRP` | 1369 * :data:`stat.S_IRGRP` |
| 1511 * :data:`stat.S_IWGRP` | 1370 * :data:`stat.S_IWGRP` |
| 1512 * :data:`stat.S_IXGRP` | 1371 * :data:`stat.S_IXGRP` |
| 1513 * :data:`stat.S_IRWXO` | 1372 * :data:`stat.S_IRWXO` |
| 1514 * :data:`stat.S_IROTH` | 1373 * :data:`stat.S_IROTH` |
| 1515 * :data:`stat.S_IWOTH` | 1374 * :data:`stat.S_IWOTH` |
| 1516 * :data:`stat.S_IXOTH` | 1375 * :data:`stat.S_IXOTH` |
| 1517 | 1376 |
| 1377 This function can support :ref:`specifying a file descriptor <path_fd>`, | |
| 1378 :ref:`paths relative to directory descriptors <dir_fd>` and :ref:`not | |
| 1379 following symlinks <follow_symlinks>`. | |
| 1380 | |
| 1518 Availability: Unix, Windows. | 1381 Availability: Unix, Windows. |
| 1519 | 1382 |
| 1520 .. note:: | 1383 .. note:: |
| 1521 | 1384 |
| 1522 Although Windows supports :func:`chmod`, you can only set the file's read -only | 1385 Although Windows supports :func:`chmod`, you can only set the file's |
| 1523 flag with it (via the ``stat.S_IWRITE`` and ``stat.S_IREAD`` | 1386 read-only flag with it (via the ``stat.S_IWRITE`` and ``stat.S_IREAD`` |
| 1524 constants or a corresponding integer value). All other bits are | 1387 constants or a corresponding integer value). All other bits are ignored. |
| 1525 ignored. | 1388 |
| 1526 | 1389 .. versionadded:: 3.3 |
| 1527 | 1390 Added support for specifying *path* as an open file descriptor, |
| 1528 .. function:: chown(path, uid, gid) | 1391 and the *dir_fd* and *follow_symlinks* arguments. |
| 1529 | 1392 |
| 1530 Change the owner and group id of *path* to the numeric *uid* and *gid*. To le ave | 1393 |
| 1531 one of the ids unchanged, set it to -1. | 1394 .. function:: chown(path, uid, gid, *, dir_fd=None, follow_symlinks=True) |
| 1532 | 1395 |
| 1533 Availability: Unix. | 1396 Change the owner and group id of *path* to the numeric *uid* and *gid*. To |
| 1397 leave one of the ids unchanged, set it to -1. | |
| 1398 | |
| 1399 This function can support :ref:`specifying a file descriptor <path_fd>`, | |
| 1400 :ref:`paths relative to directory descriptors <dir_fd>` and :ref:`not | |
| 1401 following symlinks <follow_symlinks>`. | |
| 1402 | |
| 1403 See :func:`shutil.chown` for a higher-level function that accepts names in | |
| 1404 addition to numeric ids. | |
| 1405 | |
| 1406 Availability: Unix. | |
| 1407 | |
| 1408 .. versionadded:: 3.3 | |
| 1409 Added support for specifying an open file descriptor for *path*, | |
| 1410 and the *dir_fd* and *follow_symlinks* arguments. | |
| 1411 | |
| 1412 | |
| 1413 .. function:: chroot(path) | |
| 1414 | |
| 1415 Change the root directory of the current process to *path*. | |
| 1416 | |
| 1417 Availability: Unix. | |
| 1418 | |
| 1419 | |
| 1420 .. function:: fchdir(fd) | |
| 1421 | |
| 1422 Change the current working directory to the directory represented by the file | |
| 1423 descriptor *fd*. The descriptor must refer to an opened directory, not an | |
| 1424 open file. As of Python 3.3, this is equivalent to ``os.chdir(fd)``. | |
| 1425 | |
| 1426 Availability: Unix. | |
| 1427 | |
| 1428 | |
| 1429 .. function:: getcwd() | |
| 1430 | |
| 1431 Return a string representing the current working directory. | |
| 1432 | |
| 1433 Availability: Unix, Windows. | |
| 1434 | |
| 1435 | |
| 1436 .. function:: getcwdb() | |
| 1437 | |
| 1438 Return a bytestring representing the current working directory. | |
| 1439 | |
| 1440 Availability: Unix, Windows. | |
| 1534 | 1441 |
| 1535 | 1442 |
| 1536 .. function:: lchflags(path, flags) | 1443 .. function:: lchflags(path, flags) |
| 1537 | 1444 |
| 1538 Set the flags of *path* to the numeric *flags*, like :func:`chflags`, but do not | 1445 Set the flags of *path* to the numeric *flags*, like :func:`chflags`, but do |
| 1539 follow symbolic links. | 1446 not follow symbolic links. As of Python 3.3, this is equivalent to |
| 1447 ``os.chflags(path, flags, follow_symlinks=False)``. | |
| 1540 | 1448 |
| 1541 Availability: Unix. | 1449 Availability: Unix. |
| 1542 | 1450 |
| 1543 | 1451 |
| 1544 .. function:: lchmod(path, mode) | 1452 .. function:: lchmod(path, mode) |
| 1545 | 1453 |
| 1546 Change the mode of *path* to the numeric *mode*. If path is a symlink, this | 1454 Change the mode of *path* to the numeric *mode*. If path is a symlink, this |
| 1547 affects the symlink rather than the target. See the docs for :func:`chmod` | 1455 affects the symlink rather than the target. See the docs for :func:`chmod` |
| 1548 for possible values of *mode*. | 1456 for possible values of *mode*. As of Python 3.3, this is equivalent to |
| 1457 ``os.chmod(path, mode, follow_symlinks=False)``. | |
| 1549 | 1458 |
| 1550 Availability: Unix. | 1459 Availability: Unix. |
| 1551 | 1460 |
| 1552 | 1461 |
| 1553 .. function:: lchown(path, uid, gid) | 1462 .. function:: lchown(path, uid, gid) |
| 1554 | 1463 |
| 1555 Change the owner and group id of *path* to the numeric *uid* and *gid*. This | 1464 Change the owner and group id of *path* to the numeric *uid* and *gid*. This |
| 1556 function will not follow symbolic links. | 1465 function will not follow symbolic links. As of Python 3.3, this is equivalen t |
| 1557 | 1466 to ``os.chown(path, uid, gid, follow_symlinks=False)``. |
| 1558 Availability: Unix. | 1467 |
| 1559 | 1468 Availability: Unix. |
| 1560 | 1469 |
| 1561 .. function:: link(source, link_name) | 1470 |
| 1562 | 1471 .. function:: link(src, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlink s=True) |
| 1563 Create a hard link pointing to *source* named *link_name*. | 1472 |
| 1473 Create a hard link pointing to *src* named *dst*. | |
| 1474 | |
| 1475 This function can support specifying *src_dir_fd* and/or *dst_dir_fd* to | |
| 1476 supply :ref:`paths relative to directory descriptors <dir_fd>`, and :ref:`not | |
| 1477 following symlinks <follow_symlinks>`. | |
| 1564 | 1478 |
| 1565 Availability: Unix, Windows. | 1479 Availability: Unix, Windows. |
| 1566 | 1480 |
| 1567 .. versionchanged:: 3.2 | 1481 .. versionchanged:: 3.2 |
| 1568 Added Windows support. | 1482 Added Windows support. |
| 1569 | 1483 |
| 1484 .. versionadded:: 3.3 | |
| 1485 Added the *src_dir_fd*, *dst_dir_fd*, and *follow_symlinks* arguments. | |
| 1486 | |
| 1570 | 1487 |
| 1571 .. function:: listdir(path='.') | 1488 .. function:: listdir(path='.') |
| 1572 | 1489 |
| 1573 Return a list containing the names of the entries in the directory given by | 1490 Return a list containing the names of the entries in the directory given by |
| 1574 *path* (default: ``'.'``). The list is in arbitrary order. It does not incl ude the special | 1491 *path*. The list is in arbitrary order, and does not include the special |
| 1575 entries ``'.'`` and ``'..'`` even if they are present in the directory. | 1492 entries ``'.'`` and ``'..'`` even if they are present in the directory. |
| 1576 | 1493 |
| 1577 This function can be called with a bytes or string argument, and returns | 1494 *path* may be either of type ``str`` or of type ``bytes``. If *path* |
| 1578 filenames of the same datatype. | 1495 is of type ``bytes``, the filenames returned will also be of type ``bytes``; |
| 1496 in all other circumstances, they will be of type ``str``. | |
| 1497 | |
| 1498 This function can also support :ref:`specifying a file descriptor | |
| 1499 <path_fd>`; the file descriptor must refer to a directory. | |
| 1500 | |
| 1501 .. note:: | |
| 1502 To encode ``str`` filenames to ``bytes``, use :func:`~os.fsencode`. | |
| 1579 | 1503 |
| 1580 Availability: Unix, Windows. | 1504 Availability: Unix, Windows. |
| 1581 | 1505 |
| 1582 .. versionchanged:: 3.2 | 1506 .. versionchanged:: 3.2 |
| 1583 The *path* parameter became optional. | 1507 The *path* parameter became optional. |
| 1584 | 1508 |
| 1585 .. function:: lstat(path) | 1509 .. versionadded:: 3.3 |
| 1510 Added support for specifying an open file descriptor for *path*. | |
| 1511 | |
| 1512 | |
| 1513 .. function:: lstat(path, *, dir_fd=None) | |
| 1586 | 1514 |
| 1587 Perform the equivalent of an :c:func:`lstat` system call on the given path. | 1515 Perform the equivalent of an :c:func:`lstat` system call on the given path. |
| 1588 Similar to :func:`~os.stat`, but does not follow symbolic links. On | 1516 Similar to :func:`~os.stat`, but does not follow symbolic links. On |
| 1589 platforms that do not support symbolic links, this is an alias for | 1517 platforms that do not support symbolic links, this is an alias for |
| 1590 :func:`~os.stat`. | 1518 :func:`~os.stat`. As of Python 3.3, this is equivalent to ``os.stat(path, |
| 1519 dir_fd=dir_fd, follow_symlinks=False)``. | |
| 1520 | |
| 1521 This function can also support :ref:`paths relative to directory descriptors | |
| 1522 <dir_fd>`. | |
| 1591 | 1523 |
| 1592 .. versionchanged:: 3.2 | 1524 .. versionchanged:: 3.2 |
| 1593 Added support for Windows 6.0 (Vista) symbolic links. | 1525 Added support for Windows 6.0 (Vista) symbolic links. |
| 1594 | 1526 |
| 1595 | 1527 .. versionchanged:: 3.3 |
| 1596 .. function:: lutimes(path, (atime, mtime)) | 1528 Added the *dir_fd* parameter. |
| 1597 lutimes(path, None) | 1529 |
| 1598 | 1530 |
| 1599 Like :func:`utime`, but if *path* is a symbolic link, it is not | 1531 .. function:: mkdir(path, mode=0o777, *, dir_fd=None) |
| 1600 dereferenced. | 1532 |
| 1601 | 1533 Create a directory named *path* with numeric mode *mode*. |
| 1602 Availability: Unix. | 1534 |
| 1603 | 1535 On some systems, *mode* is ignored. Where it is used, the current umask |
| 1604 .. versionadded:: 3.3 | 1536 value is first masked out. If the directory already exists, :exc:`OSError` |
| 1605 | 1537 is raised. |
| 1606 | 1538 |
| 1607 .. function:: mkfifo(path[, mode]) | 1539 This function can also support :ref:`paths relative to directory descriptors |
| 1608 | 1540 <dir_fd>`. |
| 1609 Create a FIFO (a named pipe) named *path* with numeric mode *mode*. The | |
| 1610 default *mode* is ``0o666`` (octal). The current umask value is first masked | |
| 1611 out from the mode. | |
| 1612 | |
| 1613 FIFOs are pipes that can be accessed like regular files. FIFOs exist until t hey | |
| 1614 are deleted (for example with :func:`os.unlink`). Generally, FIFOs are used a s | |
| 1615 rendezvous between "client" and "server" type processes: the server opens the | |
| 1616 FIFO for reading, and the client opens it for writing. Note that :func:`mkfi fo` | |
| 1617 doesn't open the FIFO --- it just creates the rendezvous point. | |
| 1618 | |
| 1619 Availability: Unix. | |
| 1620 | |
| 1621 | |
| 1622 .. function:: mknod(filename[, mode=0o600[, device]]) | |
| 1623 | |
| 1624 Create a filesystem node (file, device special file or named pipe) named | |
| 1625 *filename*. *mode* specifies both the permissions to use and the type of node | |
| 1626 to be created, being combined (bitwise OR) with one of ``stat.S_IFREG``, | |
| 1627 ``stat.S_IFCHR``, ``stat.S_IFBLK``, and ``stat.S_IFIFO`` (those constants are | |
| 1628 available in :mod:`stat`). For ``stat.S_IFCHR`` and ``stat.S_IFBLK``, | |
| 1629 *device* defines the newly created device special file (probably using | |
| 1630 :func:`os.makedev`), otherwise it is ignored. | |
| 1631 | |
| 1632 | |
| 1633 .. function:: major(device) | |
| 1634 | |
| 1635 Extract the device major number from a raw device number (usually the | |
| 1636 :attr:`st_dev` or :attr:`st_rdev` field from :c:type:`stat`). | |
| 1637 | |
| 1638 | |
| 1639 .. function:: minor(device) | |
| 1640 | |
| 1641 Extract the device minor number from a raw device number (usually the | |
| 1642 :attr:`st_dev` or :attr:`st_rdev` field from :c:type:`stat`). | |
| 1643 | |
| 1644 | |
| 1645 .. function:: makedev(major, minor) | |
| 1646 | |
| 1647 Compose a raw device number from the major and minor device numbers. | |
| 1648 | |
| 1649 | |
| 1650 .. function:: mkdir(path[, mode]) | |
| 1651 | |
| 1652 Create a directory named *path* with numeric mode *mode*. The default *mode* | |
| 1653 is ``0o777`` (octal). On some systems, *mode* is ignored. Where it is used, | |
| 1654 the current umask value is first masked out. If the directory already | |
| 1655 exists, :exc:`OSError` is raised. | |
| 1656 | 1541 |
| 1657 It is also possible to create temporary directories; see the | 1542 It is also possible to create temporary directories; see the |
| 1658 :mod:`tempfile` module's :func:`tempfile.mkdtemp` function. | 1543 :mod:`tempfile` module's :func:`tempfile.mkdtemp` function. |
| 1659 | 1544 |
| 1660 Availability: Unix, Windows. | 1545 Availability: Unix, Windows. |
| 1546 | |
| 1547 .. versionadded:: 3.3 | |
| 1548 The *dir_fd* argument. | |
| 1661 | 1549 |
| 1662 | 1550 |
| 1663 .. function:: makedirs(path, mode=0o777, exist_ok=False) | 1551 .. function:: makedirs(path, mode=0o777, exist_ok=False) |
| 1664 | 1552 |
| 1665 .. index:: | 1553 .. index:: |
| 1666 single: directory; creating | 1554 single: directory; creating |
| 1667 single: UNC paths; and os.makedirs() | 1555 single: UNC paths; and os.makedirs() |
| 1668 | 1556 |
| 1669 Recursive directory creation function. Like :func:`mkdir`, but makes all | 1557 Recursive directory creation function. Like :func:`mkdir`, but makes all |
| 1670 intermediate-level directories needed to contain the leaf directory. If | 1558 intermediate-level directories needed to contain the leaf directory. If |
| 1671 the target directory with the same mode as specified already exists, | 1559 the target directory with the same mode as specified already exists, |
| 1672 raises an :exc:`OSError` exception if *exist_ok* is False, otherwise no | 1560 raises an :exc:`OSError` exception if *exist_ok* is False, otherwise no |
| 1673 exception is raised. If the directory cannot be created in other cases, | 1561 exception is raised. If the directory cannot be created in other cases, |
| 1674 raises an :exc:`OSError` exception. The default *mode* is ``0o777`` (octal). | 1562 raises an :exc:`OSError` exception. The default *mode* is ``0o777`` (octal). |
| 1675 On some systems, *mode* is ignored. Where it is used, the current umask | 1563 On some systems, *mode* is ignored. Where it is used, the current umask |
| 1676 value is first masked out. | 1564 value is first masked out. |
| 1677 | 1565 |
| 1678 .. note:: | 1566 .. note:: |
| 1679 | 1567 |
| 1680 :func:`makedirs` will become confused if the path elements to create | 1568 :func:`makedirs` will become confused if the path elements to create |
| 1681 include :data:`pardir`. | 1569 include :data:`pardir`. |
| 1682 | 1570 |
| 1683 This function handles UNC paths correctly. | 1571 This function handles UNC paths correctly. |
| 1684 | 1572 |
| 1685 .. versionadded:: 3.2 | 1573 .. versionadded:: 3.2 |
| 1686 The *exist_ok* parameter. | 1574 The *exist_ok* parameter. |
| 1687 | 1575 |
| 1688 | 1576 |
| 1577 .. function:: mkfifo(path, mode=0o666, *, dir_fd=None) | |
| 1578 | |
| 1579 Create a FIFO (a named pipe) named *path* with numeric mode *mode*. | |
| 1580 The current umask value is first masked out from the mode. | |
| 1581 | |
| 1582 This function can also support :ref:`paths relative to directory descriptors | |
| 1583 <dir_fd>`. | |
| 1584 | |
| 1585 FIFOs are pipes that can be accessed like regular files. FIFOs exist until t hey | |
| 1586 are deleted (for example with :func:`os.unlink`). Generally, FIFOs are used a s | |
| 1587 rendezvous between "client" and "server" type processes: the server opens the | |
| 1588 FIFO for reading, and the client opens it for writing. Note that :func:`mkfi fo` | |
| 1589 doesn't open the FIFO --- it just creates the rendezvous point. | |
| 1590 | |
| 1591 Availability: Unix. | |
| 1592 | |
| 1593 .. versionadded:: 3.3 | |
| 1594 The *dir_fd* argument. | |
| 1595 | |
| 1596 | |
| 1597 .. function:: mknod(filename, mode=0o600, device=0, *, dir_fd=None) | |
| 1598 | |
| 1599 Create a filesystem node (file, device special file or named pipe) named | |
| 1600 *filename*. *mode* specifies both the permissions to use and the type of node | |
| 1601 to be created, being combined (bitwise OR) with one of ``stat.S_IFREG``, | |
| 1602 ``stat.S_IFCHR``, ``stat.S_IFBLK``, and ``stat.S_IFIFO`` (those constants are | |
| 1603 available in :mod:`stat`). For ``stat.S_IFCHR`` and ``stat.S_IFBLK``, | |
| 1604 *device* defines the newly created device special file (probably using | |
| 1605 :func:`os.makedev`), otherwise it is ignored. | |
| 1606 | |
| 1607 This function can also support :ref:`paths relative to directory descriptors | |
| 1608 <dir_fd>`. | |
| 1609 | |
| 1610 .. versionadded:: 3.3 | |
| 1611 The *dir_fd* argument. | |
| 1612 | |
| 1613 | |
| 1614 .. function:: major(device) | |
| 1615 | |
| 1616 Extract the device major number from a raw device number (usually the | |
| 1617 :attr:`st_dev` or :attr:`st_rdev` field from :c:type:`stat`). | |
| 1618 | |
| 1619 | |
| 1620 .. function:: minor(device) | |
| 1621 | |
| 1622 Extract the device minor number from a raw device number (usually the | |
| 1623 :attr:`st_dev` or :attr:`st_rdev` field from :c:type:`stat`). | |
| 1624 | |
| 1625 | |
| 1626 .. function:: makedev(major, minor) | |
| 1627 | |
| 1628 Compose a raw device number from the major and minor device numbers. | |
| 1629 | |
| 1630 | |
| 1689 .. function:: pathconf(path, name) | 1631 .. function:: pathconf(path, name) |
| 1690 | 1632 |
| 1691 Return system configuration information relevant to a named file. *name* | 1633 Return system configuration information relevant to a named file. *name* |
| 1692 specifies the configuration value to retrieve; it may be a string which is th e | 1634 specifies the configuration value to retrieve; it may be a string which is th e |
| 1693 name of a defined system value; these names are specified in a number of | 1635 name of a defined system value; these names are specified in a number of |
| 1694 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define | 1636 standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define |
| 1695 additional names as well. The names known to the host operating system are | 1637 additional names as well. The names known to the host operating system are |
| 1696 given in the ``pathconf_names`` dictionary. For configuration variables not | 1638 given in the ``pathconf_names`` dictionary. For configuration variables not |
| 1697 included in that mapping, passing an integer for *name* is also accepted. | 1639 included in that mapping, passing an integer for *name* is also accepted. |
| 1698 | 1640 |
| 1699 If *name* is a string and is not known, :exc:`ValueError` is raised. If a | 1641 If *name* is a string and is not known, :exc:`ValueError` is raised. If a |
| 1700 specific value for *name* is not supported by the host system, even if it is | 1642 specific value for *name* is not supported by the host system, even if it is |
| 1701 included in ``pathconf_names``, an :exc:`OSError` is raised with | 1643 included in ``pathconf_names``, an :exc:`OSError` is raised with |
| 1702 :const:`errno.EINVAL` for the error number. | 1644 :const:`errno.EINVAL` for the error number. |
| 1703 | 1645 |
| 1646 This function can support :ref:`specifying a file descriptor | |
| 1647 <path_fd>`. | |
| 1648 | |
| 1704 Availability: Unix. | 1649 Availability: Unix. |
| 1705 | 1650 |
| 1706 | 1651 |
| 1707 .. data:: pathconf_names | 1652 .. data:: pathconf_names |
| 1708 | 1653 |
| 1709 Dictionary mapping names accepted by :func:`pathconf` and :func:`fpathconf` t o | 1654 Dictionary mapping names accepted by :func:`pathconf` and :func:`fpathconf` t o |
| 1710 the integer values defined for those names by the host operating system. Thi s | 1655 the integer values defined for those names by the host operating system. Thi s |
| 1711 can be used to determine the set of names known to the system. Availability: | 1656 can be used to determine the set of names known to the system. |
| 1712 Unix. | 1657 |
| 1713 | 1658 Availability: Unix. |
| 1714 | 1659 |
| 1715 .. function:: readlink(path) | 1660 |
| 1661 .. function:: readlink(path, *, dir_fd=None) | |
| 1716 | 1662 |
| 1717 Return a string representing the path to which the symbolic link points. The | 1663 Return a string representing the path to which the symbolic link points. The |
| 1718 result may be either an absolute or relative pathname; if it is relative, it may | 1664 result may be either an absolute or relative pathname; if it is relative, it |
| 1719 be converted to an absolute pathname using ``os.path.join(os.path.dirname(pat h), | 1665 may be converted to an absolute pathname using |
| 1720 result)``. | 1666 ``os.path.join(os.path.dirname(path), result)``. |
| 1721 | 1667 |
| 1722 If the *path* is a string object, the result will also be a string object, | 1668 If the *path* is a string object, the result will also be a string object, |
| 1723 and the call may raise an UnicodeDecodeError. If the *path* is a bytes | 1669 and the call may raise an UnicodeDecodeError. If the *path* is a bytes |
| 1724 object, the result will be a bytes object. | 1670 object, the result will be a bytes object. |
| 1725 | 1671 |
| 1672 This function can also support :ref:`paths relative to directory descriptors | |
| 1673 <dir_fd>`. | |
| 1674 | |
| 1726 Availability: Unix, Windows | 1675 Availability: Unix, Windows |
| 1727 | 1676 |
| 1728 .. versionchanged:: 3.2 | 1677 .. versionchanged:: 3.2 |
| 1729 Added support for Windows 6.0 (Vista) symbolic links. | 1678 Added support for Windows 6.0 (Vista) symbolic links. |
| 1730 | 1679 |
| 1731 | 1680 .. versionadded:: 3.3 |
| 1732 .. function:: remove(path) | 1681 The *dir_fd* argument. |
| 1682 | |
| 1683 | |
| 1684 .. function:: remove(path, *, dir_fd=None) | |
| 1733 | 1685 |
| 1734 Remove (delete) the file *path*. If *path* is a directory, :exc:`OSError` is | 1686 Remove (delete) the file *path*. If *path* is a directory, :exc:`OSError` is |
| 1735 raised; see :func:`rmdir` below to remove a directory. This is identical to | 1687 raised. Use :func:`rmdir` to remove directories. |
| 1736 the :func:`unlink` function documented below. On Windows, attempting to | 1688 |
| 1737 remove a file that is in use causes an exception to be raised; on Unix, the | 1689 This function can support :ref:`paths relative to directory descriptors |
| 1738 directory entry is removed but the storage allocated to the file is not made | 1690 <dir_fd>`. |
| 1739 available until the original file is no longer in use. | 1691 |
| 1740 | 1692 On Windows, attempting to remove a file that is in use causes an exception to |
| 1741 Availability: Unix, Windows. | 1693 be raised; on Unix, the directory entry is removed but the storage allocated |
| 1694 to the file is not made available until the original file is no longer in use . | |
| 1695 | |
| 1696 This function is identical to :func:`unlink`. | |
| 1697 | |
| 1698 Availability: Unix, Windows. | |
| 1699 | |
| 1700 .. versionadded:: 3.3 | |
| 1701 The *dir_fd* argument. | |
| 1742 | 1702 |
| 1743 | 1703 |
| 1744 .. function:: removedirs(path) | 1704 .. function:: removedirs(path) |
| 1745 | 1705 |
| 1746 .. index:: single: directory; deleting | 1706 .. index:: single: directory; deleting |
| 1747 | 1707 |
| 1748 Remove directories recursively. Works like :func:`rmdir` except that, if the | 1708 Remove directories recursively. Works like :func:`rmdir` except that, if the |
| 1749 leaf directory is successfully removed, :func:`removedirs` tries to | 1709 leaf directory is successfully removed, :func:`removedirs` tries to |
| 1750 successively remove every parent directory mentioned in *path* until an erro r | 1710 successively remove every parent directory mentioned in *path* until an erro r |
| 1751 is raised (which is ignored, because it generally means that a parent directo ry | 1711 is raised (which is ignored, because it generally means that a parent directo ry |
| 1752 is not empty). For example, ``os.removedirs('foo/bar/baz')`` will first remov e | 1712 is not empty). For example, ``os.removedirs('foo/bar/baz')`` will first remov e |
| 1753 the directory ``'foo/bar/baz'``, and then remove ``'foo/bar'`` and ``'foo'`` if | 1713 the directory ``'foo/bar/baz'``, and then remove ``'foo/bar'`` and ``'foo'`` if |
| 1754 they are empty. Raises :exc:`OSError` if the leaf directory could not be | 1714 they are empty. Raises :exc:`OSError` if the leaf directory could not be |
| 1755 successfully removed. | 1715 successfully removed. |
| 1756 | 1716 |
| 1757 | 1717 |
| 1758 .. function:: rename(src, dst) | 1718 .. function:: rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None) |
| 1759 | 1719 |
| 1760 Rename the file or directory *src* to *dst*. If *dst* is a directory, | 1720 Rename the file or directory *src* to *dst*. If *dst* is a directory, |
| 1761 :exc:`OSError` will be raised. On Unix, if *dst* exists and is a file, it wi ll | 1721 :exc:`OSError` will be raised. On Unix, if *dst* exists and is a file, it wi ll |
| 1762 be replaced silently if the user has permission. The operation may fail on s ome | 1722 be replaced silently if the user has permission. The operation may fail on s ome |
| 1763 Unix flavors if *src* and *dst* are on different filesystems. If successful, | 1723 Unix flavors if *src* and *dst* are on different filesystems. If successful, |
| 1764 the renaming will be an atomic operation (this is a POSIX requirement). On | 1724 the renaming will be an atomic operation (this is a POSIX requirement). On |
| 1765 Windows, if *dst* already exists, :exc:`OSError` will be raised even if it is a | 1725 Windows, if *dst* already exists, :exc:`OSError` will be raised even if it is a |
| 1766 file; there may be no way to implement an atomic rename when *dst* names an | 1726 file. |
| 1767 existing file. | 1727 |
| 1768 | 1728 This function can support specifying *src_dir_fd* and/or *dst_dir_fd* to |
| 1769 Availability: Unix, Windows. | 1729 supply :ref:`paths relative to directory descriptors <dir_fd>`. |
| 1730 | |
| 1731 If you want cross-platform overwriting of the destination, use :func:`replace `. | |
| 1732 | |
| 1733 Availability: Unix, Windows. | |
| 1734 | |
| 1735 .. versionadded:: 3.3 | |
| 1736 The *src_dir_fd* and *dst_dir_fd* arguments. | |
| 1770 | 1737 |
| 1771 | 1738 |
| 1772 .. function:: renames(old, new) | 1739 .. function:: renames(old, new) |
| 1773 | 1740 |
| 1774 Recursive directory or file renaming function. Works like :func:`rename`, exc ept | 1741 Recursive directory or file renaming function. Works like :func:`rename`, exc ept |
| 1775 creation of any intermediate directories needed to make the new pathname good is | 1742 creation of any intermediate directories needed to make the new pathname good is |
| 1776 attempted first. After the rename, directories corresponding to rightmost pat h | 1743 attempted first. After the rename, directories corresponding to rightmost pat h |
| 1777 segments of the old name will be pruned away using :func:`removedirs`. | 1744 segments of the old name will be pruned away using :func:`removedirs`. |
| 1778 | 1745 |
| 1779 .. note:: | 1746 .. note:: |
| 1780 | 1747 |
| 1781 This function can fail with the new directory structure made if you lack | 1748 This function can fail with the new directory structure made if you lack |
| 1782 permissions needed to remove the leaf directory or file. | 1749 permissions needed to remove the leaf directory or file. |
| 1783 | 1750 |
| 1784 | 1751 |
| 1785 .. function:: rmdir(path) | 1752 .. function:: replace(src, dst, *, src_dir_fd=None, dst_dir_fd=None) |
| 1753 | |
| 1754 Rename the file or directory *src* to *dst*. If *dst* is a directory, | |
| 1755 :exc:`OSError` will be raised. If *dst* exists and is a file, it will | |
| 1756 be replaced silently if the user has permission. The operation may fail | |
| 1757 if *src* and *dst* are on different filesystems. If successful, | |
| 1758 the renaming will be an atomic operation (this is a POSIX requirement). | |
| 1759 | |
| 1760 This function can support specifying *src_dir_fd* and/or *dst_dir_fd* to | |
| 1761 supply :ref:`paths relative to directory descriptors <dir_fd>`. | |
| 1762 | |
| 1763 Availability: Unix, Windows. | |
| 1764 | |
| 1765 .. versionadded:: 3.3 | |
| 1766 | |
| 1767 | |
| 1768 .. function:: rmdir(path, *, dir_fd=None) | |
| 1786 | 1769 |
| 1787 Remove (delete) the directory *path*. Only works when the directory is | 1770 Remove (delete) the directory *path*. Only works when the directory is |
| 1788 empty, otherwise, :exc:`OSError` is raised. In order to remove whole | 1771 empty, otherwise, :exc:`OSError` is raised. In order to remove whole |
| 1789 directory trees, :func:`shutil.rmtree` can be used. | 1772 directory trees, :func:`shutil.rmtree` can be used. |
| 1790 | 1773 |
| 1791 Availability: Unix, Windows. | 1774 This function can support :ref:`paths relative to directory descriptors |
| 1792 | 1775 <dir_fd>`. |
| 1793 | 1776 |
| 1794 .. function:: stat(path) | 1777 Availability: Unix, Windows. |
| 1778 | |
| 1779 .. versionadded:: 3.3 | |
| 1780 The *dir_fd* parameter. | |
| 1781 | |
| 1782 | |
| 1783 .. function:: stat(path, *, dir_fd=None, follow_symlinks=True) | |
| 1795 | 1784 |
| 1796 Perform the equivalent of a :c:func:`stat` system call on the given path. | 1785 Perform the equivalent of a :c:func:`stat` system call on the given path. |
| 1797 (This function follows symlinks; to stat a symlink use :func:`lstat`.) | 1786 *path* may be specified as either a string or as an open file descriptor. |
| 1798 | 1787 (This function normally follows symlinks; to stat a symlink add the argument |
| 1799 The return value is an object whose attributes correspond to the members | 1788 ``follow_symlinks=False``, or use :func:`lstat`.) |
| 1800 of the :c:type:`stat` structure, namely: | 1789 |
| 1790 The return value is an object whose attributes correspond roughly | |
| 1791 to the members of the :c:type:`stat` structure, namely: | |
| 1801 | 1792 |
| 1802 * :attr:`st_mode` - protection bits, | 1793 * :attr:`st_mode` - protection bits, |
| 1803 * :attr:`st_ino` - inode number, | 1794 * :attr:`st_ino` - inode number, |
| 1804 * :attr:`st_dev` - device, | 1795 * :attr:`st_dev` - device, |
| 1805 * :attr:`st_nlink` - number of hard links, | 1796 * :attr:`st_nlink` - number of hard links, |
| 1806 * :attr:`st_uid` - user id of owner, | 1797 * :attr:`st_uid` - user id of owner, |
| 1807 * :attr:`st_gid` - group id of owner, | 1798 * :attr:`st_gid` - group id of owner, |
| 1808 * :attr:`st_size` - size of file, in bytes, | 1799 * :attr:`st_size` - size of file, in bytes, |
| 1809 * :attr:`st_atime` - time of most recent access, | 1800 * :attr:`st_atime` - time of most recent access expressed in seconds, |
| 1810 * :attr:`st_mtime` - time of most recent content modification, | 1801 * :attr:`st_mtime` - time of most recent content modification |
| 1811 * :attr:`st_ctime` - platform dependent; time of most recent metadata change on | 1802 expressed in seconds, |
| 1812 Unix, or the time of creation on Windows) | 1803 * :attr:`st_ctime` - platform dependent; time of most recent metadata |
| 1804 change on Unix, or the time of creation on Windows, expressed in seconds | |
| 1805 * :attr:`st_atime_ns` - time of most recent access | |
| 1806 expressed in nanoseconds as an integer, | |
| 1807 * :attr:`st_mtime_ns` - time of most recent content modification | |
| 1808 expressed in nanoseconds as an integer, | |
| 1809 * :attr:`st_ctime_ns` - platform dependent; time of most recent metadata | |
| 1810 change on Unix, or the time of creation on Windows, | |
| 1811 expressed in nanoseconds as an integer | |
| 1813 | 1812 |
| 1814 On some Unix systems (such as Linux), the following attributes may also be | 1813 On some Unix systems (such as Linux), the following attributes may also be |
| 1815 available: | 1814 available: |
| 1816 | 1815 |
| 1817 * :attr:`st_blocks` - number of blocks allocated for file | 1816 * :attr:`st_blocks` - number of blocks allocated for file |
| 1818 * :attr:`st_blksize` - filesystem blocksize | 1817 * :attr:`st_blksize` - filesystem blocksize |
| 1819 * :attr:`st_rdev` - type of device if an inode device | 1818 * :attr:`st_rdev` - type of device if an inode device |
| 1820 * :attr:`st_flags` - user defined flags for file | 1819 * :attr:`st_flags` - user defined flags for file |
| 1821 | 1820 |
| 1822 On other Unix systems (such as FreeBSD), the following attributes may be | 1821 On other Unix systems (such as FreeBSD), the following attributes may be |
| 1823 available (but may be only filled out if root tries to use them): | 1822 available (but may be only filled out if root tries to use them): |
| 1824 | 1823 |
| 1825 * :attr:`st_gen` - file generation number | 1824 * :attr:`st_gen` - file generation number |
| 1826 * :attr:`st_birthtime` - time of file creation | 1825 * :attr:`st_birthtime` - time of file creation |
| 1827 | 1826 |
| 1828 On Mac OS systems, the following attributes may also be available: | 1827 On Mac OS systems, the following attributes may also be available: |
| 1829 | 1828 |
| 1830 * :attr:`st_rsize` | 1829 * :attr:`st_rsize` |
| 1831 * :attr:`st_creator` | 1830 * :attr:`st_creator` |
| 1832 * :attr:`st_type` | 1831 * :attr:`st_type` |
| 1833 | 1832 |
| 1834 .. note:: | 1833 .. note:: |
| 1835 | 1834 |
| 1836 The exact meaning and resolution of the :attr:`st_atime`, | 1835 The exact meaning and resolution of the :attr:`st_atime`, |
| 1837 :attr:`st_mtime`, and :attr:`st_ctime` attributes depend on the operating | 1836 :attr:`st_mtime`, and :attr:`st_ctime` attributes depend on the operating |
| 1838 system and the file system. For example, on Windows systems using the FAT | 1837 system and the file system. For example, on Windows systems using the FAT |
| 1839 or FAT32 file systems, :attr:`st_mtime` has 2-second resolution, and | 1838 or FAT32 file systems, :attr:`st_mtime` has 2-second resolution, and |
| 1840 :attr:`st_atime` has only 1-day resolution. See your operating system | 1839 :attr:`st_atime` has only 1-day resolution. See your operating system |
| 1841 documentation for details. | 1840 documentation for details. |
| 1842 | 1841 Similarly, although :attr:`st_atime_ns`, :attr:`st_mtime_ns`, |
| 1843 For backward compatibility, the return value of :func:`~os.stat` is also acce ssible | 1842 and :attr:`st_ctime_ns` are always expressed in nanoseconds, many |
| 1844 as a tuple of at least 10 integers giving the most important (and portable) | 1843 systems do not provide nanosecond precision. On systems that do |
| 1845 members of the :c:type:`stat` structure, in the order :attr:`st_mode`, | 1844 provide nanosecond precision, the floating-point object used to |
| 1846 :attr:`st_ino`, :attr:`st_dev`, :attr:`st_nlink`, :attr:`st_uid`, | 1845 store :attr:`st_atime`, :attr:`st_mtime`, and :attr:`st_ctime` |
| 1847 :attr:`st_gid`, :attr:`st_size`, :attr:`st_atime`, :attr:`st_mtime`, | 1846 cannot preserve all of it, and as such will be slightly inexact. |
| 1848 :attr:`st_ctime`. More items may be added at the end by some implementations. | 1847 If you need the exact timestamps you should always use |
| 1848 :attr:`st_atime_ns`, :attr:`st_mtime_ns`, and :attr:`st_ctime_ns`. | |
| 1849 | |
| 1850 For backward compatibility, the return value of :func:`~os.stat` is also | |
| 1851 accessible as a tuple of at least 10 integers giving the most important (and | |
| 1852 portable) members of the :c:type:`stat` structure, in the order | |
| 1853 :attr:`st_mode`, :attr:`st_ino`, :attr:`st_dev`, :attr:`st_nlink`, | |
| 1854 :attr:`st_uid`, :attr:`st_gid`, :attr:`st_size`, :attr:`st_atime`, | |
| 1855 :attr:`st_mtime`, :attr:`st_ctime`. More items may be added at the end by | |
| 1856 some implementations. | |
| 1857 | |
| 1858 This function can support :ref:`specifying a file descriptor | |
| 1859 <path_fd>`, :ref:`specifying a file descriptor <path_fd>` and :ref:`not | |
| 1860 following symlinks <follow_symlinks>`. | |
| 1849 | 1861 |
| 1850 .. index:: module: stat | 1862 .. index:: module: stat |
| 1851 | 1863 |
| 1852 The standard module :mod:`stat` defines functions and constants that are usef ul | 1864 The standard module :mod:`stat` defines functions and constants that are usef ul |
| 1853 for extracting information from a :c:type:`stat` structure. (On Windows, some | 1865 for extracting information from a :c:type:`stat` structure. (On Windows, some |
| 1854 items are filled with dummy values.) | 1866 items are filled with dummy values.) |
| 1855 | 1867 |
| 1856 Example:: | 1868 Example:: |
| 1857 | 1869 |
| 1858 >>> import os | 1870 >>> import os |
| 1859 >>> statinfo = os.stat('somefile.txt') | 1871 >>> statinfo = os.stat('somefile.txt') |
| 1860 >>> statinfo | 1872 >>> statinfo |
| 1861 posix.stat_result(st_mode=33188, st_ino=7876932, st_dev=234881026, | 1873 posix.stat_result(st_mode=33188, st_ino=7876932, st_dev=234881026, |
| 1862 st_nlink=1, st_uid=501, st_gid=501, st_size=264, st_atime=1297230295, | 1874 st_nlink=1, st_uid=501, st_gid=501, st_size=264, st_atime=1297230295, |
| 1863 st_mtime=1297230027, st_ctime=1297230027) | 1875 st_mtime=1297230027, st_ctime=1297230027) |
| 1864 >>> statinfo.st_size | 1876 >>> statinfo.st_size |
| 1865 264 | 1877 264 |
| 1866 | 1878 |
| 1867 Availability: Unix, Windows. | 1879 Availability: Unix, Windows. |
| 1868 | 1880 |
| 1881 .. versionadded:: 3.3 | |
| 1882 Added the *dir_fd* and *follow_symlinks* arguments, | |
| 1883 specifying a file descriptor instead of a path, | |
| 1884 and the :attr:`st_atime_ns`, :attr:`st_mtime_ns`, | |
| 1885 and :attr:`st_ctime_ns` members. | |
| 1886 | |
| 1869 | 1887 |
| 1870 .. function:: stat_float_times([newvalue]) | 1888 .. function:: stat_float_times([newvalue]) |
| 1871 | 1889 |
| 1872 Determine whether :class:`stat_result` represents time stamps as float object s. | 1890 Determine whether :class:`stat_result` represents time stamps as float object s. |
| 1873 If *newvalue* is ``True``, future calls to :func:`~os.stat` return floats, if it is | 1891 If *newvalue* is ``True``, future calls to :func:`~os.stat` return floats, if it is |
| 1874 ``False``, future calls return ints. If *newvalue* is omitted, return the | 1892 ``False``, future calls return ints. If *newvalue* is omitted, return the |
| 1875 current setting. | 1893 current setting. |
| 1876 | 1894 |
| 1877 For compatibility with older Python versions, accessing :class:`stat_result` as | 1895 For compatibility with older Python versions, accessing :class:`stat_result` as |
| 1878 a tuple always returns integers. | 1896 a tuple always returns integers. |
| 1879 | 1897 |
| 1880 Python now returns float values by default. Applications which do not work | 1898 Python now returns float values by default. Applications which do not work |
| 1881 correctly with floating point time stamps can use this function to restore th e | 1899 correctly with floating point time stamps can use this function to restore th e |
| 1882 old behaviour. | 1900 old behaviour. |
| 1883 | 1901 |
| 1884 The resolution of the timestamps (that is the smallest possible fraction) | 1902 The resolution of the timestamps (that is the smallest possible fraction) |
| 1885 depends on the system. Some systems only support second resolution; on these | 1903 depends on the system. Some systems only support second resolution; on these |
| 1886 systems, the fraction will always be zero. | 1904 systems, the fraction will always be zero. |
| 1887 | 1905 |
| 1888 It is recommended that this setting is only changed at program startup time i n | 1906 It is recommended that this setting is only changed at program startup time i n |
| 1889 the *__main__* module; libraries should never change this setting. If an | 1907 the *__main__* module; libraries should never change this setting. If an |
| 1890 application uses a library that works incorrectly if floating point time stam ps | 1908 application uses a library that works incorrectly if floating point time stam ps |
| 1891 are processed, this application should turn the feature off until the library | 1909 are processed, this application should turn the feature off until the library |
| 1892 has been corrected. | 1910 has been corrected. |
| 1911 | |
| 1912 .. deprecated:: 3.3 | |
| 1893 | 1913 |
| 1894 | 1914 |
| 1895 .. function:: statvfs(path) | 1915 .. function:: statvfs(path) |
| 1896 | 1916 |
| 1897 Perform a :c:func:`statvfs` system call on the given path. The return value is | 1917 Perform a :c:func:`statvfs` system call on the given path. The return value is |
| 1898 an object whose attributes describe the filesystem on the given path, and | 1918 an object whose attributes describe the filesystem on the given path, and |
| 1899 correspond to the members of the :c:type:`statvfs` structure, namely: | 1919 correspond to the members of the :c:type:`statvfs` structure, namely: |
| 1900 :attr:`f_bsize`, :attr:`f_frsize`, :attr:`f_blocks`, :attr:`f_bfree`, | 1920 :attr:`f_bsize`, :attr:`f_frsize`, :attr:`f_blocks`, :attr:`f_bfree`, |
| 1901 :attr:`f_bavail`, :attr:`f_files`, :attr:`f_ffree`, :attr:`f_favail`, | 1921 :attr:`f_bavail`, :attr:`f_files`, :attr:`f_ffree`, :attr:`f_favail`, |
| 1902 :attr:`f_flag`, :attr:`f_namemax`. | 1922 :attr:`f_flag`, :attr:`f_namemax`. |
| 1903 | 1923 |
| 1904 Two module-level constants are defined for the :attr:`f_flag` attribute's | 1924 Two module-level constants are defined for the :attr:`f_flag` attribute's |
| 1905 bit-flags: if :const:`ST_RDONLY` is set, the filesystem is mounted | 1925 bit-flags: if :const:`ST_RDONLY` is set, the filesystem is mounted |
| 1906 read-only, and if :const:`ST_NOSUID` is set, the semantics of | 1926 read-only, and if :const:`ST_NOSUID` is set, the semantics of |
| 1907 setuid/setgid bits are disabled or not supported. | 1927 setuid/setgid bits are disabled or not supported. |
| 1908 | 1928 |
| 1929 This function can support :ref:`specifying a file descriptor <path_fd>`. | |
| 1930 | |
| 1909 .. versionchanged:: 3.2 | 1931 .. versionchanged:: 3.2 |
| 1910 The :const:`ST_RDONLY` and :const:`ST_NOSUID` constants were added. | 1932 The :const:`ST_RDONLY` and :const:`ST_NOSUID` constants were added. |
| 1911 | 1933 |
| 1912 Availability: Unix. | 1934 Availability: Unix. |
| 1913 | 1935 |
| 1914 | 1936 .. versionadded:: 3.3 |
| 1915 .. function:: symlink(source, link_name) | 1937 Added support for specifying an open file descriptor for *path*. |
| 1916 symlink(source, link_name, target_is_directory=False) | 1938 |
| 1939 | |
| 1940 .. data:: supports_dir_fd | |
| 1941 | |
| 1942 A :class:`~collections.Set` object indicating which functions in the | |
| 1943 :mod:`os` module permit use of their *dir_fd* parameter. Different platforms | |
| 1944 provide different functionality, and an option that might work on one might | |
| 1945 be unsupported on another. For consistency's sakes, functions that support | |
| 1946 *dir_fd* always allow specifying the parameter, but will throw an exception | |
| 1947 if the functionality is not actually available. | |
| 1948 | |
| 1949 To check whether a particular function permits use of its *dir_fd* | |
| 1950 parameter, use the ``in`` operator on ``supports_dir_fd``. As an example, | |
| 1951 this expression determines whether the *dir_fd* parameter of :func:`os.stat` | |
| 1952 is locally available:: | |
| 1953 | |
| 1954 os.stat in os.supports_dir_fd | |
| 1955 | |
| 1956 Currently *dir_fd* parameters only work on Unix platforms; none of them work | |
| 1957 on Windows. | |
| 1958 | |
| 1959 .. versionadded:: 3.3 | |
| 1960 | |
| 1961 | |
| 1962 .. data:: supports_effective_ids | |
| 1963 | |
| 1964 A :class:`~collections.Set` object indicating which functions in the | |
| 1965 :mod:`os` module permit use of the *effective_ids* parameter for | |
| 1966 :func:`os.access`. If the local platform supports it, the collection will | |
| 1967 contain :func:`os.access`, otherwise it will be empty. | |
| 1968 | |
| 1969 To check whether you can use the *effective_ids* parameter for | |
| 1970 :func:`os.access`, use the ``in`` operator on ``supports_dir_fd``, like so:: | |
| 1971 | |
| 1972 os.access in os.supports_effective_ids | |
| 1973 | |
| 1974 Currently *effective_ids* only works on Unix platforms; it does not work on | |
| 1975 Windows. | |
| 1976 | |
| 1977 .. versionadded:: 3.3 | |
| 1978 | |
| 1979 | |
| 1980 .. data:: supports_fd | |
| 1981 | |
| 1982 A :class:`~collections.Set` object indicating which functions in the | |
| 1983 :mod:`os` module permit specifying their *path* parameter as an open file | |
| 1984 descriptor. Different platforms provide different functionality, and an | |
| 1985 option that might work on one might be unsupported on another. For | |
| 1986 consistency's sakes, functions that support *fd* always allow specifying | |
| 1987 the parameter, but will throw an exception if the functionality is not | |
| 1988 actually available. | |
| 1989 | |
| 1990 To check whether a particular function permits specifying an open file | |
| 1991 descriptor for its *path* parameter, use the ``in`` operator on | |
| 1992 ``supports_fd``. As an example, this expression determines whether | |
| 1993 :func:`os.chdir` accepts open file descriptors when called on your local | |
| 1994 platform:: | |
| 1995 | |
| 1996 os.chdir in os.supports_fd | |
| 1997 | |
| 1998 .. versionadded:: 3.3 | |
| 1999 | |
| 2000 | |
| 2001 .. data:: supports_follow_symlinks | |
| 2002 | |
| 2003 A :class:`~collections.Set` object indicating which functions in the | |
| 2004 :mod:`os` module permit use of their *follow_symlinks* parameter. Different | |
| 2005 platforms provide different functionality, and an option that might work on | |
| 2006 one might be unsupported on another. For consistency's sakes, functions that | |
| 2007 support *follow_symlinks* always allow specifying the parameter, but will | |
| 2008 throw an exception if the functionality is not actually available. | |
| 2009 | |
| 2010 To check whether a particular function permits use of its *follow_symlinks* | |
| 2011 parameter, use the ``in`` operator on ``supports_follow_symlinks``. As an | |
| 2012 example, this expression determines whether the *follow_symlinks* parameter | |
| 2013 of :func:`os.stat` is locally available:: | |
| 2014 | |
| 2015 os.stat in os.supports_follow_symlinks | |
| 2016 | |
| 2017 .. versionadded:: 3.3 | |
| 2018 | |
| 2019 | |
| 2020 .. function:: symlink(source, link_name, target_is_directory=False, *, dir_fd=No ne) | |
| 1917 | 2021 |
| 1918 Create a symbolic link pointing to *source* named *link_name*. | 2022 Create a symbolic link pointing to *source* named *link_name*. |
| 1919 | 2023 |
| 1920 On Windows, symlink version takes an additional optional parameter, | 2024 On Windows, a symlink represents either a file or a directory, and does not |
| 1921 *target_is_directory*, which defaults to ``False``. | 2025 morph to the target dynamically. If *target_is_directory* is set to ``True`` , |
| 1922 | 2026 the symlink will be created as a directory symlink, otherwise as a file symli nk |
| 1923 On Windows, a symlink represents a file or a directory, and does not morph to | 2027 (the default). On non-Window platforms, *target_is_directory* is ignored. |
| 1924 the target dynamically. For this reason, when creating a symlink on Windows, | |
| 1925 if the target is not already present, the symlink will default to being a | |
| 1926 file symlink. If *target_is_directory* is set to ``True``, the symlink will | |
| 1927 be created as a directory symlink. This parameter is ignored if the target | |
| 1928 exists (and the symlink is created with the same type as the target). | |
| 1929 | 2028 |
| 1930 Symbolic link support was introduced in Windows 6.0 (Vista). :func:`symlink` | 2029 Symbolic link support was introduced in Windows 6.0 (Vista). :func:`symlink` |
| 1931 will raise a :exc:`NotImplementedError` on Windows versions earlier than 6.0. | 2030 will raise a :exc:`NotImplementedError` on Windows versions earlier than 6.0. |
| 1932 | 2031 |
| 2032 This function can support :ref:`paths relative to directory descriptors | |
| 2033 <dir_fd>`. | |
| 2034 | |
| 1933 .. note:: | 2035 .. note:: |
| 1934 | 2036 |
| 1935 The *SeCreateSymbolicLinkPrivilege* is required in order to successfully | 2037 On Windows, the *SeCreateSymbolicLinkPrivilege* is required in order to |
| 1936 create symlinks. This privilege is not typically granted to regular | 2038 successfully create symlinks. This privilege is not typically granted to |
| 1937 users but is available to accounts which can escalate privileges to the | 2039 regular users but is available to accounts which can escalate privileges |
| 1938 administrator level. Either obtaining the privilege or running your | 2040 to the administrator level. Either obtaining the privilege or running your |
| 1939 application as an administrator are ways to successfully create symlinks. | 2041 application as an administrator are ways to successfully create symlinks. |
| 1940 | |
| 1941 | 2042 |
| 1942 :exc:`OSError` is raised when the function is called by an unprivileged | 2043 :exc:`OSError` is raised when the function is called by an unprivileged |
| 1943 user. | 2044 user. |
| 1944 | 2045 |
| 1945 Availability: Unix, Windows. | 2046 Availability: Unix, Windows. |
| 1946 | 2047 |
| 1947 .. versionchanged:: 3.2 | 2048 .. versionchanged:: 3.2 |
| 1948 Added support for Windows 6.0 (Vista) symbolic links. | 2049 Added support for Windows 6.0 (Vista) symbolic links. |
| 1949 | 2050 |
| 2051 .. versionadded:: 3.3 | |
| 2052 Added the *dir_fd* argument, and now allow *target_is_directory* | |
| 2053 on non-Windows platforms. | |
| 2054 | |
| 1950 | 2055 |
| 1951 .. function:: sync() | 2056 .. function:: sync() |
| 1952 | 2057 |
| 1953 Force write of everything to disk. | 2058 Force write of everything to disk. |
| 1954 | 2059 |
| 1955 Availability: Unix. | 2060 Availability: Unix. |
| 1956 | 2061 |
| 1957 .. versionadded:: 3.3 | 2062 .. versionadded:: 3.3 |
| 1958 | 2063 |
| 1959 | 2064 |
| 1960 .. function:: truncate(path, length) | 2065 .. function:: truncate(path, length) |
| 1961 | 2066 |
| 1962 Truncate the file corresponding to *path*, so that it is at most | 2067 Truncate the file corresponding to *path*, so that it is at most |
| 1963 *length* bytes in size. | 2068 *length* bytes in size. |
| 1964 | 2069 |
| 1965 Availability: Unix. | 2070 This function can support :ref:`specifying a file descriptor <path_fd>`. |
| 1966 | 2071 |
| 1967 .. versionadded:: 3.3 | 2072 Availability: Unix. |
| 1968 | 2073 |
| 1969 | 2074 .. versionadded:: 3.3 |
| 1970 .. function:: unlink(path) | 2075 |
| 1971 | 2076 |
| 1972 Remove (delete) the file *path*. This is the same function as | 2077 .. function:: unlink(path, *, dir_fd=None) |
| 1973 :func:`remove`; the :func:`unlink` name is its traditional Unix | 2078 |
| 1974 name. | 2079 Remove (delete) the file *path*. This function is identical to |
| 1975 | 2080 :func:`remove`; the ``unlink`` name is its traditional Unix |
| 1976 Availability: Unix, Windows. | 2081 name. Please see the documentation for :func:`remove` for |
| 1977 | 2082 further information. |
| 1978 | 2083 |
| 1979 .. function:: utime(path, times) | 2084 Availability: Unix, Windows. |
| 1980 | 2085 |
| 1981 Set the access and modified times of the file specified by *path*. If *times* | 2086 .. versionadded:: 3.3 |
| 1982 is ``None``, then the file's access and modified times are set to the current | 2087 The *dir_fd* parameter. |
| 1983 time. (The effect is similar to running the Unix program :program:`touch` on | 2088 |
| 1984 the path.) Otherwise, *times* must be a 2-tuple of numbers, of the form | 2089 |
| 1985 ``(atime, mtime)`` which is used to set the access and modified times, | 2090 .. function:: utime(path, times=None, *, ns=None, dir_fd=None, follow_symlinks=T rue) |
| 1986 respectively. Whether a directory can be given for *path* depends on whether | 2091 |
| 1987 the operating system implements directories as files (for example, Windows | 2092 Set the access and modified times of the file specified by *path*. |
| 1988 does not). Note that the exact times you set here may not be returned by a | 2093 |
| 1989 subsequent :func:`~os.stat` call, depending on the resolution with which your | 2094 :func:`utime` takes two optional parameters, *times* and *ns*. |
| 1990 operating system records access and modification times; see :func:`~os.stat`. | 2095 These specify the times set on *path* and are used as follows: |
| 1991 | 2096 |
| 1992 Availability: Unix, Windows. | 2097 - If *ns* is not ``None``, |
| 2098 it must be a 2-tuple of the form ``(atime_ns, mtime_ns)`` | |
| 2099 where each member is an int expressing nanoseconds. | |
| 2100 - If *times* is not ``None``, | |
| 2101 it must be a 2-tuple of the form ``(atime, mtime)`` | |
| 2102 where each member is an int or float expressing seconds. | |
| 2103 - If *times* and *ns* are both ``None``, | |
| 2104 this is equivalent to specifying ``ns=(atime_ns, mtime_ns)`` | |
| 2105 where both times are the current time. | |
| 2106 (The effect is similar to running the Unix program | |
| 2107 :program:`touch` on *path*.) | |
| 2108 | |
| 2109 It is an error to specify tuples for both *times* and *ns*. | |
| 2110 | |
| 2111 Whether a directory can be given for *path* | |
| 2112 depends on whether the operating system implements directories as files | |
| 2113 (for example, Windows does not). Note that the exact times you set here may | |
| 2114 not be returned by a subsequent :func:`~os.stat` call, depending on the | |
| 2115 resolution with which your operating system records access and modification | |
| 2116 times; see :func:`~os.stat`. The best way to preserve exact times is to | |
| 2117 use the *st_atime_ns* and *st_mtime_ns* fields from the :func:`os.stat` | |
| 2118 result object with the *ns* parameter to `utime`. | |
| 2119 | |
| 2120 This function can support :ref:`specifying a file descriptor <path_fd>`, | |
| 2121 :ref:`paths relative to directory descriptors <dir_fd>` and :ref:`not | |
| 2122 following symlinks <follow_symlinks>`. | |
| 2123 | |
| 2124 Availability: Unix, Windows. | |
| 2125 | |
| 2126 .. versionadded:: 3.3 | |
| 2127 Added support for specifying an open file descriptor for *path*, | |
| 2128 and the *dir_fd*, *follow_symlinks*, and *ns* parameters. | |
| 1993 | 2129 |
| 1994 | 2130 |
| 1995 .. function:: walk(top, topdown=True, onerror=None, followlinks=False) | 2131 .. function:: walk(top, topdown=True, onerror=None, followlinks=False) |
| 1996 | 2132 |
| 1997 .. index:: | 2133 .. index:: |
| 1998 single: directory; walking | 2134 single: directory; walking |
| 1999 single: directory; traversal | 2135 single: directory; traversal |
| 2000 | 2136 |
| 2001 Generate the file names in a directory tree by walking the tree | 2137 Generate the file names in a directory tree by walking the tree |
| 2002 either top-down or bottom-up. For each directory in the tree rooted at direct ory | 2138 either top-down or bottom-up. For each directory in the tree rooted at direct ory |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 2018 | 2154 |
| 2019 When *topdown* is ``True``, the caller can modify the *dirnames* list in-plac e | 2155 When *topdown* is ``True``, the caller can modify the *dirnames* list in-plac e |
| 2020 (perhaps using :keyword:`del` or slice assignment), and :func:`walk` will onl y | 2156 (perhaps using :keyword:`del` or slice assignment), and :func:`walk` will onl y |
| 2021 recurse into the subdirectories whose names remain in *dirnames*; this can be | 2157 recurse into the subdirectories whose names remain in *dirnames*; this can be |
| 2022 used to prune the search, impose a specific order of visiting, or even to inf orm | 2158 used to prune the search, impose a specific order of visiting, or even to inf orm |
| 2023 :func:`walk` about directories the caller creates or renames before it resume s | 2159 :func:`walk` about directories the caller creates or renames before it resume s |
| 2024 :func:`walk` again. Modifying *dirnames* when *topdown* is ``False`` is | 2160 :func:`walk` again. Modifying *dirnames* when *topdown* is ``False`` is |
| 2025 ineffective, because in bottom-up mode the directories in *dirnames* are | 2161 ineffective, because in bottom-up mode the directories in *dirnames* are |
| 2026 generated before *dirpath* itself is generated. | 2162 generated before *dirpath* itself is generated. |
| 2027 | 2163 |
| 2028 By default errors from the :func:`listdir` call are ignored. If optional | 2164 By default, errors from the :func:`listdir` call are ignored. If optional |
| 2029 argument *onerror* is specified, it should be a function; it will be called w ith | 2165 argument *onerror* is specified, it should be a function; it will be called w ith |
| 2030 one argument, an :exc:`OSError` instance. It can report the error to continu e | 2166 one argument, an :exc:`OSError` instance. It can report the error to continu e |
| 2031 with the walk, or raise the exception to abort the walk. Note that the filen ame | 2167 with the walk, or raise the exception to abort the walk. Note that the filen ame |
| 2032 is available as the ``filename`` attribute of the exception object. | 2168 is available as the ``filename`` attribute of the exception object. |
| 2033 | 2169 |
| 2034 By default, :func:`walk` will not walk down into symbolic links that resolve to | 2170 By default, :func:`walk` will not walk down into symbolic links that resolve to |
| 2035 directories. Set *followlinks* to ``True`` to visit directories pointed to by | 2171 directories. Set *followlinks* to ``True`` to visit directories pointed to by |
| 2036 symlinks, on systems that support them. | 2172 symlinks, on systems that support them. |
| 2037 | 2173 |
| 2038 .. note:: | 2174 .. note:: |
| 2039 | 2175 |
| 2040 Be aware that setting *followlinks* to ``True`` can lead to infinite recur sion if a | 2176 Be aware that setting *followlinks* to ``True`` can lead to infinite |
| 2041 link points to a parent directory of itself. :func:`walk` does not keep tr ack of | 2177 recursion if a link points to a parent directory of itself. :func:`walk` |
| 2042 the directories it visited already. | 2178 does not keep track of the directories it visited already. |
| 2043 | 2179 |
| 2044 .. note:: | 2180 .. note:: |
| 2045 | 2181 |
| 2046 If you pass a relative pathname, don't change the current working director y | 2182 If you pass a relative pathname, don't change the current working director y |
| 2047 between resumptions of :func:`walk`. :func:`walk` never changes the curre nt | 2183 between resumptions of :func:`walk`. :func:`walk` never changes the curre nt |
| 2048 directory, and assumes that its caller doesn't either. | 2184 directory, and assumes that its caller doesn't either. |
| 2049 | 2185 |
| 2050 This example displays the number of bytes taken by non-directory files in eac h | 2186 This example displays the number of bytes taken by non-directory files in eac h |
| 2051 directory under the starting directory, except that it doesn't look under any | 2187 directory under the starting directory, except that it doesn't look under any |
| 2052 CVS subdirectory:: | 2188 CVS subdirectory:: |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 2068 # CAUTION: This is dangerous! For example, if top == '/', it | 2204 # CAUTION: This is dangerous! For example, if top == '/', it |
| 2069 # could delete all your disk files. | 2205 # could delete all your disk files. |
| 2070 import os | 2206 import os |
| 2071 for root, dirs, files in os.walk(top, topdown=False): | 2207 for root, dirs, files in os.walk(top, topdown=False): |
| 2072 for name in files: | 2208 for name in files: |
| 2073 os.remove(os.path.join(root, name)) | 2209 os.remove(os.path.join(root, name)) |
| 2074 for name in dirs: | 2210 for name in dirs: |
| 2075 os.rmdir(os.path.join(root, name)) | 2211 os.rmdir(os.path.join(root, name)) |
| 2076 | 2212 |
| 2077 | 2213 |
| 2214 .. function:: fwalk(top='.', topdown=True, onerror=None, *, follow_symlinks=Fals e, dir_fd=None) | |
| 2215 | |
| 2216 .. index:: | |
| 2217 single: directory; walking | |
| 2218 single: directory; traversal | |
| 2219 | |
| 2220 This behaves exactly like :func:`walk`, except that it yields a 4-tuple | |
| 2221 ``(dirpath, dirnames, filenames, dirfd)``, and it supports ``dir_fd``. | |
| 2222 | |
| 2223 *dirpath*, *dirnames* and *filenames* are identical to :func:`walk` output, | |
| 2224 and *dirfd* is a file descriptor referring to the directory *dirpath*. | |
| 2225 | |
| 2226 This function always supports :ref:`paths relative to directory descriptors | |
| 2227 <dir_fd>` and :ref:`not following symlinks <follow_symlinks>`. Note however | |
| 2228 that, unlike other functions, the :func:`fwalk` default value for | |
| 2229 *follow_symlinks* is ``False``. | |
| 2230 | |
| 2231 .. note:: | |
| 2232 | |
| 2233 Since :func:`fwalk` yields file descriptors, those are only valid until | |
| 2234 the next iteration step, so you should duplicate them (e.g. with | |
| 2235 :func:`dup`) if you want to keep them longer. | |
| 2236 | |
| 2237 This example displays the number of bytes taken by non-directory files in eac h | |
| 2238 directory under the starting directory, except that it doesn't look under any | |
| 2239 CVS subdirectory:: | |
| 2240 | |
| 2241 import os | |
| 2242 for root, dirs, files, rootfd in os.fwalk('python/Lib/email'): | |
| 2243 print(root, "consumes", end="") | |
| 2244 print(sum([os.stat(name, dir_fd=rootfd).st_size for name in files]), | |
| 2245 end="") | |
| 2246 print("bytes in", len(files), "non-directory files") | |
| 2247 if 'CVS' in dirs: | |
| 2248 dirs.remove('CVS') # don't visit CVS directories | |
| 2249 | |
| 2250 In the next example, walking the tree bottom-up is essential: | |
| 2251 :func:`unlinkat` doesn't allow deleting a directory before the directory is | |
| 2252 empty:: | |
| 2253 | |
| 2254 # Delete everything reachable from the directory named in "top", | |
| 2255 # assuming there are no symbolic links. | |
| 2256 # CAUTION: This is dangerous! For example, if top == '/', it | |
| 2257 # could delete all your disk files. | |
| 2258 import os | |
| 2259 for root, dirs, files, rootfd in os.fwalk(top, topdown=False): | |
| 2260 for name in files: | |
| 2261 os.unlinkat(rootfd, name) | |
| 2262 for name in dirs: | |
| 2263 os.unlinkat(rootfd, name, os.AT_REMOVEDIR) | |
| 2264 | |
| 2265 Availability: Unix. | |
| 2266 | |
| 2267 .. versionadded:: 3.3 | |
| 2268 | |
| 2269 | |
| 2270 Linux extended attributes | |
| 2271 ~~~~~~~~~~~~~~~~~~~~~~~~~ | |
| 2272 | |
| 2273 .. versionadded:: 3.3 | |
| 2274 | |
| 2275 These functions are all available on Linux only. | |
| 2276 | |
| 2277 .. function:: getxattr(path, attribute, *, follow_symlinks=True) | |
| 2278 | |
| 2279 Return the value of the extended filesystem attribute *attribute* for | |
| 2280 *path*. *attribute* can be bytes or str. If it is str, it is encoded | |
| 2281 with the filesystem encoding. | |
| 2282 | |
| 2283 This function can support :ref:`specifying a file descriptor <path_fd>` and | |
| 2284 :ref:`not following symlinks <follow_symlinks>`. | |
| 2285 | |
| 2286 | |
| 2287 .. function:: listxattr(path=None, *, follow_symlinks=True) | |
| 2288 | |
| 2289 Return a list of the extended filesystem attributes on *path*. The | |
| 2290 attributes in the list are represented as strings decoded with the filesystem | |
| 2291 encoding. If *path* is ``None``, :func:`listxattr` will examine the current | |
| 2292 directory. | |
| 2293 | |
| 2294 This function can support :ref:`specifying a file descriptor <path_fd>` and | |
| 2295 :ref:`not following symlinks <follow_symlinks>`. | |
| 2296 | |
| 2297 | |
| 2298 .. function:: removexattr(path, attribute, *, follow_symlinks=True) | |
| 2299 | |
| 2300 Removes the extended filesystem attribute *attribute* from *path*. | |
| 2301 *attribute* should be bytes or str. If it is a string, it is encoded | |
| 2302 with the filesystem encoding. | |
| 2303 | |
| 2304 This function can support :ref:`specifying a file descriptor <path_fd>` and | |
| 2305 :ref:`not following symlinks <follow_symlinks>`. | |
| 2306 | |
| 2307 | |
| 2308 .. function:: setxattr(path, attribute, value, flags=0, *, follow_symlinks=True) | |
| 2309 | |
| 2310 Set the extended filesystem attribute *attribute* on *path* to *value*. | |
| 2311 *attribute* must be a bytes or str with no embedded NULs. If it is a str, | |
| 2312 it is encoded with the filesystem encoding. *flags* may be | |
| 2313 :data:`XATTR_REPLACE` or :data:`XATTR_CREATE`. If :data:`XATTR_REPLACE` is | |
| 2314 given and the attribute does not exist, ``EEXISTS`` will be raised. | |
| 2315 If :data:`XATTR_CREATE` is given and the attribute already exists, the | |
| 2316 attribute will not be created and ``ENODATA`` will be raised. | |
| 2317 | |
| 2318 This function can support :ref:`specifying a file descriptor <path_fd>` and | |
| 2319 :ref:`not following symlinks <follow_symlinks>`. | |
| 2320 | |
| 2321 .. note:: | |
| 2322 | |
| 2323 A bug in Linux kernel versions less than 2.6.39 caused the flags argument | |
| 2324 to be ignored on some filesystems. | |
| 2325 | |
| 2326 | |
| 2327 .. data:: XATTR_SIZE_MAX | |
| 2328 | |
| 2329 The maximum size the value of an extended attribute can be. Currently, this | |
| 2330 is 64 kilobytes on Linux. | |
| 2331 | |
| 2332 | |
| 2333 .. data:: XATTR_CREATE | |
| 2334 | |
| 2335 This is a possible value for the flags argument in :func:`setxattr`. It | |
| 2336 indicates the operation must create an attribute. | |
| 2337 | |
| 2338 | |
| 2339 .. data:: XATTR_REPLACE | |
| 2340 | |
| 2341 This is a possible value for the flags argument in :func:`setxattr`. It | |
| 2342 indicates the operation must replace an existing attribute. | |
| 2343 | |
| 2344 | |
| 2078 .. _os-process: | 2345 .. _os-process: |
| 2079 | 2346 |
| 2080 Process Management | 2347 Process Management |
| 2081 ------------------ | 2348 ------------------ |
| 2082 | 2349 |
| 2083 These functions may be used to create and manage processes. | 2350 These functions may be used to create and manage processes. |
| 2084 | 2351 |
| 2085 The various :func:`exec\*` functions take a list of arguments for the new | 2352 The various :func:`exec\*` functions take a list of arguments for the new |
| 2086 program loaded into the process. In each case, the first of these arguments is | 2353 program loaded into the process. In each case, the first of these arguments is |
| 2087 passed to the new program as its own name rather than as an argument a user may | 2354 passed to the new program as its own name rather than as an argument a user may |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2141 locate the executable; *path* must contain an appropriate absolute or relativ e | 2408 locate the executable; *path* must contain an appropriate absolute or relativ e |
| 2142 path. | 2409 path. |
| 2143 | 2410 |
| 2144 For :func:`execle`, :func:`execlpe`, :func:`execve`, and :func:`execvpe` (not e | 2411 For :func:`execle`, :func:`execlpe`, :func:`execve`, and :func:`execvpe` (not e |
| 2145 that these all end in "e"), the *env* parameter must be a mapping which is | 2412 that these all end in "e"), the *env* parameter must be a mapping which is |
| 2146 used to define the environment variables for the new process (these are used | 2413 used to define the environment variables for the new process (these are used |
| 2147 instead of the current process' environment); the functions :func:`execl`, | 2414 instead of the current process' environment); the functions :func:`execl`, |
| 2148 :func:`execlp`, :func:`execv`, and :func:`execvp` all cause the new process t o | 2415 :func:`execlp`, :func:`execv`, and :func:`execvp` all cause the new process t o |
| 2149 inherit the environment of the current process. | 2416 inherit the environment of the current process. |
| 2150 | 2417 |
| 2151 Availability: Unix, Windows. | 2418 For :func:`execve` on some platforms, *path* may also be specified as an open |
| 2152 | 2419 file descriptor. This functionality may not be supported on your platform; |
| 2420 you can check whether or not it is available using :data:`os.supports_fd`. | |
| 2421 If it is unavailable, using it will raise a :exc:`NotImplementedError`. | |
| 2422 | |
| 2423 Availability: Unix, Windows. | |
| 2424 | |
| 2425 .. versionadded:: 3.3 | |
| 2426 Added support for specifying an open file descriptor for *path* | |
| 2427 for :func:`execve`. | |
| 2153 | 2428 |
| 2154 .. function:: _exit(n) | 2429 .. function:: _exit(n) |
| 2155 | 2430 |
| 2156 Exit the process with status *n*, without calling cleanup handlers, flushing | 2431 Exit the process with status *n*, without calling cleanup handlers, flushing |
| 2157 stdio buffers, etc. | 2432 stdio buffers, etc. |
| 2158 | 2433 |
| 2159 Availability: Unix, Windows. | 2434 Availability: Unix, Windows. |
| 2160 | 2435 |
| 2161 .. note:: | 2436 .. note:: |
| 2162 | 2437 |
| (...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2524 The :mod:`subprocess` module provides more powerful facilities for spawning | 2799 The :mod:`subprocess` module provides more powerful facilities for spawning |
| 2525 new processes and retrieving their results; using that module is preferable | 2800 new processes and retrieving their results; using that module is preferable |
| 2526 to using this function. See the :ref:`subprocess-replacements` section in | 2801 to using this function. See the :ref:`subprocess-replacements` section in |
| 2527 the :mod:`subprocess` documentation for some helpful recipes. | 2802 the :mod:`subprocess` documentation for some helpful recipes. |
| 2528 | 2803 |
| 2529 Availability: Unix, Windows. | 2804 Availability: Unix, Windows. |
| 2530 | 2805 |
| 2531 | 2806 |
| 2532 .. function:: times() | 2807 .. function:: times() |
| 2533 | 2808 |
| 2534 Return a 5-tuple of floating point numbers indicating accumulated (processor | 2809 Returns the current global process times. |
| 2535 or other) times, in seconds. The items are: user time, system time, | 2810 The return value is an object with five attributes: |
| 2536 children's user time, children's system time, and elapsed real time since a | 2811 |
| 2537 fixed point in the past, in that order. See the Unix manual page | 2812 * :attr:`user` - user time |
| 2813 * :attr:`system` - system time | |
| 2814 * :attr:`children_user` - user time of all child processes | |
| 2815 * :attr:`children_system` - system time of all child processes | |
| 2816 * :attr:`elapsed` - elapsed real time since a fixed point in the past | |
| 2817 | |
| 2818 For backwards compatibility, this object also behaves like a five-tuple | |
| 2819 containing :attr:`user`, :attr:`system`, :attr:`children_user`, | |
| 2820 :attr:`children_system`, and :attr:`elapsed` in that order. | |
| 2821 | |
| 2822 See the Unix manual page | |
| 2538 :manpage:`times(2)` or the corresponding Windows Platform API documentation. | 2823 :manpage:`times(2)` or the corresponding Windows Platform API documentation. |
| 2539 On Windows, only the first two items are filled, the others are zero. | 2824 On Windows, only :attr:`user` and :attr:`system` are known; the other |
| 2540 | 2825 attributes are zero. |
| 2541 Availability: Unix, Windows | 2826 On OS/2, only :attr:`elapsed` is known; the other attributes are zero. |
| 2827 | |
| 2828 Availability: Unix, Windows. | |
| 2829 | |
| 2830 .. versionchanged:: 3.3 | |
| 2831 Return type changed from a tuple to a tuple-like object | |
| 2832 with named attributes. | |
| 2542 | 2833 |
| 2543 | 2834 |
| 2544 .. function:: wait() | 2835 .. function:: wait() |
| 2545 | 2836 |
| 2546 Wait for completion of a child process, and return a tuple containing its pid | 2837 Wait for completion of a child process, and return a tuple containing its pid |
| 2547 and exit status indication: a 16-bit number, whose low byte is the signal num ber | 2838 and exit status indication: a 16-bit number, whose low byte is the signal num ber |
| 2548 that killed the process, and whose high byte is the exit status (if the signa l | 2839 that killed the process, and whose high byte is the exit status (if the signa l |
| 2549 number is zero); the high bit of the low byte is set if a core file was | 2840 number is zero); the high bit of the low byte is set if a core file was |
| 2550 produced. | 2841 produced. |
| 2551 | 2842 |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2660 is available immediately. The function returns ``(0, 0)`` in this case. | 2951 is available immediately. The function returns ``(0, 0)`` in this case. |
| 2661 | 2952 |
| 2662 Availability: Unix. | 2953 Availability: Unix. |
| 2663 | 2954 |
| 2664 | 2955 |
| 2665 .. data:: WCONTINUED | 2956 .. data:: WCONTINUED |
| 2666 | 2957 |
| 2667 This option causes child processes to be reported if they have been continued | 2958 This option causes child processes to be reported if they have been continued |
| 2668 from a job control stop since their status was last reported. | 2959 from a job control stop since their status was last reported. |
| 2669 | 2960 |
| 2670 Availability: Some Unix systems. | 2961 Availability: some Unix systems. |
| 2671 | 2962 |
| 2672 | 2963 |
| 2673 .. data:: WUNTRACED | 2964 .. data:: WUNTRACED |
| 2674 | 2965 |
| 2675 This option causes child processes to be reported if they have been stopped b ut | 2966 This option causes child processes to be reported if they have been stopped b ut |
| 2676 their current state has not been reported since they were stopped. | 2967 their current state has not been reported since they were stopped. |
| 2677 | 2968 |
| 2678 Availability: Unix. | 2969 Availability: Unix. |
| 2679 | 2970 |
| 2680 | 2971 |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2781 | 3072 |
| 2782 A round-robin scheduling policy. | 3073 A round-robin scheduling policy. |
| 2783 | 3074 |
| 2784 .. data:: SCHED_RESET_ON_FORK | 3075 .. data:: SCHED_RESET_ON_FORK |
| 2785 | 3076 |
| 2786 This flag can OR'ed with any other scheduling policy. When a process with | 3077 This flag can OR'ed with any other scheduling policy. When a process with |
| 2787 this flag set forks, its child's scheduling policy and priority are reset to | 3078 this flag set forks, its child's scheduling policy and priority are reset to |
| 2788 the default. | 3079 the default. |
| 2789 | 3080 |
| 2790 | 3081 |
| 2791 .. class:: sched_param(sched_prioirity) | 3082 .. class:: sched_param(sched_priority) |
|
Charles-François Natali
2011/07/31 00:37:27
typo: priority
| |
| 2792 | 3083 |
| 2793 This class represents tunable scheduling parameters used in | 3084 This class represents tunable scheduling parameters used in |
| 2794 :func:`sched_setparam`, :func:`sched_setscheduler`, and | 3085 :func:`sched_setparam`, :func:`sched_setscheduler`, and |
| 2795 :func:`sched_getparam`. It is immutable. | 3086 :func:`sched_getparam`. It is immutable. |
| 2796 | 3087 |
| 2797 At the moment, there is only one possible parameter: | 3088 At the moment, there is only one possible parameter: |
| 2798 | 3089 |
| 2799 .. attribute:: sched_priority | 3090 .. attribute:: sched_priority |
| 2800 | 3091 |
| 2801 The scheduling priority for a scheduling policy. | 3092 The scheduling priority for a scheduling policy. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2843 | 3134 |
| 2844 Return the round-robin quantum in seconds for the process with PID *pid*. A | 3135 Return the round-robin quantum in seconds for the process with PID *pid*. A |
| 2845 *pid* of 0 means the calling process. | 3136 *pid* of 0 means the calling process. |
| 2846 | 3137 |
| 2847 | 3138 |
| 2848 .. function:: sched_yield() | 3139 .. function:: sched_yield() |
| 2849 | 3140 |
| 2850 Voluntarily relinquish the CPU. | 3141 Voluntarily relinquish the CPU. |
| 2851 | 3142 |
| 2852 | 3143 |
| 2853 .. class:: cpu_set(ncpus) | |
| 2854 | |
| 2855 :class:`cpu_set` represents a set of CPUs on which a process runs. *ncpus* is | |
|
Charles-François Natali
2011/07/31 00:37:27
It's not really a "set of CPUs on which a process
| |
| 2856 the number of CPUs the set should describe. Methods on :class:`cpu_set` allow | |
| 2857 CPUs to be add or removed. | |
| 2858 | |
| 2859 :class:`cpu_set` supports the AND, OR, and XOR bitwise operations. For | |
| 2860 example, given two cpu_sets, ``one`` and ``two``, ``one | two`` returns a | |
| 2861 :class:`cpu_set` containing the cpus enabled both in ``one`` and ``two``. | |
| 2862 | |
| 2863 .. method:: set(i) | |
| 2864 | |
| 2865 Enable CPU *i*. | |
| 2866 | |
| 2867 .. method:: clear(i) | |
| 2868 | |
| 2869 Remove CPU *i*. | |
| 2870 | |
| 2871 .. method:: isset(i) | |
| 2872 | |
| 2873 Return ``True`` if CPU *i* is enabled in the set. | |
| 2874 | |
| 2875 .. method:: count() | |
| 2876 | |
| 2877 Return the number of enabled CPUs in the set. | |
| 2878 | |
| 2879 .. method:: zero() | |
| 2880 | |
| 2881 Clear the set completely. | |
| 2882 | |
| 2883 | |
| 2884 .. function:: sched_setaffinity(pid, mask) | 3144 .. function:: sched_setaffinity(pid, mask) |
| 2885 | 3145 |
| 2886 Restrict the process with PID *pid* to a set of CPUs. *mask* is a | 3146 Restrict the process with PID *pid* (or the current process if zero) to a |
| 2887 :class:`cpu_set` instance. | 3147 set of CPUs. *mask* is an iterable of integers representing the set of |
| 2888 | 3148 CPUs to which the process should be restricted. |
| 2889 | 3149 |
| 2890 .. function:: sched_getaffinity(pid, size) | 3150 |
| 2891 | 3151 .. function:: sched_getaffinity(pid) |
| 2892 Return the :class:`cpu_set` the process with PID *pid* is restricted to. The | 3152 |
| 2893 result will contain *size* CPUs. | 3153 Return the set of CPUs the process with PID *pid* (or the current process |
| 3154 if zero) is restricted to. | |
| 2894 | 3155 |
|
Charles-François Natali
2011/07/31 00:37:27
Thos functions accept too a PID of 0 to denote the
| |
| 2895 | 3156 |
| 2896 .. _os-path: | 3157 .. _os-path: |
| 2897 | 3158 |
| 2898 Miscellaneous System Information | 3159 Miscellaneous System Information |
| 2899 -------------------------------- | 3160 -------------------------------- |
| 2900 | 3161 |
| 2901 | 3162 |
| 2902 .. function:: confstr(name) | 3163 .. function:: confstr(name) |
| 2903 | 3164 |
| 2904 Return string-valued system configuration values. *name* specifies the | 3165 Return string-valued system configuration values. *name* specifies the |
| 2905 configuration value to retrieve; it may be a string which is the name of a | 3166 configuration value to retrieve; it may be a string which is the name of a |
| 2906 defined system value; these names are specified in a number of standards (POS IX, | 3167 defined system value; these names are specified in a number of standards (POS IX, |
| 2907 Unix 95, Unix 98, and others). Some platforms define additional names as wel l. | 3168 Unix 95, Unix 98, and others). Some platforms define additional names as wel l. |
| 2908 The names known to the host operating system are given as the keys of the | 3169 The names known to the host operating system are given as the keys of the |
| 2909 ``confstr_names`` dictionary. For configuration variables not included in th at | 3170 ``confstr_names`` dictionary. For configuration variables not included in th at |
| 2910 mapping, passing an integer for *name* is also accepted. | 3171 mapping, passing an integer for *name* is also accepted. |
| 2911 | 3172 |
| 2912 If the configuration value specified by *name* isn't defined, ``None`` is | 3173 If the configuration value specified by *name* isn't defined, ``None`` is |
| 2913 returned. | 3174 returned. |
| 2914 | 3175 |
| 2915 If *name* is a string and is not known, :exc:`ValueError` is raised. If a | 3176 If *name* is a string and is not known, :exc:`ValueError` is raised. If a |
| 2916 specific value for *name* is not supported by the host system, even if it is | 3177 specific value for *name* is not supported by the host system, even if it is |
| 2917 included in ``confstr_names``, an :exc:`OSError` is raised with | 3178 included in ``confstr_names``, an :exc:`OSError` is raised with |
| 2918 :const:`errno.EINVAL` for the error number. | 3179 :const:`errno.EINVAL` for the error number. |
| 2919 | 3180 |
| 2920 Availability: Unix | 3181 Availability: Unix. |
| 2921 | 3182 |
| 2922 | 3183 |
| 2923 .. data:: confstr_names | 3184 .. data:: confstr_names |
| 2924 | 3185 |
| 2925 Dictionary mapping names accepted by :func:`confstr` to the integer values | 3186 Dictionary mapping names accepted by :func:`confstr` to the integer values |
| 2926 defined for those names by the host operating system. This can be used to | 3187 defined for those names by the host operating system. This can be used to |
| 2927 determine the set of names known to the system. | 3188 determine the set of names known to the system. |
| 2928 | 3189 |
| 2929 Availability: Unix. | 3190 Availability: Unix. |
| 2930 | 3191 |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3032 Miscellaneous Functions | 3293 Miscellaneous Functions |
| 3033 ----------------------- | 3294 ----------------------- |
| 3034 | 3295 |
| 3035 | 3296 |
| 3036 .. function:: urandom(n) | 3297 .. function:: urandom(n) |
| 3037 | 3298 |
| 3038 Return a string of *n* random bytes suitable for cryptographic use. | 3299 Return a string of *n* random bytes suitable for cryptographic use. |
| 3039 | 3300 |
| 3040 This function returns random bytes from an OS-specific randomness source. Th e | 3301 This function returns random bytes from an OS-specific randomness source. Th e |
| 3041 returned data should be unpredictable enough for cryptographic applications, | 3302 returned data should be unpredictable enough for cryptographic applications, |
| 3042 though its exact quality depends on the OS implementation. On a UNIX-like | 3303 though its exact quality depends on the OS implementation. On a Unix-like |
| 3043 system this will query /dev/urandom, and on Windows it will use CryptGenRando m. | 3304 system this will query /dev/urandom, and on Windows it will use CryptGenRando m. |
| 3044 If a randomness source is not found, :exc:`NotImplementedError` will be raise d. | 3305 If a randomness source is not found, :exc:`NotImplementedError` will be raise d. |
| LEFT | RIGHT |