<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<link rel=
"STYLESHEET" href=
"lib.css" type='text/css'
/>
<link rel=
"SHORTCUT ICON" href=
"../icons/pyfav.png" type=
"image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index'
/>
<link rel=
"first" href=
"lib.html" title='Python Library Reference'
/>
<link rel='contents' href='contents.html'
title=
"Contents" />
<link rel='index' href='genindex.html' title='Index'
/>
<link rel='last' href='about.html' title='About this document...'
/>
<link rel='help' href='about.html' title='About this document...'
/>
<link rel=
"next" href=
"os-file-dir.html" />
<link rel=
"prev" href=
"os-newstreams.html" />
<link rel=
"parent" href=
"module-os.html" />
<link rel=
"next" href=
"os-file-dir.html" />
<meta name='aesop' content='information'
/>
<title>6.1.3 File Descriptor Operations
</title>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"6.1.2 File Object Creation"
href=
"os-newstreams.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"6.1 os "
href=
"module-os.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"6.1.4 Files and Directories"
href=
"os-file-dir.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"os-newstreams.html">6.1.2 File Object Creation
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"module-os.html">6.1 os
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"os-file-dir.html">6.1.4 Files and Directories
</A>
<!--End of Navigation Panel-->
<H2><A NAME=
"SECTION008130000000000000000"></A><A NAME=
"os-fd-ops"></A>
6.1.3 File Descriptor Operations
These functions operate on I/O streams referred to
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1558' xml:id='l2h-
1558'
class=
"function">close
</tt></b>(
</nobr></td>
<td><var>fd
</var>)
</td></tr></table></dt>
Close file descriptor
<var>fd
</var>.
Availability: Macintosh,
<span class=
"Unix">Unix
</span>, Windows.
<div class=
"note"><b class=
"label">Note:
</b>
This function is intended for low-level I/O and must be applied
to a file descriptor as returned by
<tt class=
"function">open()
</tt> or
<tt class=
"function">pipe()
</tt>. To close a ``file object'' returned by the
built-in function
<tt class=
"function">open()
</tt> or by
<tt class=
"function">popen()
</tt> or
<tt class=
"function">fdopen()
</tt>, use its
<tt class=
"method">close()
</tt> method.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1559' xml:id='l2h-
1559'
class=
"function">dup
</tt></b>(
</nobr></td>
<td><var>fd
</var>)
</td></tr></table></dt>
Return a duplicate of file descriptor
<var>fd
</var>.
Availability: Macintosh,
<span class=
"Unix">Unix
</span>, Windows.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1560' xml:id='l2h-
1560'
class=
"function">dup2
</tt></b>(
</nobr></td>
<td><var>fd, fd2
</var>)
</td></tr></table></dt>
Duplicate file descriptor
<var>fd
</var> to
<var>fd2
</var>, closing the latter
Availability: Macintosh,
<span class=
"Unix">Unix
</span>, Windows.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1561' xml:id='l2h-
1561'
class=
"function">fdatasync
</tt></b>(
</nobr></td>
<td><var>fd
</var>)
</td></tr></table></dt>
Force write of file with filedescriptor
<var>fd
</var> to disk.
Does not force update of metadata.
Availability:
<span class=
"Unix">Unix
</span>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1562' xml:id='l2h-
1562'
class=
"function">fpathconf
</tt></b>(
</nobr></td>
<td><var>fd, name
</var>)
</td></tr></table></dt>
Return system configuration information relevant to an open file.
<var>name
</var> specifies the configuration value to retrieve; it may be a
string which is the name of a defined system value; these names are
specified in a number of standards (POSIX
.1,
<span class=
"Unix">Unix
</span> 95,
<span class=
"Unix">Unix
</span> 98, and
others). Some platforms define additional names as well. The names
known to the host operating system are given in the
<code>pathconf_names
</code> dictionary. For configuration variables not
included in that mapping, passing an integer for
<var>name
</var> is also
Availability: Macintosh,
<span class=
"Unix">Unix
</span>.
If
<var>name
</var> is a string and is not known,
<tt class=
"exception">ValueError
</tt> is
raised. If a specific value for
<var>name
</var> is not supported by the
host system, even if it is included in
<code>pathconf_names
</code>, an
<tt class=
"exception">OSError
</tt> is raised with
<tt class=
"constant">errno.EINVAL
</tt> for the
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1563' xml:id='l2h-
1563'
class=
"function">fstat
</tt></b>(
</nobr></td>
<td><var>fd
</var>)
</td></tr></table></dt>
Return status for file descriptor
<var>fd
</var>, like
<tt class=
"function">stat()
</tt>.
Availability: Macintosh,
<span class=
"Unix">Unix
</span>, Windows.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1564' xml:id='l2h-
1564'
class=
"function">fstatvfs
</tt></b>(
</nobr></td>
<td><var>fd
</var>)
</td></tr></table></dt>
Return information about the filesystem containing the file associated
with file descriptor
<var>fd
</var>, like
<tt class=
"function">statvfs()
</tt>.
Availability:
<span class=
"Unix">Unix
</span>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1565' xml:id='l2h-
1565'
class=
"function">fsync
</tt></b>(
</nobr></td>
<td><var>fd
</var>)
</td></tr></table></dt>
Force write of file with filedescriptor
<var>fd
</var> to disk. On
<span class=
"Unix">Unix
</span>,
this calls the native
<tt class=
"cfunction">fsync()
</tt> function; on Windows, the
MS
<tt class=
"cfunction">_commit()
</tt> function.
If you're starting with a Python file object
<var>f
</var>, first do
<code><var>f
</var>.flush()
</code>, and then do
<code>os.fsync(
<var>f
</var>.fileno())
</code>,
to ensure that all internal buffers associated with
<var>f
</var> are written
Availability: Macintosh,
<span class=
"Unix">Unix
</span>, and Windows starting in
2.2.3.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1566' xml:id='l2h-
1566'
class=
"function">ftruncate
</tt></b>(
</nobr></td>
<td><var>fd, length
</var>)
</td></tr></table></dt>
Truncate the file corresponding to file descriptor
<var>fd
</var>,
so that it is at most
<var>length
</var> bytes in size.
Availability: Macintosh,
<span class=
"Unix">Unix
</span>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1567' xml:id='l2h-
1567'
class=
"function">isatty
</tt></b>(
</nobr></td>
<td><var>fd
</var>)
</td></tr></table></dt>
Return
<code>True
</code> if the file descriptor
<var>fd
</var> is open and
connected to a tty(-like) device, else
<code>False
</code>.
Availability: Macintosh,
<span class=
"Unix">Unix
</span>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1568' xml:id='l2h-
1568'
class=
"function">lseek
</tt></b>(
</nobr></td>
<td><var>fd, pos, how
</var>)
</td></tr></table></dt>
Set the current position of file descriptor
<var>fd
</var> to position
<var>pos
</var>, modified by
<var>how
</var>:
<code>0</code> to set the position
relative to the beginning of the file;
<code>1</code> to set it relative to
the current position;
<code>2</code> to set it relative to the end of the
Availability: Macintosh,
<span class=
"Unix">Unix
</span>, Windows.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1569' xml:id='l2h-
1569'
class=
"function">open
</tt></b>(
</nobr></td>
<td><var>file, flags
</var><big>[
</big><var>, mode
</var><big>]
</big><var></var>)
</td></tr></table></dt>
Open the file
<var>file
</var> and set various flags according to
<var>flags
</var> and possibly its mode according to
<var>mode
</var>.
The default
<var>mode
</var> is
<code>0777</code> (octal), and the current umask
value is first masked out. Return the file descriptor for the newly
Availability: Macintosh,
<span class=
"Unix">Unix
</span>, Windows.
For a description of the flag and mode values, see the C run-time
documentation; flag constants (like
<tt class=
"constant">O_RDONLY
</tt> and
<tt class=
"constant">O_WRONLY
</tt>) are defined in this module too (see below).
<div class=
"note"><b class=
"label">Note:
</b>
This function is intended for low-level I/O. For normal usage,
use the built-in function
<tt class=
"function">open()
</tt>, which returns a ``file
object'' with
<tt class=
"method">read()
</tt> and
<tt class=
"method">write()
</tt> methods (and many
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1570' xml:id='l2h-
1570'
class=
"function">openpty
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
Open a new pseudo-terminal pair. Return a pair of file descriptors
<code>(
<var>master
</var>,
<var>slave
</var>)
</code> for the pty and the tty,
respectively. For a (slightly) more portable approach, use the
<tt class=
"module"><a href=
"module-pty.html">pty
</a></tt><a id='l2h-
1581' xml:id='l2h-
1581'
></a> module.
Availability: Macintosh, Some flavors of
<span class=
"Unix">Unix
</span>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1571' xml:id='l2h-
1571'
class=
"function">pipe
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
Create a pipe. Return a pair of file descriptors
<code>(
<var>r
</var>,
<var>w
</var>)
</code> usable for reading and writing, respectively.
Availability: Macintosh,
<span class=
"Unix">Unix
</span>, Windows.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1572' xml:id='l2h-
1572'
class=
"function">read
</tt></b>(
</nobr></td>
<td><var>fd, n
</var>)
</td></tr></table></dt>
Read at most
<var>n
</var> bytes from file descriptor
<var>fd
</var>.
Return a string containing the bytes read. If the end of the file
referred to by
<var>fd
</var> has been reached, an empty string is
Availability: Macintosh,
<span class=
"Unix">Unix
</span>, Windows.
<div class=
"note"><b class=
"label">Note:
</b>
This function is intended for low-level I/O and must be applied
to a file descriptor as returned by
<tt class=
"function">open()
</tt> or
<tt class=
"function">pipe()
</tt>. To read a ``file object'' returned by the
built-in function
<tt class=
"function">open()
</tt> or by
<tt class=
"function">popen()
</tt> or
<tt class=
"function">fdopen()
</tt>, or
<code>sys.stdin
</code>, use its
<tt class=
"method">read()
</tt> or
<tt class=
"method">readline()
</tt> methods.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1573' xml:id='l2h-
1573'
class=
"function">tcgetpgrp
</tt></b>(
</nobr></td>
<td><var>fd
</var>)
</td></tr></table></dt>
Return the process group associated with the terminal given by
<var>fd
</var> (an open file descriptor as returned by
<tt class=
"function">open()
</tt>).
Availability: Macintosh,
<span class=
"Unix">Unix
</span>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1574' xml:id='l2h-
1574'
class=
"function">tcsetpgrp
</tt></b>(
</nobr></td>
<td><var>fd, pg
</var>)
</td></tr></table></dt>
Set the process group associated with the terminal given by
<var>fd
</var> (an open file descriptor as returned by
<tt class=
"function">open()
</tt>)
Availability: Macintosh,
<span class=
"Unix">Unix
</span>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1575' xml:id='l2h-
1575'
class=
"function">ttyname
</tt></b>(
</nobr></td>
<td><var>fd
</var>)
</td></tr></table></dt>
Return a string which specifies the terminal device associated with
file-descriptor
<var>fd
</var>. If
<var>fd
</var> is not associated with a terminal
device, an exception is raised.
Availability:Macintosh,
<span class=
"Unix">Unix
</span>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1576' xml:id='l2h-
1576'
class=
"function">write
</tt></b>(
</nobr></td>
<td><var>fd, str
</var>)
</td></tr></table></dt>
Write the string
<var>str
</var> to file descriptor
<var>fd
</var>.
Return the number of bytes actually written.
Availability: Macintosh,
<span class=
"Unix">Unix
</span>, Windows.
<div class=
"note"><b class=
"label">Note:
</b>
This function is intended for low-level I/O and must be applied
to a file descriptor as returned by
<tt class=
"function">open()
</tt> or
<tt class=
"function">pipe()
</tt>. To write a ``file object'' returned by the
built-in function
<tt class=
"function">open()
</tt> or by
<tt class=
"function">popen()
</tt> or
<tt class=
"function">fdopen()
</tt>, or
<code>sys.stdout
</code> or
<code>sys.stderr
</code>, use
its
<tt class=
"method">write()
</tt> method.
The following data items are available for use in constructing the
<var>flags
</var> parameter to the
<tt class=
"function">open()
</tt> function.
<dl><dt><b><tt id='l2h-
1577' xml:id='l2h-
1577'
>O_RDONLY
</tt></b></dt>
<dt><b><tt id='l2h-
1582' xml:id='l2h-
1582'
>O_WRONLY
</tt></b></dt><dd>
<dt><b><tt id='l2h-
1583' xml:id='l2h-
1583'
>O_RDWR
</tt></b></dt><dd>
<dt><b><tt id='l2h-
1584' xml:id='l2h-
1584'
>O_APPEND
</tt></b></dt><dd>
<dt><b><tt id='l2h-
1585' xml:id='l2h-
1585'
>O_CREAT
</tt></b></dt><dd>
<dt><b><tt id='l2h-
1586' xml:id='l2h-
1586'
>O_EXCL
</tt></b></dt><dd>
<dt><b><tt id='l2h-
1587' xml:id='l2h-
1587'
>O_TRUNC
</tt></b></dt><dd>
Options for the
<var>flag
</var> argument to the
<tt class=
"function">open()
</tt> function.
These can be bit-wise OR'd together.
Availability: Macintosh,
<span class=
"Unix">Unix
</span>, Windows.
<dl><dt><b><tt id='l2h-
1578' xml:id='l2h-
1578'
>O_DSYNC
</tt></b></dt>
<dt><b><tt id='l2h-
1588' xml:id='l2h-
1588'
>O_RSYNC
</tt></b></dt><dd>
<dt><b><tt id='l2h-
1589' xml:id='l2h-
1589'
>O_SYNC
</tt></b></dt><dd>
<dt><b><tt id='l2h-
1590' xml:id='l2h-
1590'
>O_NDELAY
</tt></b></dt><dd>
<dt><b><tt id='l2h-
1591' xml:id='l2h-
1591'
>O_NONBLOCK
</tt></b></dt><dd>
<dt><b><tt id='l2h-
1592' xml:id='l2h-
1592'
>O_NOCTTY
</tt></b></dt><dd>
More options for the
<var>flag
</var> argument to the
<tt class=
"function">open()
</tt> function.
Availability: Macintosh,
<span class=
"Unix">Unix
</span>.
<dl><dt><b><tt id='l2h-
1579' xml:id='l2h-
1579'
>O_BINARY
</tt></b></dt>
Option for the
<var>flag
</var> argument to the
<tt class=
"function">open()
</tt> function.
This can be bit-wise OR'd together with those listed above.
<dl><dt><b><tt id='l2h-
1580' xml:id='l2h-
1580'
>O_NOINHERIT
</tt></b></dt>
<dt><b><tt id='l2h-
1593' xml:id='l2h-
1593'
>O_SHORT_LIVED
</tt></b></dt><dd>
<dt><b><tt id='l2h-
1594' xml:id='l2h-
1594'
>O_TEMPORARY
</tt></b></dt><dd>
<dt><b><tt id='l2h-
1595' xml:id='l2h-
1595'
>O_RANDOM
</tt></b></dt><dd>
<dt><b><tt id='l2h-
1596' xml:id='l2h-
1596'
>O_SEQUENTIAL
</tt></b></dt><dd>
<dt><b><tt id='l2h-
1597' xml:id='l2h-
1597'
>O_TEXT
</tt></b></dt><dd>
Options for the
<var>flag
</var> argument to the
<tt class=
"function">open()
</tt> function.
These can be bit-wise OR'd together.
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"6.1.2 File Object Creation"
href=
"os-newstreams.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"6.1 os "
href=
"module-os.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"6.1.4 Files and Directories"
href=
"os-file-dir.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"os-newstreams.html">6.1.2 File Object Creation
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"module-os.html">6.1 os
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"os-file-dir.html">6.1.4 Files and Directories
</A>
<span class=
"release-info">Release
2.4.2, documentation updated on
28 September
2005.
</span>
<!--End of Navigation Panel-->
See
<i><a href=
"about.html">About this document...
</a></i> for information on suggesting changes.