1 \section{\module{os
} ---
2 Miscellaneous OS interfaces
}
4 \declaremodule{standard
}{os
}
5 \modulesynopsis{Miscellaneous OS interfaces.
}
8 This module provides a more portable way of using operating system
9 (OS) dependent functionality than importing an OS dependent built-in
10 module like
\refmodule{posix
} or
\module{nt
}.
12 This module searches for an OS dependent built-in module like
13 \module{mac
} or
\refmodule{posix
} and exports the same functions and data
14 as found there. The design of all Python's built-in OS dependent
15 modules is such that as long as the same functionality is available,
16 it uses the same interface; e.g., the function
17 \code{os.stat(
\var{path
})
} returns stat information about
\var{path
} in
18 the same format (which happens to have originated with the
21 Extensions peculiar to a particular OS are also available through the
22 \module{os
} module, but using them is of course a threat to
25 Note that after the first time
\module{os
} is imported, there is
26 \emph{no
} performance penalty in using functions from
\module{os
}
27 instead of directly from the OS dependent built-in module, so there
28 should be
\emph{no
} reason not to use
\module{os
}!
31 % Frank Stajano <fstajano@uk.research.att.com> complained that it
32 % wasn't clear that the entries described in the subsections were all
33 % available at the module level (most uses of subsections are
34 % different); I think this is only a problem for the HTML version,
35 % where the relationship may not be as clear.
38 The
\module{os
} module contains many functions and data values.
39 The items below and in the following sub-sections are all available
40 directly from the
\module{os
} module.
44 \begin{excdesc
}{error
}
45 This exception is raised when a function returns a
46 system-related error (e.g., not for illegal argument types). This is
47 also known as the built-in exception
\exception{OSError
}. The
48 accompanying value is a pair containing the numeric error code from
49 \cdata{errno
} and the corresponding string, as would be printed by the
50 C function
\cfunction{perror()
}. See the module
51 \refmodule{errno
}\refbimodindex{errno
}, which contains names for the
52 error codes defined by the underlying operating system.
54 When exceptions are classes, this exception carries two attributes,
55 \member{errno
} and
\member{strerror
}. The first holds the value of
56 the C
\cdata{errno
} variable, and the latter holds the corresponding
57 error message from
\cfunction{strerror()
}. For exceptions that
58 involve a file system path (e.g.
\function{chdir()
} or
59 \function{unlink()
}), the exception instance will contain a third
60 attribute,
\member{filename
}, which is the file name passed to the
63 When exceptions are strings, the string for the exception is
67 \begin{datadesc
}{name
}
68 The name of the OS dependent module imported. The following names
69 have currently been registered:
\code{'posix'
},
\code{'nt'
},
70 \code{'dos'
},
\code{'mac'
},
\code{'os2'
},
\code{'ce'
},
\code{'java'
}.
73 \begin{datadesc
}{path
}
74 The corresponding OS dependent standard module for pathname
75 operations, e.g.,
\module{posixpath
} or
\module{macpath
}. Thus, given
76 the proper imports,
\code{os.path.split(
\var{file
})
} is equivalent to but
77 more portable than
\code{posixpath.split(
\var{file
})
}. Note that this
78 is also a valid module: it may be imported directly as
84 \subsection{Process Parameters
\label{os-procinfo
}}
86 These functions and data items provide information and operate on the
87 current process and user.
89 \begin{datadesc
}{environ
}
90 A mapping object representing the string environment. For example,
91 \code{environ
['HOME'
]} is the pathname of your home directory (on some
92 platforms), and is equivalent to
\code{getenv("HOME")
} in C.
94 If the platform supports the
\function{putenv()
} function, this
95 mapping may be used to modify the environment as well as query the
96 environment.
\function{putenv()
} will be called automatically when
97 the mapping is modified.
99 If
\function{putenv()
} is not provided, this mapping may be passed to
100 the appropriate process-creation functions to cause child processes to
101 use a modified environment.
104 \begin{funcdescni
}{chdir
}{path
}
105 \funclineni{getcwd
}{}
106 These functions are described in ``Files and Directories'' (section
110 \begin{funcdesc
}{ctermid
}{}
111 Return the filename corresponding to the controlling terminal of the
113 Availability:
\UNIX{}.
116 \begin{funcdesc
}{getegid
}{}
117 Return the current process' effective group id.
118 Availability:
\UNIX{}.
121 \begin{funcdesc
}{geteuid
}{}
122 \index{user!effective id
}
123 Return the current process' effective user id.
124 Availability:
\UNIX{}.
127 \begin{funcdesc
}{getgid
}{}
128 \index{process!group
}
129 Return the current process' group id.
130 Availability:
\UNIX{}.
133 \begin{funcdesc
}{getgroups
}{}
134 Return list of supplemental group ids associated with the current
136 Availability:
\UNIX{}.
139 \begin{funcdesc
}{getlogin
}{}
140 Return the actual login name for the current process, even if there
141 are multiple login names which map to the same user id.
142 Availability:
\UNIX{}.
145 \begin{funcdesc
}{getpgrp
}{}
146 \index{process!group
}
147 Return the current process group id.
148 Availability:
\UNIX{}.
151 \begin{funcdesc
}{getpid
}{}
153 Return the current process id.
154 Availability:
\UNIX{}, Windows.
157 \begin{funcdesc
}{getppid
}{}
158 \index{process!id of parent
}
159 Return the parent's process id.
160 Availability:
\UNIX{}.
163 \begin{funcdesc
}{getuid
}{}
165 Return the current process' user id.
166 Availability:
\UNIX{}.
169 \begin{funcdesc
}{putenv
}{varname, value
}
170 \index{environment variables!setting
}
171 Set the environment variable named
\var{varname
} to the string
172 \var{value
}. Such changes to the environment affect subprocesses
173 started with
\function{os.system()
},
\function{popen()
} or
174 \function{fork()
} and
\function{execv()
}.
175 Availability: most flavors of
\UNIX{}, Windows.
177 When
\function{putenv()
} is
178 supported, assignments to items in
\code{os.environ
} are automatically
179 translated into corresponding calls to
\function{putenv()
}; however,
180 calls to
\function{putenv()
} don't update
\code{os.environ
}, so it is
181 actually preferable to assign to items of
\code{os.environ
}.
184 \begin{funcdesc
}{setegid
}{egid
}
185 Set the current process's effective group id.
186 Availability:
\UNIX{}.
189 \begin{funcdesc
}{seteuid
}{euid
}
190 Set the current process's effective user id.
191 Availability:
\UNIX{}.
194 \begin{funcdesc
}{setgid
}{gid
}
195 Set the current process' group id.
196 Availability:
\UNIX{}.
199 \begin{funcdesc
}{setpgrp
}{}
200 Calls the system call
\cfunction{setpgrp()
} or
\cfunction{setpgrp(
0,
201 0)
} depending on which version is implemented (if any). See the
202 \UNIX{} manual for the semantics.
203 Availability:
\UNIX{}.
206 \begin{funcdesc
}{setpgid
}{pid, pgrp
}
207 Calls the system call
\cfunction{setpgid()
}. See the
\UNIX{} manual
209 Availability:
\UNIX{}.
212 \begin{funcdesc
}{setreuid
}{ruid, euid
}
213 Set the current process's real and effective user ids.
214 Availability:
\UNIX{}.
217 \begin{funcdesc
}{setregid
}{rgid, egid
}
218 Set the current process's real and effective group ids.
219 Availability:
\UNIX{}.
222 \begin{funcdesc
}{setsid
}{}
223 Calls the system call
\cfunction{setsid()
}. See the
\UNIX{} manual
225 Availability:
\UNIX{}.
228 \begin{funcdesc
}{setuid
}{uid
}
229 \index{user!id, setting
}
230 Set the current process' user id.
231 Availability:
\UNIX{}.
234 % placed in this section since it relates to errno.... a little weak ;-(
235 \begin{funcdesc
}{strerror
}{code
}
236 Return the error message corresponding to the error code in
238 Availability:
\UNIX{}, Windows.
241 \begin{funcdesc
}{umask
}{mask
}
242 Set the current numeric umask and returns the previous umask.
243 Availability:
\UNIX{}, Windows.
246 \begin{funcdesc
}{uname
}{}
247 Return a
5-tuple containing information identifying the current
248 operating system. The tuple contains
5 strings:
249 \code{(
\var{sysname
},
\var{nodename
},
\var{release
},
\var{version
},
250 \var{machine
})
}. Some systems truncate the nodename to
8
251 characters or to the leading component; a better way to get the
252 hostname is
\function{socket.gethostname()
}
253 \withsubitem{(in module socket)
}{\ttindex{gethostname()
}}
255 \withsubitem{(in module socket)
}{\ttindex{gethostbyaddr()
}}
256 \code{socket.gethostbyaddr(socket.gethostname())
}.
257 Availability: recent flavors of
\UNIX{}.
262 \subsection{File Object Creation
\label{os-newstreams
}}
264 These functions create new file objects.
267 \begin{funcdesc
}{fdopen
}{fd
\optional{, mode
\optional{, bufsize
}}}
268 Return an open file object connected to the file descriptor
\var{fd
}.
269 \index{I/O control!buffering
}
270 The
\var{mode
} and
\var{bufsize
} arguments have the same meaning as
271 the corresponding arguments to the built-in
\function{open()
}
273 Availability: Macintosh,
\UNIX{}, Windows.
276 \begin{funcdesc
}{popen
}{command
\optional{, mode
\optional{, bufsize
}}}
277 Open a pipe to or from
\var{command
}. The return value is an open
278 file object connected to the pipe, which can be read or written
279 depending on whether
\var{mode
} is
\code{'r'
} (default) or
\code{'w'
}.
280 The
\var{bufsize
} argument has the same meaning as the corresponding
281 argument to the built-in
\function{open()
} function. The exit status of
282 the command (encoded in the format specified for
\function{wait()
}) is
283 available as the return value of the
\method{close()
} method of the file
284 object, except that when the exit status is zero (termination without
285 errors),
\code{None
} is returned.
286 Availability:
\UNIX{}, Windows.
288 \versionchanged[This function worked unreliably under Windows in
289 earlier versions of Python. This was due to the use of the
290 \cfunction{_popen()
} function from the libraries provided with
291 Windows. Newer versions of Python do not use the broken
292 implementation from the Windows libraries
]{2.0}
295 \begin{funcdesc
}{tmpfile
}{}
296 Return a new file object opened in update mode (
\samp{w+
}). The file
297 has no directory entries associated with it and will be automatically
298 deleted once there are no file descriptors for the file.
299 Availability:
\UNIX{}.
303 For each of these
\function{popen()
} variants, if
\var{bufsize
} is
304 specified, it specifies the buffer size for the I/O pipes.
305 \var{mode
}, if provided, should be the string
\code{'b'
} or
306 \code{'t'
}; on Windows this is needed to determine whether the file
307 objects should be opened in binary or text mode. The default value
308 for
\var{mode
} is
\code{'t'
}.
310 \begin{funcdesc
}{popen2
}{cmd
\optional{, bufsize
\optional{, mode
}}}
311 Executes
\var{cmd
} as a sub-process. Returns the file objects
312 \code{(
\var{child_stdin
},
\var{child_stdout
})
}.
316 \begin{funcdesc
}{popen3
}{cmd
\optional{, bufsize
\optional{, mode
}}}
317 Executes
\var{cmd
} as a sub-process. Returns the file objects
318 \code{(
\var{child_stdin
},
\var{child_stdout
},
\var{child_stderr
})
}.
322 \begin{funcdesc
}{popen4
}{cmd
\optional{, bufsize
\optional{, mode
}}}
323 Executes
\var{cmd
} as a sub-process. Returns the file objects
324 \code{(
\var{child_stdin
},
\var{child_stdout_and_stderr
})
}.
328 This functionality is also available in the
\refmodule{popen2
} module
329 using functions of the same names, but the return values of those
330 functions have a different order.
333 \subsection{File Descriptor Operations
\label{os-fd-ops
}}
335 These functions operate on I/O streams referred to
336 using file descriptors.
339 \begin{funcdesc
}{close
}{fd
}
340 Close file descriptor
\var{fd
}.
341 Availability: Macintosh,
\UNIX{}, Windows.
343 Note: this function is intended for low-level I/O and must be applied
344 to a file descriptor as returned by
\function{open()
} or
345 \function{pipe()
}. To close a ``file object'' returned by the
346 built-in function
\function{open()
} or by
\function{popen()
} or
347 \function{fdopen()
}, use its
\method{close()
} method.
350 \begin{funcdesc
}{dup
}{fd
}
351 Return a duplicate of file descriptor
\var{fd
}.
352 Availability: Macintosh,
\UNIX{}, Windows.
355 \begin{funcdesc
}{dup2
}{fd, fd2
}
356 Duplicate file descriptor
\var{fd
} to
\var{fd2
}, closing the latter
358 Availability:
\UNIX{}, Windows.
361 \begin{funcdesc
}{fpathconf
}{fd, name
}
362 Return system configuration information relevant to an open file.
363 \var{name
} specifies the configuration value to retrieve; it may be a
364 string which is the name of a defined system value; these names are
365 specified in a number of standards (
\POSIX.1, Unix95, Unix98, and
366 others). Some platforms define additional names as well. The names
367 known to the host operating system are given in the
368 \code{pathconf_names
} dictionary. For configuration variables not
369 included in that mapping, passing an integer for
\var{name
} is also
371 Availability:
\UNIX{}.
373 If
\var{name
} is a string and is not known,
\exception{ValueError
} is
374 raised. If a specific value for
\var{name
} is not supported by the
375 host system, even if it is included in
\code{pathconf_names
}, an
376 \exception{OSError
} is raised with
\constant{errno.EINVAL
} for the
380 \begin{funcdesc
}{fstat
}{fd
}
381 Return status for file descriptor
\var{fd
}, like
\function{stat()
}.
382 Availability:
\UNIX{}, Windows.
385 \begin{funcdesc
}{fstatvfs
}{fd
}
386 Return information about the filesystem containing the file associated
387 with file descriptor
\var{fd
}, like
\function{statvfs()
}.
388 Availability:
\UNIX{}.
391 \begin{funcdesc
}{ftruncate
}{fd, length
}
392 Truncate the file corresponding to file descriptor
\var{fd
},
393 so that it is at most
\var{length
} bytes in size.
394 Availability:
\UNIX{}.
397 \begin{funcdesc
}{isatty
}{fd
}
398 Return
\code{1} if the file descriptor
\var{fd
} is open and connected to a
399 tty(-like) device, else
\code{0}.
400 Availability:
\UNIX{}
403 \begin{funcdesc
}{lseek
}{fd, pos, how
}
404 Set the current position of file descriptor
\var{fd
} to position
405 \var{pos
}, modified by
\var{how
}:
\code{0} to set the position
406 relative to the beginning of the file;
\code{1} to set it relative to
407 the current position;
\code{2} to set it relative to the end of the
409 Availability: Macintosh,
\UNIX{}, Windows.
412 \begin{funcdesc
}{open
}{file, flags
\optional{, mode
}}
413 Open the file
\var{file
} and set various flags according to
414 \var{flags
} and possibly its mode according to
\var{mode
}.
415 The default
\var{mode
} is
\code{0777} (octal), and the current umask
416 value is first masked out. Return the file descriptor for the newly
418 Availability: Macintosh,
\UNIX{}, Windows.
420 For a description of the flag and mode values, see the C run-time
421 documentation; flag constants (like
\constant{O_RDONLY
} and
422 \constant{O_WRONLY
}) are defined in this module too (see below).
424 Note: this function is intended for low-level I/O. For normal usage,
425 use the built-in function
\function{open()
}, which returns a ``file
426 object'' with
\method{read()
} and
\method{write()
} methods (and many
430 \begin{funcdesc
}{openpty
}{}
431 Open a new pseudo-terminal pair. Return a pair of file descriptors
432 \code{(
\var{master
},
\var{slave
})
} for the pty and the tty,
433 respectively. For a (slightly) more portable approach, use the
434 \refmodule{pty
}\refstmodindex{pty
} module.
435 Availability: Some flavors of
\UNIX{}
438 \begin{funcdesc
}{pipe
}{}
439 Create a pipe. Return a pair of file descriptors
\code{(
\var{r
},
440 \var{w
})
} usable for reading and writing, respectively.
441 Availability:
\UNIX{}, Windows.
444 \begin{funcdesc
}{read
}{fd, n
}
445 Read at most
\var{n
} bytes from file descriptor
\var{fd
}.
446 Return a string containing the bytes read.
447 Availability: Macintosh,
\UNIX{}, Windows.
449 Note: this function is intended for low-level I/O and must be applied
450 to a file descriptor as returned by
\function{open()
} or
451 \function{pipe()
}. To read a ``file object'' returned by the
452 built-in function
\function{open()
} or by
\function{popen()
} or
453 \function{fdopen()
}, or
\code{sys.stdin
}, use its
454 \method{read()
} or
\method{readline()
} methods.
457 \begin{funcdesc
}{tcgetpgrp
}{fd
}
458 Return the process group associated with the terminal given by
459 \var{fd
} (an open file descriptor as returned by
\function{open()
}).
460 Availability:
\UNIX{}.
463 \begin{funcdesc
}{tcsetpgrp
}{fd, pg
}
464 Set the process group associated with the terminal given by
465 \var{fd
} (an open file descriptor as returned by
\function{open()
})
467 Availability:
\UNIX{}.
470 \begin{funcdesc
}{ttyname
}{fd
}
471 Return a string which specifies the terminal device associated with
472 file-descriptor
\var{fd
}. If
\var{fd
} is not associated with a terminal
473 device, an exception is raised.
474 Availability:
\UNIX{}.
477 \begin{funcdesc
}{write
}{fd, str
}
478 Write the string
\var{str
} to file descriptor
\var{fd
}.
479 Return the number of bytes actually written.
480 Availability: Macintosh,
\UNIX{}, Windows.
482 Note: this function is intended for low-level I/O and must be applied
483 to a file descriptor as returned by
\function{open()
} or
484 \function{pipe()
}. To write a ``file object'' returned by the
485 built-in function
\function{open()
} or by
\function{popen()
} or
486 \function{fdopen()
}, or
\code{sys.stdout
} or
\code{sys.stderr
}, use
487 its
\method{write()
} method.
491 The following data items are available for use in constructing the
492 \var{flags
} parameter to the
\function{open()
} function.
494 \begin{datadesc
}{O_RDONLY
}
498 \dataline{O_NONBLOCK
}
507 Options for the
\var{flag
} argument to the
\function{open()
} function.
508 These can be bit-wise OR'd together.
509 Availability: Macintosh,
\UNIX{}, Windows.
512 \begin{datadesc
}{O_BINARY
}
513 Option for the
\var{flag
} argument to the
\function{open()
} function.
514 This can be bit-wise OR'd together with those listed above.
515 Availability: Macintosh, Windows.
516 % XXX need to check on the availability of this one.
520 \subsection{Files and Directories
\label{os-file-dir
}}
522 \begin{funcdesc
}{access
}{path, mode
}
523 Check read/write/execute permissions for this process or existence of
524 file
\var{path
}.
\var{mode
} should be
\constant{F_OK
} to test the
525 existence of
\var{path
}, or it can be the inclusive OR of one or more
526 of
\constant{R_OK
},
\constant{W_OK
}, and
\constant{X_OK
} to test
527 permissions. Return
\code{1} if access is allowed,
\code{0} if not.
528 See the
\UNIX{} man page
\manpage{access
}{2} for more information.
529 Availability:
\UNIX{}, Windows.
532 \begin{datadesc
}{F_OK
}
533 Value to pass as the
\var{mode
} parameter of
\function{access()
} to
534 test the existence of
\var{path
}.
537 \begin{datadesc
}{R_OK
}
538 Value to include in the
\var{mode
} parameter of
\function{access()
}
539 to test the readability of
\var{path
}.
542 \begin{datadesc
}{W_OK
}
543 Value to include in the
\var{mode
} parameter of
\function{access()
}
544 to test the writability of
\var{path
}.
547 \begin{datadesc
}{X_OK
}
548 Value to include in the
\var{mode
} parameter of
\function{access()
}
549 to determine if
\var{path
} can be executed.
552 \begin{funcdesc
}{chdir
}{path
}
553 \index{directory!changing
}
554 Change the current working directory to
\var{path
}.
555 Availability: Macintosh,
\UNIX{}, Windows.
558 \begin{funcdesc
}{getcwd
}{}
559 Return a string representing the current working directory.
560 Availability: Macintosh,
\UNIX{}, Windows.
563 \begin{funcdesc
}{chmod
}{path, mode
}
564 Change the mode of
\var{path
} to the numeric
\var{mode
}.
565 Availability:
\UNIX{}, Windows.
568 \begin{funcdesc
}{chown
}{path, uid, gid
}
569 Change the owner and group id of
\var{path
} to the numeric
\var{uid
}
571 Availability:
\UNIX{}.
574 \begin{funcdesc
}{link
}{src, dst
}
575 Create a hard link pointing to
\var{src
} named
\var{dst
}.
576 Availability:
\UNIX{}.
579 \begin{funcdesc
}{listdir
}{path
}
580 Return a list containing the names of the entries in the directory.
581 The list is in arbitrary order. It does not include the special
582 entries
\code{'.'
} and
\code{'..'
} even if they are present in the
584 Availability: Macintosh,
\UNIX{}, Windows.
587 \begin{funcdesc
}{lstat
}{path
}
588 Like
\function{stat()
}, but do not follow symbolic links.
589 Availability:
\UNIX{}.
592 \begin{funcdesc
}{mkfifo
}{path
\optional{, mode
}}
593 Create a FIFO (a named pipe) named
\var{path
} with numeric mode
594 \var{mode
}. The default
\var{mode
} is
\code{0666} (octal). The current
595 umask value is first masked out from the mode.
596 Availability:
\UNIX{}.
598 FIFOs are pipes that can be accessed like regular files. FIFOs exist
599 until they are deleted (for example with
\function{os.unlink()
}).
600 Generally, FIFOs are used as rendezvous between ``client'' and
601 ``server'' type processes: the server opens the FIFO for reading, and
602 the client opens it for writing. Note that
\function{mkfifo()
}
603 doesn't open the FIFO --- it just creates the rendezvous point.
606 \begin{funcdesc
}{mkdir
}{path
\optional{, mode
}}
607 Create a directory named
\var{path
} with numeric mode
\var{mode
}.
608 The default
\var{mode
} is
\code{0777} (octal). On some systems,
609 \var{mode
} is ignored. Where it is used, the current umask value is
611 Availability: Macintosh,
\UNIX{}, Windows.
614 \begin{funcdesc
}{makedirs
}{path
\optional{, mode
}}
615 \index{directory!creating
}
616 Recursive directory creation function. Like
\function{mkdir()
},
617 but makes all intermediate-level directories needed to contain the
618 leaf directory. Throws an
\exception{error
} exception if the leaf
619 directory already exists or cannot be created. The default
\var{mode
}
620 is
\code{0777} (octal).
624 \begin{funcdesc
}{pathconf
}{path, name
}
625 Return system configuration information relevant to a named file.
626 \var{name
} specifies the configuration value to retrieve; it may be a
627 string which is the name of a defined system value; these names are
628 specified in a number of standards (
\POSIX.1, Unix95, Unix98, and
629 others). Some platforms define additional names as well. The names
630 known to the host operating system are given in the
631 \code{pathconf_names
} dictionary. For configuration variables not
632 included in that mapping, passing an integer for
\var{name
} is also
634 Availability:
\UNIX{}.
636 If
\var{name
} is a string and is not known,
\exception{ValueError
} is
637 raised. If a specific value for
\var{name
} is not supported by the
638 host system, even if it is included in
\code{pathconf_names
}, an
639 \exception{OSError
} is raised with
\constant{errno.EINVAL
} for the
643 \begin{datadesc
}{pathconf_names
}
644 Dictionary mapping names accepted by
\function{pathconf()
} and
645 \function{fpathconf()
} to the integer values defined for those names
646 by the host operating system. This can be used to determine the set
647 of names known to the system.
651 \begin{funcdesc
}{readlink
}{path
}
652 Return a string representing the path to which the symbolic link
654 Availability:
\UNIX{}.
657 \begin{funcdesc
}{remove
}{path
}
658 Remove the file
\var{path
}. See
\function{rmdir()
} below to remove a
659 directory. This is identical to the
\function{unlink()
} function
661 Availability: Macintosh,
\UNIX{}, Windows.
664 \begin{funcdesc
}{removedirs
}{path
}
665 \index{directory!deleting
}
666 Recursive directory removal function. Works like
667 \function{rmdir()
} except that, if the leaf directory is
668 successfully removed, directories corresponding to rightmost path
669 segments will be pruned way until either the whole path is consumed or
670 an error is raised (which is ignored, because it generally means that
671 a parent directory is not empty). Throws an
\exception{error
}
672 exception if the leaf directory could not be successfully removed.
676 \begin{funcdesc
}{rename
}{src, dst
}
677 Rename the file or directory
\var{src
} to
\var{dst
}.
678 Availability: Macintosh,
\UNIX{}, Windows.
681 \begin{funcdesc
}{renames
}{old, new
}
682 Recursive directory or file renaming function.
683 Works like
\function{rename()
}, except creation of any intermediate
684 directories needed to make the new pathname good is attempted first.
685 After the rename, directories corresponding to rightmost path segments
686 of the old name will be pruned away using
\function{removedirs()
}.
688 Note: this function can fail with the new directory structure made if
689 you lack permissions needed to remove the leaf directory or file.
693 \begin{funcdesc
}{rmdir
}{path
}
694 Remove the directory
\var{path
}.
695 Availability: Macintosh,
\UNIX{}, Windows.
698 \begin{funcdesc
}{stat
}{path
}
699 Perform a
\cfunction{stat()
} system call on the given path. The
700 return value is a tuple of at least
10 integers giving the most
701 important (and portable) members of the
\emph{stat
} structure, in the
713 More items may be added at the end by some implementations. Note that
714 on the Macintosh, the time values are floating point values, like all
715 time values on the Macintosh.
716 (On MS Windows, some items are filled with dummy values.)
717 Availability: Macintosh,
\UNIX{}, Windows.
719 Note: The standard module
\refmodule{stat
}\refstmodindex{stat
} defines
720 functions and constants that are useful for extracting information
721 from a
\ctype{stat
} structure.
724 \begin{funcdesc
}{statvfs
}{path
}
725 Perform a
\cfunction{statvfs()
} system call on the given path. The
726 return value is a tuple of
10 integers giving the most common
727 members of the
\ctype{statvfs
} structure, in the order
738 Availability:
\UNIX{}.
740 Note: The standard module
\module{statvfs
}\refstmodindex{statvfs
}
741 defines constants that are useful for extracting information
742 from a
\ctype{statvfs
} structure.
745 \begin{funcdesc
}{symlink
}{src, dst
}
746 Create a symbolic link pointing to
\var{src
} named
\var{dst
}.
747 Availability:
\UNIX{}.
750 \begin{funcdesc
}{tempnam
}{\optional{dir
\optional{, prefix
}}}
751 Return a unique path name that is reasonable for creating a temporary
752 file. This will be an absolute path that names a potential directory
753 entry in the directory
\var{dir
} or a common location for temporary
754 files if
\var{dir
} is omitted or
\code{None
}. If given and not
755 \code{None
},
\var{prefix
} is used to provide a short prefix to the
756 filename. Applications are responsible for properly creating and
757 managing files created using paths returned by
\function{tempnam()
};
758 no automatic cleanup is provided.
761 \begin{funcdesc
}{tmpnam
}{}
762 Return a unique path name that is reasonable for creating a temporary
763 file. This will be an absolute path that names a potential directory
764 entry in a common location for temporary files. Applications are
765 responsible for properly creating and managing files created using
766 paths returned by
\function{tmpnam()
}; no automatic cleanup is
770 \begin{datadesc
}{TMP_MAX
}
771 The maximum number of unique names that
\function{tmpnam()
} will
772 generate before reusing names.
775 \begin{funcdesc
}{unlink
}{path
}
776 Remove the file
\var{path
}. This is the same function as
777 \function{remove()
}; the
\function{unlink()
} name is its traditional
779 Availability: Macintosh,
\UNIX{}, Windows.
782 \begin{funcdesc
}{utime
}{path, times
}
783 Set the access and modified times of the file specified by
\var{path
}.
784 If
\var{times
} is
\code{None
}, then the file's access and modified
785 times are set to the current time. Otherwise,
\var{times
} must be a
786 2-tuple of numbers, of the form
\code{(
\var{atime
},
\var{mtime
})
}
787 which is used to set the access and modified times, respectively.
788 \versionchanged[Added support for
\code{None
} for
\var{times
}]{2.0}
789 Availability: Macintosh,
\UNIX{}, Windows.
793 \subsection{Process Management
\label{os-process
}}
795 These functions may be used to create and manage processes.
797 The various
\function{exec*()
} functions take a list of arguments for
798 the new program loaded into the process. In each case, the first of
799 these arguments is passed to the new program as its own name rather
800 than as an argument a user may have typed on a command line. For the
801 C programmer, this is the
\code{argv
[0]} passed to a program's
802 \cfunction{main()
}. For example,
\samp{os.execv('/bin/echo',
['foo',
803 'bar'
])
} will only print
\samp{bar
} on standard output;
\samp{foo
}
804 will seem to be ignored.
807 \begin{funcdesc
}{abort
}{}
808 Generate a
\constant{SIGABRT
} signal to the current process. On
809 \UNIX, the default behavior is to produce a core dump; on Windows, the
810 process immediately returns an exit code of
\code{3}. Be aware that
811 programs which use
\function{signal.signal()
} to register a handler
812 for
\constant{SIGABRT
} will behave differently.
813 Availability:
\UNIX, Windows.
816 \begin{funcdesc
}{execl
}{path, arg0, arg1, ...
}
817 This is equivalent to
818 \samp{execv(
\var{path
}, (
\var{arg0
},
\var{arg1
}, ...))
}.
819 Availability:
\UNIX{}, Windows.
822 \begin{funcdesc
}{execle
}{path, arg0, arg1, ..., env
}
823 This is equivalent to
824 \samp{execve(
\var{path
}, (
\var{arg0
},
\var{arg1
}, ...),
\var{env
})
}.
825 Availability:
\UNIX{}, Windows.
828 \begin{funcdesc
}{execlp
}{path, arg0, arg1, ...
}
829 This is equivalent to
830 \samp{execvp(
\var{path
}, (
\var{arg0
},
\var{arg1
}, ...))
}.
831 Availability:
\UNIX{}, Windows.
834 \begin{funcdesc
}{execv
}{path, args
}
835 Execute the executable
\var{path
} with argument list
\var{args
},
836 replacing the current process (i.e., the Python interpreter).
837 The argument list may be a tuple or list of strings.
838 Availability:
\UNIX{}, Windows.
841 \begin{funcdesc
}{execve
}{path, args, env
}
842 Execute the executable
\var{path
} with argument list
\var{args
},
843 and environment
\var{env
},
844 replacing the current process (i.e., the Python interpreter).
845 The argument list may be a tuple or list of strings.
846 The environment must be a dictionary mapping strings to strings.
847 Availability:
\UNIX{}, Windows.
850 \begin{funcdesc
}{execvp
}{path, args
}
851 This is like
\samp{execv(
\var{path
},
\var{args
})
} but duplicates
852 the shell's actions in searching for an executable file in a list of
853 directories. The directory list is obtained from
854 \code{environ
['PATH'
]}.
855 Availability:
\UNIX{}, Windows.
858 \begin{funcdesc
}{execvpe
}{path, args, env
}
859 This is a cross between
\function{execve()
} and
\function{execvp()
}.
860 The directory list is obtained from
\code{\var{env
}['PATH'
]}.
861 Availability:
\UNIX{}, Windows.
864 \begin{funcdesc
}{_exit
}{n
}
865 Exit to the system with status
\var{n
}, without calling cleanup
866 handlers, flushing stdio buffers, etc.
867 Availability:
\UNIX{}, Windows.
869 Note: the standard way to exit is
\code{sys.exit(
\var{n
})
}.
870 \function{_exit()
} should normally only be used in the child process
871 after a
\function{fork()
}.
874 \begin{funcdesc
}{fork
}{}
875 Fork a child process. Return
\code{0} in the child, the child's
876 process id in the parent.
877 Availability:
\UNIX{}.
880 \begin{funcdesc
}{forkpty
}{}
881 Fork a child process, using a new pseudo-terminal as the child's
882 controlling terminal. Return a pair of
\code{(
\var{pid
},
\var{fd
})
},
883 where
\var{pid
} is
\code{0} in the child, the new child's process id
884 in the parent, and
\code{fd
} is the file descriptor of the master end
885 of the pseudo-terminal. For a more portable approach, use the
886 \refmodule{pty
} module.
887 Availability: Some flavors of
\UNIX{}
890 \begin{funcdesc
}{kill
}{pid, sig
}
891 \index{process!killing
}
892 \index{process!signalling
}
893 Kill the process
\var{pid
} with signal
\var{sig
}.
894 Availability:
\UNIX{}.
897 \begin{funcdesc
}{nice
}{increment
}
898 Add
\var{increment
} to the process's ``niceness''. Return the new
900 Availability:
\UNIX{}.
903 \begin{funcdesc
}{plock
}{op
}
904 Lock program segments into memory. The value of
\var{op
}
905 (defined in
\code{<sys/lock.h>
}) determines which segments are locked.
906 Availability:
\UNIX{}.
909 \begin{funcdesc
}{spawnv
}{mode, path, args
}
910 Execute the program
\var{path
} in a new process, passing the arguments
911 specified in
\var{args
} as command-line parameters.
\var{args
} may be
912 a list or a tuple.
\var{mode
} is a magic operational constant. See
913 the Visual
\Cpp{} Runtime Library documentation for further
914 information; the constants are exposed to the Python programmer as
916 Availability:
\UNIX{}, Windows.
920 \begin{funcdesc
}{spawnve
}{mode, path, args, env
}
921 Execute the program
\var{path
} in a new process, passing the arguments
922 specified in
\var{args
} as command-line parameters and the contents of
923 the mapping
\var{env
} as the environment.
\var{args
} may be a list or
924 a tuple.
\var{mode
} is a magic operational constant. See the Visual
925 \Cpp{} Runtime Library documentation for further information; the
926 constants are exposed to the Python programmer as listed below.
927 Availability:
\UNIX{}, Windows.
931 \begin{datadesc
}{P_WAIT
}
934 Possible values for the
\var{mode
} parameter to
\function{spawnv()
}
935 and
\function{spawnve()
}.
936 Availability:
\UNIX{}, Windows.
940 \begin{datadesc
}{P_OVERLAY
}
942 Possible values for the
\var{mode
} parameter to
\function{spawnv()
}
943 and
\function{spawnve()
}. These are less portable than those listed
945 Availability: Windows.
949 \begin{funcdesc
}{startfile
}{path
}
950 Start a file with its associated application. This acts like
951 double-clicking the file in Windows Explorer, or giving the file name
952 as an argument to the DOS
\program{start
} command: the file is opened
953 with whatever application (if any) its extension is associated.
955 \function{startfile()
} returns as soon as the associated application
956 is launched. There is no option to wait for the application to close,
957 and no way to retrieve the application's exit status. The
\var{path
}
958 parameter is relative to the current directory. If you want to use an
959 absolute path, make sure the first character is not a slash
960 (
\character{/
}); the underlying Win32
\cfunction{ShellExecute()
}
961 function doesn't work it is. Use the
\function{os.path.normpath()
}
962 function to ensure that the path is properly encoded for Win32.
963 Availability: Windows.
967 \begin{funcdesc
}{system
}{command
}
968 Execute the command (a string) in a subshell. This is implemented by
969 calling the Standard C function
\cfunction{system()
}, and has the
970 same limitations. Changes to
\code{posix.environ
},
\code{sys.stdin
},
971 etc.\ are not reflected in the environment of the executed command.
972 The return value is the exit status of the process encoded in the
973 format specified for
\function{wait()
}, except on Windows
95 and
98,
974 where it is always
\code{0}. Note that
\POSIX{} does not specify the
975 meaning of the return value of the C
\cfunction{system()
} function,
976 so the return value of the Python function is system-dependent.
977 Availability:
\UNIX{}, Windows.
980 \begin{funcdesc
}{times
}{}
981 Return a
5-tuple of floating point numbers indicating accumulated (CPU
983 times, in seconds. The items are: user time, system time, children's
984 user time, children's system time, and elapsed real time since a fixed
985 point in the past, in that order. See the
\UNIX{} manual page
986 \manpage{times
}{2} or the corresponding Windows Platform API
988 Availability:
\UNIX{}, Windows.
991 \begin{funcdesc
}{wait
}{}
992 Wait for completion of a child process, and return a tuple containing
993 its pid and exit status indication: a
16-bit number, whose low byte is
994 the signal number that killed the process, and whose high byte is the
995 exit status (if the signal number is zero); the high bit of the low
996 byte is set if a core file was produced.
997 Availability:
\UNIX{}.
1000 \begin{funcdesc
}{waitpid
}{pid, options
}
1001 Wait for completion of a child process given by process id
\var{pid
},
1002 and return a tuple containing its process id and exit status
1003 indication (encoded as for
\function{wait()
}). The semantics of the
1004 call are affected by the value of the integer
\var{options
}, which
1005 should be
\code{0} for normal operation.
1006 Availability:
\UNIX{}.
1008 If
\var{pid
} is greater than
\code{0},
\function{waitpid()
} requests
1009 status information for that specific process. If
\var{pid
} is
1010 \code{0}, the request is for the status of any child in the process
1011 group of the current process. If
\var{pid
} is
\code{-
1}, the request
1012 pertains to any child of the current process. If
\var{pid
} is less
1013 than
\code{-
1}, status is requested for any process in the process
1014 group
\code{-
\var{pid
}} (the absolute value of
\var{pid
}).
1017 \begin{datadesc
}{WNOHANG
}
1018 The option for
\function{waitpid()
} to avoid hanging if no child
1019 process status is available immediately.
1020 Availability:
\UNIX{}.
1023 The following functions take a process status code as returned by
1024 \function{system()
},
\function{wait()
}, or
\function{waitpid()
} as a
1025 parameter. They may be used to determine the disposition of a
1028 \begin{funcdesc
}{WIFSTOPPED
}{status
}
1029 Return true if the process has been stopped.
1030 Availability:
\UNIX{}.
1033 \begin{funcdesc
}{WIFSIGNALED
}{status
}
1034 Return true if the process exited due to a signal.
1035 Availability:
\UNIX{}.
1038 \begin{funcdesc
}{WIFEXITED
}{status
}
1039 Return true if the process exited using the
\manpage{exit
}{2} system
1041 Availability:
\UNIX{}.
1044 \begin{funcdesc
}{WEXITSTATUS
}{status
}
1045 If
\code{WIFEXITED(
\var{status
})
} is true, return the integer
1046 parameter to the
\manpage{exit
}{2} system call. Otherwise, the return
1047 value is meaningless.
1048 Availability:
\UNIX{}.
1051 \begin{funcdesc
}{WSTOPSIG
}{status
}
1052 Return the signal which caused the process to stop.
1053 Availability:
\UNIX{}.
1056 \begin{funcdesc
}{WTERMSIG
}{status
}
1057 Return the signal which caused the process to exit.
1058 Availability:
\UNIX{}.
1062 \subsection{Miscellaneous System Information
\label{os-path
}}
1065 \begin{funcdesc
}{confstr
}{name
}
1066 Return string-valued system configuration values.
1067 \var{name
} specifies the configuration value to retrieve; it may be a
1068 string which is the name of a defined system value; these names are
1069 specified in a number of standards (
\POSIX, Unix95, Unix98, and
1070 others). Some platforms define additional names as well. The names
1071 known to the host operating system are given in the
1072 \code{confstr_names
} dictionary. For configuration variables not
1073 included in that mapping, passing an integer for
\var{name
} is also
1075 Availability:
\UNIX{}.
1077 If the configuration value specified by
\var{name
} isn't defined, the
1078 empty string is returned.
1080 If
\var{name
} is a string and is not known,
\exception{ValueError
} is
1081 raised. If a specific value for
\var{name
} is not supported by the
1082 host system, even if it is included in
\code{confstr_names
}, an
1083 \exception{OSError
} is raised with
\constant{errno.EINVAL
} for the
1087 \begin{datadesc
}{confstr_names
}
1088 Dictionary mapping names accepted by
\function{confstr()
} to the
1089 integer values defined for those names by the host operating system.
1090 This can be used to determine the set of names known to the system.
1091 Availability:
\UNIX.
1094 \begin{funcdesc
}{sysconf
}{name
}
1095 Return integer-valued system configuration values.
1096 If the configuration value specified by
\var{name
} isn't defined,
1097 \code{-
1} is returned. The comments regarding the
\var{name
}
1098 parameter for
\function{confstr()
} apply here as well; the dictionary
1099 that provides information on the known names is given by
1100 \code{sysconf_names
}.
1101 Availability:
\UNIX{}.
1104 \begin{datadesc
}{sysconf_names
}
1105 Dictionary mapping names accepted by
\function{sysconf()
} to the
1106 integer values defined for those names by the host operating system.
1107 This can be used to determine the set of names known to the system.
1108 Availability:
\UNIX.
1112 The follow data values are used to support path manipulation
1113 operations. These are defined for all platforms.
1115 Higher-level operations on pathnames are defined in the
1116 \refmodule{os.path
} module.
1119 \begin{datadesc
}{curdir
}
1120 The constant string used by the OS to refer to the current directory,
1121 e.g.\
\code{'.'
} for
\POSIX{} or
\code{':'
} for the Macintosh.
1124 \begin{datadesc
}{pardir
}
1125 The constant string used by the OS to refer to the parent directory,
1126 e.g.\
\code{'..'
} for
\POSIX{} or
\code{'::'
} for the Macintosh.
1129 \begin{datadesc
}{sep
}
1130 The character used by the OS to separate pathname components,
1131 e.g.\
\character{/
} for
\POSIX{} or
\character{:
} for the Macintosh.
1132 Note that knowing this is not sufficient to be able to parse or
1133 concatenate pathnames --- use
\function{os.path.split()
} and
1134 \function{os.path.join()
} --- but it is occasionally useful.
1137 \begin{datadesc
}{altsep
}
1138 An alternative character used by the OS to separate pathname components,
1139 or
\code{None
} if only one separator character exists. This is set to
1140 \character{/
} on DOS and Windows systems where
\code{sep
} is a backslash.
1143 \begin{datadesc
}{pathsep
}
1144 The character conventionally used by the OS to separate search patch
1145 components (as in
\envvar{PATH
}), e.g.\
\character{:
} for
\POSIX{} or
1146 \character{;
} for DOS and Windows.
1149 \begin{datadesc
}{defpath
}
1150 The default search path used by
\function{exec*p*()
} if the environment
1151 doesn't have a
\code{'PATH'
} key.
1154 \begin{datadesc
}{linesep
}
1155 The string used to separate (or, rather, terminate) lines on the
1156 current platform. This may be a single character,
1157 e.g.\
\code{'
\e n'
} for
\POSIX{} or
\code{'
\e r'
} for MacOS, or multiple
1158 characters, e.g.\
\code{'
\e r
\e n'
} for MS-DOS and MS Windows.