<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>6.1.3 File Descriptor Operations </TITLE>
<META NAME="description" CONTENT="6.1.3 File Descriptor Operations ">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="lib.css">
<LINK REL="next" HREF="os-file-dir.html">
<LINK REL="previous" HREF="os-newstreams.html">
<LINK REL="up" HREF="module-os.html">
<LINK REL="next" HREF="os-file-dir.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="os-file-dir.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="module-os.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="os-newstreams.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="os-file-dir.html">6.1.4 Files and Directories</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="module-os.html">6.1 os  </A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="os-newstreams.html">6.1.2 File Object Creation</A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H2><A NAME="SECTION008130000000000000000"></A>
<BR>
6.1.3 File Descriptor Operations 
</H2>

<P>
These functions operate on I/O streams referred to
using file descriptors.

<P>
<dl><dt><b><a name="l2h-893"><tt class=function>close</tt></a></b> (<var>fd</var>)
<dd>
Close file descriptor <var>fd</var>.
Availability: Macintosh, Unix, Windows.

<P>
Note: 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>

<P>
<dl><dt><b><a name="l2h-894"><tt class=function>dup</tt></a></b> (<var>fd</var>)
<dd>
Return a duplicate of file descriptor <var>fd</var>.
Availability: Macintosh, Unix, Windows.
</dl>

<P>
<dl><dt><b><a name="l2h-895"><tt class=function>dup2</tt></a></b> (<var>fd, fd2</var>)
<dd>
Duplicate file descriptor <var>fd</var> to <var>fd2</var>, closing the latter
first if necessary.
Availability: Unix, Windows.
</dl>

<P>
<dl><dt><b><a name="l2h-896"><tt class=function>fstat</tt></a></b> (<var>fd</var>)
<dd>
Return status for file descriptor <var>fd</var>, like <tt class=function>stat()</tt>.
Availability: Unix, Windows.
</dl>

<P>
<dl><dt><b><a name="l2h-897"><tt class=function>fstatvfs</tt></a></b> (<var>fd</var>)
<dd>
Return information about the filesystem containing the file associated
with file descriptor <var>fd</var>, like <tt class=function>statvfs()</tt>.
Availability: Unix.
</dl>

<P>
<dl><dt><b><a name="l2h-898"><tt class=function>ftruncate</tt></a></b> (<var>fd, length</var>)
<dd>
Truncate the file corresponding to file descriptor <var>fd</var>, 
so that it is at most <var>length</var> bytes in size.
Availability: Unix.
</dl>

<P>
<dl><dt><b><a name="l2h-899"><tt class=function>lseek</tt></a></b> (<var>fd, pos, how</var>)
<dd>
Set the current position of file descriptor <var>fd</var> to position
<var>pos</var>, modified by <var>how</var>: <tt>0</tt> to set the position
relative to the beginning of the file; <tt>1</tt> to set it relative to
the current position; <tt>2</tt> to set it relative to the end of the
file.
Availability: Macintosh, Unix, Windows.
</dl>

<P>
<dl><dt><b><a name="l2h-900"><tt class=function>open</tt></a></b> (<var>file, flags</var><big>[</big><var>, mode</var><big>]</big>)
<dd>
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 <tt>0777</tt> (octal), and the current umask
value is first masked out.  Return the file descriptor for the newly
opened file.
Availability: Macintosh, Unix, Windows.

<P>
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).

<P>
Note: 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
more).
</dl>

<P>
<dl><dt><b><a name="l2h-901"><tt class=function>pipe</tt></a></b> ()
<dd>
Create a pipe.  Return a pair of file descriptors <tt>(<var>r</var>,
<var>w</var>)</tt> usable for reading and writing, respectively.
Availability: Unix, Windows.
</dl>

<P>
<dl><dt><b><a name="l2h-902"><tt class=function>read</tt></a></b> (<var>fd, n</var>)
<dd>
Read at most <var>n</var> bytes from file descriptor <var>fd</var>.
Return a string containing the bytes read.
Availability: Macintosh, Unix, Windows.

<P>
Note: 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 <tt>sys.stdin</tt>, use its
<tt class=method>read()</tt> or <tt class=method>readline()</tt> methods.
</dl>

<P>
<dl><dt><b><a name="l2h-903"><tt class=function>tcgetpgrp</tt></a></b> (<var>fd</var>)
<dd>
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: Unix.
</dl>

<P>
<dl><dt><b><a name="l2h-904"><tt class=function>tcsetpgrp</tt></a></b> (<var>fd, pg</var>)
<dd>
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>)
to <var>pg</var>.
Availability: Unix.
</dl>

<P>
<dl><dt><b><a name="l2h-905"><tt class=function>ttyname</tt></a></b> (<var>fd</var>)
<dd>
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: Unix.
</dl>

<P>
<dl><dt><b><a name="l2h-906"><tt class=function>write</tt></a></b> (<var>fd, str</var>)
<dd>
Write the string <var>str</var> to file descriptor <var>fd</var>.
Return the number of bytes actually written.
Availability: Macintosh, Unix, Windows.

<P>
Note: 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 <tt>sys.stdout</tt> or <tt>sys.stderr</tt>, use
its <tt class=method>write()</tt> method.
</dl>

<P>
The following data items are available for use in constructing the
<var>flags</var> parameter to the <tt class=function>open()</tt> function.

<P>
<dl><dt><b><a name="l2h-907"><tt>O_RDONLY</tt></a></b>
<dd>
<dt><b><a name="l2h-908"><tt>O_WRONLY</tt></a></b><dd>
<dt><b><a name="l2h-909"><tt>O_RDWR</tt></a></b><dd>
<dt><b><a name="l2h-910"><tt>O_NDELAY</tt></a></b><dd>
<dt><b><a name="l2h-911"><tt>O_NONBLOCK</tt></a></b><dd>
<dt><b><a name="l2h-912"><tt>O_APPEND</tt></a></b><dd>
<dt><b><a name="l2h-913"><tt>O_DSYNC</tt></a></b><dd>
<dt><b><a name="l2h-914"><tt>O_RSYNC</tt></a></b><dd>
<dt><b><a name="l2h-915"><tt>O_SYNC</tt></a></b><dd>
<dt><b><a name="l2h-916"><tt>O_NOCTTY</tt></a></b><dd>
<dt><b><a name="l2h-917"><tt>O_CREAT</tt></a></b><dd>
<dt><b><a name="l2h-918"><tt>O_EXCL</tt></a></b><dd>
<dt><b><a name="l2h-919"><tt>O_TRUNC</tt></a></b><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, Unix, Windows.
</dl>

<P>
<p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="os-file-dir.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="module-os.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="os-newstreams.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="os-file-dir.html">6.1.4 Files and Directories</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="module-os.html">6.1 os  </A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="os-newstreams.html">6.1.2 File Object Creation</A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
