<!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>The File object — Django 1.4.20 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: '1.4.20', 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> <link rel="top" title="Django 1.4.20 documentation" href="../../index.html" /> <link rel="up" title="File handling" href="index.html" /> <link rel="next" title="File storage API" href="storage.html" /> <link rel="prev" title="File handling" href="index.html" /> <script type="text/javascript" src="../../templatebuiltins.js"></script> <script type="text/javascript"> (function($) { if (!django_template_builtins) { // templatebuiltins.js missing, do nothing. return; } $(document).ready(function() { // Hyperlink Django template tags and filters var base = "../templates/builtins.html"; if (base == "#") { // Special case for builtins.html itself base = ""; } // Tags are keywords, class '.k' $("div.highlight\\-html\\+django span.k").each(function(i, elem) { var tagname = $(elem).text(); if ($.inArray(tagname, django_template_builtins.ttags) != -1) { var fragment = tagname.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>"); } }); // Filters are functions, class '.nf' $("div.highlight\\-html\\+django span.nf").each(function(i, elem) { var filtername = $(elem).text(); if ($.inArray(filtername, django_template_builtins.tfilters) != -1) { var fragment = filtername.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>"); } }); }); })(jQuery); </script> </head> <body> <div class="document"> <div id="custom-doc" class="yui-t6"> <div id="hd"> <h1><a href="../../index.html">Django 1.4.20 documentation</a></h1> <div id="global-nav"> <a title="Home page" href="../../index.html">Home</a> | <a title="Table of contents" href="../../contents.html">Table of contents</a> | <a title="Global index" href="../../genindex.html">Index</a> | <a title="Module index" href="../../py-modindex.html">Modules</a> </div> <div class="nav"> « <a href="index.html" title="File handling">previous</a> | <a href="../index.html" title="API Reference" accesskey="U">up</a> | <a href="storage.html" title="File storage API">next</a> »</div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="yui-g" id="ref-files-file"> <div class="section" id="s-the-file-object"> <span id="the-file-object"></span><h1>The <tt class="docutils literal"><span class="pre">File</span></tt> object<a class="headerlink" href="#the-file-object" title="Permalink to this headline">¶</a></h1> <p>The <a class="reference internal" href="index.html#module-django.core.files" title="django.core.files: File handling and storage"><tt class="xref py py-mod docutils literal"><span class="pre">django.core.files</span></tt></a> module and its submodules contain built-in classes for basic file handling in Django.</p> <div class="section" id="s-the-file-class"> <span id="the-file-class"></span><h2>The <tt class="docutils literal"><span class="pre">File</span></tt> Class<a class="headerlink" href="#the-file-class" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="django.core.files.File"> <em class="property">class </em><tt class="descname">File</tt>(<em>file_object</em>)<a class="headerlink" href="#django.core.files.File" title="Permalink to this definition">¶</a></dt> <dd><p>The <a class="reference internal" href="#django.core.files.File" title="django.core.files.File"><tt class="xref py py-class docutils literal"><span class="pre">File</span></tt></a> is a thin wrapper around Python’s built-in file object with some Django-specific additions. Internally, Django uses this class any time it needs to represent a file.</p> <p><a class="reference internal" href="#django.core.files.File" title="django.core.files.File"><tt class="xref py py-class docutils literal"><span class="pre">File</span></tt></a> objects have the following attributes and methods:</p> <dl class="attribute"> <dt id="django.core.files.File.name"> <tt class="descname">name</tt><a class="headerlink" href="#django.core.files.File.name" title="Permalink to this definition">¶</a></dt> <dd><p>The name of file including the relative path from <a class="reference internal" href="../settings.html#std:setting-MEDIA_ROOT"><tt class="xref std std-setting docutils literal"><span class="pre">MEDIA_ROOT</span></tt></a>.</p> </dd></dl> <dl class="attribute"> <dt id="django.core.files.File.size"> <tt class="descname">size</tt><a class="headerlink" href="#django.core.files.File.size" title="Permalink to this definition">¶</a></dt> <dd><p>The size of the file in bytes.</p> </dd></dl> <dl class="attribute"> <dt id="django.core.files.File.file"> <tt class="descname">file</tt><a class="headerlink" href="#django.core.files.File.file" title="Permalink to this definition">¶</a></dt> <dd><p>The underlying Python <tt class="docutils literal"><span class="pre">file</span></tt> object passed to <a class="reference internal" href="#django.core.files.File" title="django.core.files.File"><tt class="xref py py-class docutils literal"><span class="pre">File</span></tt></a>.</p> </dd></dl> <dl class="attribute"> <dt id="django.core.files.File.mode"> <tt class="descname">mode</tt><a class="headerlink" href="#django.core.files.File.mode" title="Permalink to this definition">¶</a></dt> <dd><p>The read/write mode for the file.</p> </dd></dl> <dl class="method"> <dt id="django.core.files.File.open"> <tt class="descname">open</tt>(<span class="optional">[</span><em>mode=None</em><span class="optional">]</span>)<a class="headerlink" href="#django.core.files.File.open" title="Permalink to this definition">¶</a></dt> <dd><p>Open or reopen the file (which by definition also does <tt class="docutils literal"><span class="pre">File.seek(0)</span></tt>). The <tt class="docutils literal"><span class="pre">mode</span></tt> argument allows the same values as Python’s standard <tt class="docutils literal"><span class="pre">open()</span></tt>.</p> <p>When reopening a file, <tt class="docutils literal"><span class="pre">mode</span></tt> will override whatever mode the file was originally opened with; <tt class="docutils literal"><span class="pre">None</span></tt> means to reopen with the original mode.</p> </dd></dl> <dl class="method"> <dt id="django.core.files.File.read"> <tt class="descname">read</tt>(<span class="optional">[</span><em>num_bytes=None</em><span class="optional">]</span>)<a class="headerlink" href="#django.core.files.File.read" title="Permalink to this definition">¶</a></dt> <dd><p>Read content from the file. The optional <tt class="docutils literal"><span class="pre">size</span></tt> is the number of bytes to read; if not specified, the file will be read to the end.</p> </dd></dl> <dl class="method"> <dt id="django.core.files.File.__iter__"> <tt class="descname">__iter__</tt>()<a class="headerlink" href="#django.core.files.File.__iter__" title="Permalink to this definition">¶</a></dt> <dd><p>Iterate over the file yielding one line at a time.</p> </dd></dl> <dl class="method"> <dt id="django.core.files.File.chunks"> <tt class="descname">chunks</tt>(<span class="optional">[</span><em>chunk_size=None</em><span class="optional">]</span>)<a class="headerlink" href="#django.core.files.File.chunks" title="Permalink to this definition">¶</a></dt> <dd><p>Iterate over the file yielding “chunks” of a given size. <tt class="docutils literal"><span class="pre">chunk_size</span></tt> defaults to 64 KB.</p> <p>This is especially useful with very large files since it allows them to be streamed off disk and avoids storing the whole file in memory.</p> </dd></dl> <dl class="method"> <dt id="django.core.files.File.multiple_chunks"> <tt class="descname">multiple_chunks</tt>(<span class="optional">[</span><em>chunk_size=None</em><span class="optional">]</span>)<a class="headerlink" href="#django.core.files.File.multiple_chunks" title="Permalink to this definition">¶</a></dt> <dd><p>Returns <tt class="docutils literal"><span class="pre">True</span></tt> if the file is large enough to require multiple chunks to access all of its content give some <tt class="docutils literal"><span class="pre">chunk_size</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="django.core.files.File.write"> <tt class="descname">write</tt>(<span class="optional">[</span><em>content</em><span class="optional">]</span>)<a class="headerlink" href="#django.core.files.File.write" title="Permalink to this definition">¶</a></dt> <dd><p>Writes the specified content string to the file. Depending on the storage system behind the scenes, this content might not be fully committed until <tt class="docutils literal"><span class="pre">close()</span></tt> is called on the file.</p> </dd></dl> <dl class="method"> <dt id="django.core.files.File.close"> <tt class="descname">close</tt>()<a class="headerlink" href="#django.core.files.File.close" title="Permalink to this definition">¶</a></dt> <dd><p>Close the file.</p> </dd></dl> <p>In addition to the listed methods, <a class="reference internal" href="#django.core.files.File" title="django.core.files.File"><tt class="xref py py-class docutils literal"><span class="pre">File</span></tt></a> exposes the following attributes and methods of the underlying <tt class="docutils literal"><span class="pre">file</span></tt> object: <tt class="docutils literal"><span class="pre">encoding</span></tt>, <tt class="docutils literal"><span class="pre">fileno</span></tt>, <tt class="docutils literal"><span class="pre">flush</span></tt>, <tt class="docutils literal"><span class="pre">isatty</span></tt>, <tt class="docutils literal"><span class="pre">newlines</span></tt>, <tt class="docutils literal"><span class="pre">read</span></tt>, <tt class="docutils literal"><span class="pre">readinto</span></tt>, <tt class="docutils literal"><span class="pre">readlines</span></tt>, <tt class="docutils literal"><span class="pre">seek</span></tt>, <tt class="docutils literal"><span class="pre">softspace</span></tt>, <tt class="docutils literal"><span class="pre">tell</span></tt>, <tt class="docutils literal"><span class="pre">truncate</span></tt>, <tt class="docutils literal"><span class="pre">writelines</span></tt>, <tt class="docutils literal"><span class="pre">xreadlines</span></tt>.</p> </dd></dl> </div> <div class="section" id="s-the-contentfile-class"> <span id="the-contentfile-class"></span><h2>The <tt class="docutils literal"><span class="pre">ContentFile</span></tt> Class<a class="headerlink" href="#the-contentfile-class" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="django.core.files.base.ContentFile"> <em class="property">class </em><tt class="descname">ContentFile</tt>(<em>File</em>)<a class="headerlink" href="#django.core.files.base.ContentFile" title="Permalink to this definition">¶</a></dt> <dd><p>The <tt class="docutils literal"><span class="pre">ContentFile</span></tt> class inherits from <a class="reference internal" href="#django.core.files.File" title="django.core.files.File"><tt class="xref py py-class docutils literal"><span class="pre">File</span></tt></a>, but unlike <a class="reference internal" href="#django.core.files.File" title="django.core.files.File"><tt class="xref py py-class docutils literal"><span class="pre">File</span></tt></a> it operates on string content, rather than an actual file. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.core.files.base</span> <span class="kn">import</span> <span class="n">ContentFile</span> <span class="n">f1</span> <span class="o">=</span> <span class="n">ContentFile</span><span class="p">(</span><span class="s">"my string content"</span><span class="p">)</span> <span class="n">f2</span> <span class="o">=</span> <span class="n">ContentFile</span><span class="p">(</span><span class="s">u"my unicode content encoded as UTF-8"</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">'UTF-8'</span><span class="p">))</span> </pre></div> </div> </dd></dl> </div> <div class="section" id="s-the-imagefile-class"> <span id="the-imagefile-class"></span><h2>The <tt class="docutils literal"><span class="pre">ImageFile</span></tt> Class<a class="headerlink" href="#the-imagefile-class" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="django.core.files.images.ImageFile"> <em class="property">class </em><tt class="descname">ImageFile</tt>(<em>file_object</em>)<a class="headerlink" href="#django.core.files.images.ImageFile" title="Permalink to this definition">¶</a></dt> <dd><p>Django provides a built-in class specifically for images. <a class="reference internal" href="#django.core.files.images.ImageFile" title="django.core.files.images.ImageFile"><tt class="xref py py-class docutils literal"><span class="pre">django.core.files.images.ImageFile</span></tt></a> inherits all the attributes and methods of <a class="reference internal" href="#django.core.files.File" title="django.core.files.File"><tt class="xref py py-class docutils literal"><span class="pre">File</span></tt></a>, and additionally provides the following:</p> <dl class="attribute"> <dt id="django.core.files.images.ImageFile.width"> <tt class="descname">width</tt><a class="headerlink" href="#django.core.files.images.ImageFile.width" title="Permalink to this definition">¶</a></dt> <dd><p>Width of the image in pixels.</p> </dd></dl> <dl class="attribute"> <dt id="django.core.files.images.ImageFile.height"> <tt class="descname">height</tt><a class="headerlink" href="#django.core.files.images.ImageFile.height" title="Permalink to this definition">¶</a></dt> <dd><p>Height of the image in pixels.</p> </dd></dl> </dd></dl> </div> <div class="section" id="s-additional-methods-on-files-attached-to-objects"> <span id="additional-methods-on-files-attached-to-objects"></span><h2>Additional methods on files attached to objects<a class="headerlink" href="#additional-methods-on-files-attached-to-objects" title="Permalink to this headline">¶</a></h2> <p>Any <a class="reference internal" href="#django.core.files.File" title="django.core.files.File"><tt class="xref py py-class docutils literal"><span class="pre">File</span></tt></a> that’s associated with an object (as with <tt class="docutils literal"><span class="pre">Car.photo</span></tt>, below) will also have a couple of extra methods:</p> <dl class="method"> <dt id="django.core.files.File.save"> <tt class="descclassname">File.</tt><tt class="descname">save</tt>(<em>name</em>, <em>content</em><span class="optional">[</span>, <em>save=True</em><span class="optional">]</span>)<a class="headerlink" href="#django.core.files.File.save" title="Permalink to this definition">¶</a></dt> <dd><p>Saves a new file with the file name and contents provided. This will not replace the existing file, but will create a new file and update the object to point to it. If <tt class="docutils literal"><span class="pre">save</span></tt> is <tt class="docutils literal"><span class="pre">True</span></tt>, the model’s <tt class="docutils literal"><span class="pre">save()</span></tt> method will be called once the file is saved. That is, these two lines:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">car</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="s">'myphoto.jpg'</span><span class="p">,</span> <span class="n">content</span><span class="p">,</span> <span class="n">save</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">car</span><span class="o">.</span><span class="n">save</span><span class="p">()</span> </pre></div> </div> <p>are the same as this one line:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">car</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="s">'myphoto.jpg'</span><span class="p">,</span> <span class="n">content</span><span class="p">,</span> <span class="n">save</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> </pre></div> </div> <p>Note that the <tt class="docutils literal"><span class="pre">content</span></tt> argument must be an instance of either <a class="reference internal" href="#django.core.files.File" title="django.core.files.File"><tt class="xref py py-class docutils literal"><span class="pre">File</span></tt></a> or of a subclass of <a class="reference internal" href="#django.core.files.File" title="django.core.files.File"><tt class="xref py py-class docutils literal"><span class="pre">File</span></tt></a>, such as <tt class="xref py py-class docutils literal"><span class="pre">ContentFile</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="django.core.files.File.delete"> <tt class="descclassname">File.</tt><tt class="descname">delete</tt>(<span class="optional">[</span><em>save=True</em><span class="optional">]</span>)<a class="headerlink" href="#django.core.files.File.delete" title="Permalink to this definition">¶</a></dt> <dd><p>Removes the file from the model instance and deletes the underlying file. If <tt class="docutils literal"><span class="pre">save</span></tt> is <tt class="docutils literal"><span class="pre">True</span></tt>, the model’s <tt class="docutils literal"><span class="pre">save()</span></tt> method will be called once the file is deleted.</p> </dd></dl> </div> </div> </div> </div> </div> <div class="yui-b" id="sidebar"> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">The <tt class="docutils literal"><span class="pre">File</span></tt> object</a><ul> <li><a class="reference internal" href="#the-file-class">The <tt class="docutils literal"><span class="pre">File</span></tt> Class</a></li> <li><a class="reference internal" href="#the-contentfile-class">The <tt class="docutils literal"><span class="pre">ContentFile</span></tt> Class</a></li> <li><a class="reference internal" href="#the-imagefile-class">The <tt class="docutils literal"><span class="pre">ImageFile</span></tt> Class</a></li> <li><a class="reference internal" href="#additional-methods-on-files-attached-to-objects">Additional methods on files attached to objects</a></li> </ul> </li> </ul> <h3>Browse</h3> <ul> <li>Prev: <a href="index.html">File handling</a></li> <li>Next: <a href="storage.html">File storage API</a></li> </ul> <h3>You are here:</h3> <ul> <li> <a href="../../index.html">Django 1.4.20 documentation</a> <ul><li><a href="../index.html">API Reference</a> <ul><li><a href="index.html">File handling</a> <ul><li>The <tt class="docutils literal"><span class="pre">File</span></tt> object</li></ul> </li></ul></li></ul> </li> </ul> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../../_sources/ref/files/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" /> <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> <h3>Last update:</h3> <p class="topless">Mar 19, 2015</p> </div> </div> <div id="ft"> <div class="nav"> « <a href="index.html" title="File handling">previous</a> | <a href="../index.html" title="API Reference" accesskey="U">up</a> | <a href="storage.html" title="File storage API">next</a> »</div> </div> </div> <div class="clearer"></div> </div> </body> </html>