<!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.3.0 documentation</title> <link rel="stylesheet" href="../_static/pydoctheme.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.3.0', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python v3.3.0 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.3.0 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" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </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="../py-modindex.html" title="Python Module Index" >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="http://www.python.org/">Python</a> »</li> <li><a href="../index.html">3.3.0 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-0">These APIs are a minimal emulation of the Python 2 C API for built-in file objects, which used to rely on the buffered I/O (<tt class="xref c c-type docutils literal"><span class="pre">FILE*</span></tt>) support from the C standard library. In Python 3, files and streams use the new <a class="reference internal" href="../library/io.html#module-io" title="io: Core tools for working with streams."><tt class="xref py py-mod docutils literal"><span class="pre">io</span></tt></a> module, which defines several layers over the low-level unbuffered I/O of the operating system. The functions described below are convenience C wrappers over these new APIs, and meant mostly for internal error reporting in the interpreter; third-party code is advised to access the <a class="reference internal" href="../library/io.html#module-io" title="io: Core tools for working with streams."><tt class="xref py py-mod docutils literal"><span class="pre">io</span></tt></a> APIs instead.</p> <dl class="function"> <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> *errors</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 Python file object from the file descriptor of an already opened file <em>fd</em>. The arguments <em>name</em>, <em>encoding</em>, <em>errors</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. <em>name</em> is ignored and kept for backward compatibility. Return <em>NULL</em> on failure. For a more comprehensive description of the arguments, please refer to the <a class="reference internal" href="../library/io.html#io.open" title="io.open"><tt class="xref py py-func docutils literal"><span class="pre">io.open()</span></tt></a> function documentation.</p> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">Since Python streams have their own buffering layer, mixing them with OS-level file descriptors can produce various issues (such as unexpected ordering of data).</p> </div> <p class="versionchanged"> <span class="versionmodified">Changed in version 3.2:</span> Ignore <em>name</em> attribute.</p> </dd></dl> <dl class="function"> <dt id="PyObject_AsFileDescriptor"> int <tt class="descname">PyObject_AsFileDescriptor</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em> *p</em><big>)</big><a class="headerlink" href="#PyObject_AsFileDescriptor" title="Permalink to this definition">¶</a></dt> <dd><p>Return the file descriptor associated with <em>p</em> as an <tt class="xref c c-type 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 py py-meth 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.</p> </dd></dl> <dl class="function"> <dt id="PyFile_GetLine"> <a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a>* <tt class="descname">PyFile_GetLine</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="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-1">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 <a class="reference internal" href="../library/readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><tt class="xref py py-meth docutils literal"><span class="pre">readline()</span></tt></a> 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 class="reference internal" href="../library/exceptions.html#EOFError" title="EOFError"><tt class="xref py py-exc 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="function"> <dt id="PyFile_WriteObject"> int <tt class="descname">PyFile_WriteObject</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em> *obj</em>, <a class="reference internal" href="structures.html#PyObject" title="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-2">Write object <em>obj</em> to file object <em>p</em>. The only supported flag for <em>flags</em> is <tt class="xref py py-const docutils literal"><span class="pre">Py_PRINT_RAW</span></tt>; if given, the <a class="reference internal" href="../library/functions.html#str" title="str"><tt class="xref py py-func docutils literal"><span class="pre">str()</span></tt></a> of the object is written instead of the <a class="reference internal" href="../library/functions.html#repr" title="repr"><tt class="xref py py-func 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="function"> <dt id="PyFile_WriteString"> int <tt class="descname">PyFile_WriteString</tt><big>(</big>const char<em> *s</em>, <a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em> *p</em><big>)</big><a class="headerlink" href="#PyFile_WriteString" title="Permalink to this definition">¶</a></dt> <dd><p>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.</p> </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="../bugs.html">Report a Bug</a></li> <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="../py-modindex.html" title="Python 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="http://www.python.org/">Python</a> »</li> <li><a href="../index.html">3.3.0 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-2012, 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 Sep 29, 2012. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7. </div> </body> </html>