<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>File Objects — Python v3.1.1 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '3.1.1', COLLAPSE_MODINDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python v3.1.1 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python v3.1.1 documentation" href="../index.html" /> <link rel="up" title="Concrete Objects Layer" href="concrete.html" /> <link rel="next" title="Module Objects" href="module.html" /> <link rel="prev" title="Instance Method Objects" href="method.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../modindex.html" title="Global Module Index" accesskey="M">modules</a> |</li> <li class="right" > <a href="module.html" title="Module Objects" accesskey="N">next</a> |</li> <li class="right" > <a href="method.html" title="Instance Method Objects" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="../index.html">Python v3.1.1 documentation</a> »</li> <li><a href="index.html" >Python/C API Reference Manual</a> »</li> <li><a href="concrete.html" accesskey="U">Concrete Objects Layer</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="file-objects"> <span id="fileobjects"></span><h1>File Objects<a class="headerlink" href="#file-objects" title="Permalink to this headline">¶</a></h1> <p id="index-19">Python’s built-in file objects are implemented entirely on the <tt class="xref docutils literal"><span class="pre">FILE*</span></tt> support from the C standard library. This is an implementation detail and may change in future releases of Python. The <tt class="docutils literal"><span class="pre">PyFile_</span></tt> APIs are a wrapper over the <a title="Core tools for working with streams." class="reference external" href="../library/io.html#module-io"><tt class="xref docutils literal"><span class="pre">io</span></tt></a> module.</p> <dl class="cfunction"> <dt id="PyFile_FromFd"> <tt class="descname">PyFile_FromFd</tt><big>(</big>int<em> fd</em>, char<em> *name</em>, char<em> *mode</em>, int<em> buffering</em>, char<em> *encoding</em>, char<em> *newline</em>, int<em> closefd</em><big>)</big><a class="headerlink" href="#PyFile_FromFd" title="Permalink to this definition">¶</a></dt> <dd><p>Create a new <tt class="xref docutils literal"><span class="pre">PyFileObject</span></tt> from the file descriptor of an already opened file <em>fd</em>. The arguments <em>name</em>, <em>encoding</em> and <em>newline</em> can be <em>NULL</em> to use the defaults; <em>buffering</em> can be <em>-1</em> to use the default. Return <em>NULL</em> on failure.</p> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">Take care when you are mixing streams and descriptors! For more information, see <a class="reference external" href="http://www.gnu.org/software/libc/manual/html_node/Stream_002fDescriptor-Precautions.html#Stream_002fDescriptor-Precautions">the GNU C Library docs</a>.</p> </div> </dd></dl> <dl class="cfunction"> <dt id="PyObject_AsFileDescriptor"> int <tt class="descname">PyObject_AsFileDescriptor</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *p</em><big>)</big><a class="headerlink" href="#PyObject_AsFileDescriptor" title="Permalink to this definition">¶</a></dt> <dd>Return the file descriptor associated with <em>p</em> as an <tt class="xref docutils literal"><span class="pre">int</span></tt>. If the object is an integer, its value is returned. If not, the object’s <tt class="xref docutils literal"><span class="pre">fileno()</span></tt> method is called if it exists; the method must return an integer, which is returned as the file descriptor value. Sets an exception and returns <tt class="docutils literal"><span class="pre">-1</span></tt> on failure.</dd></dl> <dl class="cfunction"> <dt id="PyFile_GetLine"> <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyFile_GetLine</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *p</em>, int<em> n</em><big>)</big><a class="headerlink" href="#PyFile_GetLine" title="Permalink to this definition">¶</a></dt> <dd><em class="refcount">Return value: New reference.</em><p id="index-20">Equivalent to <tt class="docutils literal"><span class="pre">p.readline([n])</span></tt>, this function reads one line from the object <em>p</em>. <em>p</em> may be a file object or any object with a <tt class="xref docutils literal"><span class="pre">readline()</span></tt> method. If <em>n</em> is <tt class="docutils literal"><span class="pre">0</span></tt>, exactly one line is read, regardless of the length of the line. If <em>n</em> is greater than <tt class="docutils literal"><span class="pre">0</span></tt>, no more than <em>n</em> bytes will be read from the file; a partial line can be returned. In both cases, an empty string is returned if the end of the file is reached immediately. If <em>n</em> is less than <tt class="docutils literal"><span class="pre">0</span></tt>, however, one line is read regardless of length, but <a title="exceptions.EOFError" class="reference external" href="../library/exceptions.html#exceptions.EOFError"><tt class="xref docutils literal"><span class="pre">EOFError</span></tt></a> is raised if the end of the file is reached immediately.</p> </dd></dl> <dl class="cfunction"> <dt id="PyFile_WriteObject"> int <tt class="descname">PyFile_WriteObject</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *obj</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *p</em>, int<em> flags</em><big>)</big><a class="headerlink" href="#PyFile_WriteObject" title="Permalink to this definition">¶</a></dt> <dd><p id="index-21">Write object <em>obj</em> to file object <em>p</em>. The only supported flag for <em>flags</em> is <tt class="xref docutils literal"><span class="pre">Py_PRINT_RAW</span></tt>; if given, the <a title="str" class="reference external" href="../library/functions.html#str"><tt class="xref docutils literal"><span class="pre">str()</span></tt></a> of the object is written instead of the <a title="repr" class="reference external" href="../library/functions.html#repr"><tt class="xref docutils literal"><span class="pre">repr()</span></tt></a>. Return <tt class="docutils literal"><span class="pre">0</span></tt> on success or <tt class="docutils literal"><span class="pre">-1</span></tt> on failure; the appropriate exception will be set.</p> </dd></dl> <dl class="cfunction"> <dt id="PyFile_WriteString"> int <tt class="descname">PyFile_WriteString</tt><big>(</big>const char<em> *s</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *p</em><big>)</big><a class="headerlink" href="#PyFile_WriteString" title="Permalink to this definition">¶</a></dt> <dd>Write string <em>s</em> to file object <em>p</em>. Return <tt class="docutils literal"><span class="pre">0</span></tt> on success or <tt class="docutils literal"><span class="pre">-1</span></tt> on failure; the appropriate exception will be set.</dd></dl> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h4>Previous topic</h4> <p class="topless"><a href="method.html" title="previous chapter">Instance Method Objects</a></p> <h4>Next topic</h4> <p class="topless"><a href="module.html" title="next chapter">Module Objects</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/c-api/file.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" size="18" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../modindex.html" title="Global Module Index" >modules</a> |</li> <li class="right" > <a href="module.html" title="Module Objects" >next</a> |</li> <li class="right" > <a href="method.html" title="Instance Method Objects" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="../index.html">Python v3.1.1 documentation</a> »</li> <li><a href="index.html" >Python/C API Reference Manual</a> »</li> <li><a href="concrete.html" >Concrete Objects Layer</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2009, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Aug 16, 2009. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.2. </div> </body> </html>