14.1.2 File Object Creation

These functions create new file objects.

fdopen( fd[, mode[, bufsize]])
Return an open file object connected to the file descriptor fd. The mode and bufsize arguments have the same meaning as the corresponding arguments to the built-in open() function. Availability: Macintosh, Unix, Windows.

Changed in version 2.3: When specified, the mode argument must now start with one of the letters "r", "w", or "a", otherwise a ValueError is raised. Changed in version 2.5: On Unix, when the mode argument starts with "a", the O_APPEND flag is set on the file descriptor (which the fdopen() implementation already does on most platforms).

popen( command[, mode[, bufsize]])
Open a pipe to or from command. The return value is an open file object connected to the pipe, which can be read or written depending on whether mode is 'r' (default) or 'w'. The bufsize argument has the same meaning as the corresponding argument to the built-in open() function. The exit status of the command (encoded in the format specified for wait()) is available as the return value of the close() method of the file object, except that when the exit status is zero (termination without errors), None is returned. Availability: Macintosh, Unix, Windows.

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function.

Changed in version 2.0: This function worked unreliably under Windows in earlier versions of Python. This was due to the use of the _popen() function from the libraries provided with Windows. Newer versions of Python do not use the broken implementation from the Windows libraries.

tmpfile( )
Return a new file object opened in update mode ("w+b"). The file has no directory entries associated with it and will be automatically deleted once there are no file descriptors for the file. Availability: Macintosh, Unix, Windows.

There are a number of different popen*() functions that provide slightly different ways to create subprocesses. Note that the subprocess module is easier to use and more powerful; consider using that module before writing code using the lower-level popen*() functions.

For each of the popen*() variants, if bufsize is specified, it specifies the buffer size for the I/O pipes. mode, if provided, should be the string 'b' or 't'; on Windows this is needed to determine whether the file objects should be opened in binary or text mode. The default value for mode is 't'.

Also, for each of these variants, on Unix, cmd may be a sequence, in which case arguments will be passed directly to the program without shell intervention (as with os.spawnv()). If cmd is a string it will be passed to the shell (as with os.system()).

These methods do not make it possible to retrieve the exit status from the child processes. The only way to control the input and output streams and also retrieve the return codes is to use the Popen3 and Popen4 classes from the popen2 module; these are only available on Unix.

For a discussion of possible deadlock conditions related to the use of these functions, see ``Flow Control Issues'' (section 17.4.2).

popen2( cmd[, mode[, bufsize]])
Executes cmd as a sub-process. Returns the file objects (child_stdin, child_stdout). Availability: Macintosh, Unix, Windows. New in version 2.0.

popen3( cmd[, mode[, bufsize]])
Executes cmd as a sub-process. Returns the file objects (child_stdin, child_stdout, child_stderr). Availability: Macintosh, Unix, Windows. New in version 2.0.

popen4( cmd[, mode[, bufsize]])
Executes cmd as a sub-process. Returns the file objects (child_stdin, child_stdout_and_stderr). Availability: Macintosh, Unix, Windows. New in version 2.0.

(Note that child_stdin, child_stdout, and child_stderr are named from the point of view of the child process, so child_stdin is the child's standard input.)

This functionality is also available in the popen2 module using functions of the same names, but the return values of those functions have a different order.

See About this document... for information on suggesting changes.