{"title":"The File object","version":"2.1","locale":"es","docname":"ref/files/file","url":"/es/2.1/ref/files/file/","canonical":"https://djangodocs.dev/es/2.1/ref/files/file/","summary":"The django.core.files module and its submodules contain built-in classes for basic file handling in Django.","html":"<h1>The <code class=\"docutils literal notranslate\">File</code> object<a class=\"heading-anchor\" href=\"#the-file-object\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>The <a class=\"reference internal\" href=\"/es/2.1/ref/files/#module-django.core.files\" title=\"django.core.files: File handling and storage\"><code class=\"xref py py-mod docutils literal notranslate\">django.core.files</code></a> module and its submodules contain built-in classes\nfor basic file handling in Django.</p>\n<section id=\"the-file-class\">\n<h2>The <code class=\"docutils literal notranslate\">File</code> class<a class=\"heading-anchor\" href=\"#the-file-class\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.core.files.File\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">File</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">file_object</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.files.File\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The <a class=\"reference internal\" href=\"#django.core.files.File\" title=\"django.core.files.File\"><code class=\"xref py py-class docutils literal notranslate\">File</code></a> class is a thin wrapper around a Python\n<a class=\"reference external\" href=\"https://docs.python.org/3/glossary.html#term-file-object\" title=\"(en Python versión 3.14)\"><span class=\"xref std std-term\">file object</span></a> with some Django-specific additions.\nInternally, Django uses this class when it needs to represent a file.</p>\n<p><a class=\"reference internal\" href=\"#django.core.files.File\" title=\"django.core.files.File\"><code class=\"xref py py-class docutils literal notranslate\">File</code></a> objects have the following attributes and methods:</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.core.files.File.name\">\n<span class=\"sig-name descname\">name</span><a class=\"heading-anchor\" href=\"#django.core.files.File.name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The name of the file including the relative path from\n<a class=\"reference internal\" href=\"/es/2.1/ref/settings/#std-setting-MEDIA_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\">MEDIA_ROOT</code></a>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.core.files.File.size\">\n<span class=\"sig-name descname\">size</span><a class=\"heading-anchor\" href=\"#django.core.files.File.size\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The size of the file in bytes.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.core.files.File.file\">\n<span class=\"sig-name descname\">file</span><a class=\"heading-anchor\" href=\"#django.core.files.File.file\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The underlying <a class=\"reference external\" href=\"https://docs.python.org/3/glossary.html#term-file-object\" title=\"(en Python versión 3.14)\"><span class=\"xref std std-term\">file object</span></a> that this class wraps.</p>\n<aside class=\"admonition-be-careful-with-this-attribute-in-subclasses admonition\" role=\"note\">\n<p class=\"admonition-title\">Be careful with this attribute in subclasses.</p>\n<p>Some subclasses of <a class=\"reference internal\" href=\"#django.core.files.File\" title=\"django.core.files.File\"><code class=\"xref py py-class docutils literal notranslate\">File</code></a>, including\n<a class=\"reference internal\" href=\"#django.core.files.base.ContentFile\" title=\"django.core.files.base.ContentFile\"><code class=\"xref py py-class docutils literal notranslate\">ContentFile</code></a> and\n<a class=\"reference internal\" href=\"/es/2.1/ref/models/fields/#django.db.models.fields.files.FieldFile\" title=\"django.db.models.fields.files.FieldFile\"><code class=\"xref py py-class docutils literal notranslate\">FieldFile</code></a>, may replace this\nattribute with an object other than a Python <a class=\"reference external\" href=\"https://docs.python.org/3/glossary.html#term-file-object\" title=\"(en Python versión 3.14)\"><span class=\"xref std std-term\">file object</span></a>.\nIn these cases, this attribute may itself be a <a class=\"reference internal\" href=\"#django.core.files.File\" title=\"django.core.files.File\"><code class=\"xref py py-class docutils literal notranslate\">File</code></a>\nsubclass (and not necessarily the same subclass). Whenever\npossible, use the attributes and methods of the subclass itself\nrather than the those of the subclass’s <code class=\"docutils literal notranslate\">file</code> attribute.</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.core.files.File.mode\">\n<span class=\"sig-name descname\">mode</span><a class=\"heading-anchor\" href=\"#django.core.files.File.mode\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The read/write mode for the file.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.files.File.open\">\n<span class=\"sig-name descname\">open</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">mode<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.files.File.open\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Open or reopen the file (which also does <code class=\"docutils literal notranslate\">File.seek(0)</code>).\nThe <code class=\"docutils literal notranslate\">mode</code> argument allows the same values\nas Python’s built-in <a class=\"reference external\" href=\"https://docs.python.org/3/library/functions.html#open\" title=\"(en Python versión 3.14)\"><code class=\"docutils literal notranslate\">open()</code></a>.</p>\n<p>When reopening a file, <code class=\"docutils literal notranslate\">mode</code> will override whatever mode the file\nwas originally opened with; <code class=\"docutils literal notranslate\">None</code> means to reopen with the original\nmode.</p>\n<p>It can be used as a context manager, e.g. <code class=\"docutils literal notranslate\">with file.open() as f:</code>.</p>\n<aside class=\"version-note version-changed\" data-version=\"2.0\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 2.0</p><p>Context manager support was added.</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.files.File.__iter__\">\n<span class=\"sig-name descname\">__iter__</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.files.File.__iter__\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Iterate over the file yielding one line at a time.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.files.File.chunks\">\n<span class=\"sig-name descname\">chunks</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">chunk_size<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.files.File.chunks\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Iterate over the file yielding «chunks» of a given size. <code class=\"docutils literal notranslate\">chunk_size</code>\ndefaults to 64 KB.</p>\n<p>This is especially useful with very large files since it allows them to\nbe streamed off disk and avoids storing the whole file in memory.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.files.File.multiple_chunks\">\n<span class=\"sig-name descname\">multiple_chunks</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">chunk_size<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.files.File.multiple_chunks\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns <code class=\"docutils literal notranslate\">True</code> if the file is large enough to require multiple chunks\nto access all of its content give some <code class=\"docutils literal notranslate\">chunk_size</code>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.files.File.close\">\n<span class=\"sig-name descname\">close</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.files.File.close\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Close the file.</p>\n</dd></dl>\n\n<p>In addition to the listed methods, <a class=\"reference internal\" href=\"#django.core.files.File\" title=\"django.core.files.File\"><code class=\"xref py py-class docutils literal notranslate\">File</code></a> exposes\nthe following attributes and methods of its <code class=\"docutils literal notranslate\">file</code> object:\n<code class=\"docutils literal notranslate\">encoding</code>, <code class=\"docutils literal notranslate\">fileno</code>, <code class=\"docutils literal notranslate\">flush</code>, <code class=\"docutils literal notranslate\">isatty</code>, <code class=\"docutils literal notranslate\">newlines</code>, <code class=\"docutils literal notranslate\">read</code>,\n<code class=\"docutils literal notranslate\">readinto</code>, <code class=\"docutils literal notranslate\">readline</code>, <code class=\"docutils literal notranslate\">readlines</code>, <code class=\"docutils literal notranslate\">seek</code>, <code class=\"docutils literal notranslate\">tell</code>,\n<code class=\"docutils literal notranslate\">truncate</code>, <code class=\"docutils literal notranslate\">write</code>, <code class=\"docutils literal notranslate\">writelines</code>, <code class=\"docutils literal notranslate\">readable()</code>, <code class=\"docutils literal notranslate\">writable()</code>,\nand <code class=\"docutils literal notranslate\">seekable()</code>.</p>\n</dd></dl>\n\n</section>\n<section id=\"the-contentfile-class\">\n<h2>The <code class=\"docutils literal notranslate\">ContentFile</code> class<a class=\"heading-anchor\" href=\"#the-contentfile-class\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.core.files.base.ContentFile\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">ContentFile</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">File</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.files.base.ContentFile\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The <code class=\"docutils literal notranslate\">ContentFile</code> class inherits from <a class=\"reference internal\" href=\"#django.core.files.File\" title=\"django.core.files.File\"><code class=\"xref py py-class docutils literal notranslate\">File</code></a>,\nbut unlike <a class=\"reference internal\" href=\"#django.core.files.File\" title=\"django.core.files.File\"><code class=\"xref py py-class docutils literal notranslate\">File</code></a> it operates on string content\n(bytes also supported), rather than an actual file. For example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.core.files.base</span> <span class=\"kn\">import</span> ContentFile\n\nf1 <span class=\"o\">=</span> ContentFile<span class=\"p\">(</span><span class=\"s2\">&quot;esta frase está en español&quot;</span><span class=\"p\">)</span>\nf2 <span class=\"o\">=</span> ContentFile<span class=\"p\">(</span><span class=\"sa\">b</span><span class=\"s2\">&quot;these are bytes&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n</dd></dl>\n\n</section>\n<section id=\"the-imagefile-class\">\n<h2>The <code class=\"docutils literal notranslate\">ImageFile</code> class<a class=\"heading-anchor\" href=\"#the-imagefile-class\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.core.files.images.ImageFile\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">ImageFile</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">file_object</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.files.images.ImageFile\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Django provides a built-in class specifically for images.\n<a class=\"reference internal\" href=\"#django.core.files.images.ImageFile\" title=\"django.core.files.images.ImageFile\"><code class=\"xref py py-class docutils literal notranslate\">django.core.files.images.ImageFile</code></a> inherits all the attributes\nand methods of <a class=\"reference internal\" href=\"#django.core.files.File\" title=\"django.core.files.File\"><code class=\"xref py py-class docutils literal notranslate\">File</code></a>, and additionally\nprovides the following:</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.core.files.images.ImageFile.width\">\n<span class=\"sig-name descname\">width</span><a class=\"heading-anchor\" href=\"#django.core.files.images.ImageFile.width\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Width of the image in pixels.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.core.files.images.ImageFile.height\">\n<span class=\"sig-name descname\">height</span><a class=\"heading-anchor\" href=\"#django.core.files.images.ImageFile.height\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Height of the image in pixels.</p>\n</dd></dl>\n\n</dd></dl>\n\n</section>\n<section id=\"additional-methods-on-files-attached-to-objects\">\n<h2>Additional methods on files attached to objects<a class=\"heading-anchor\" href=\"#additional-methods-on-files-attached-to-objects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Any <a class=\"reference internal\" href=\"#django.core.files.File\" title=\"django.core.files.File\"><code class=\"xref py py-class docutils literal notranslate\">File</code></a> that is associated with an object (as with <code class=\"docutils literal notranslate\">Car.photo</code>,\nbelow) will also have a couple of extra methods:</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.files.File.save\">\n<span class=\"sig-prename descclassname\">File.</span><span class=\"sig-name descname\">save</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">name</em>, <em class=\"sig-param\">content</em>, <em class=\"sig-param\">save<span class=\"o\">=</span><span class=\"default_value\">True</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.files.File.save\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Saves a new file with the file name and contents provided. This will not\nreplace the existing file, but will create a new file and update the object\nto point to it. If <code class=\"docutils literal notranslate\">save</code> is <code class=\"docutils literal notranslate\">True</code>, the model’s <code class=\"docutils literal notranslate\">save()</code> method will\nbe called once the file is saved. That is, these two lines:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>car<span class=\"o\">.</span>photo<span class=\"o\">.</span>save<span class=\"p\">(</span><span class=\"s1\">&#39;myphoto.jpg&#39;</span><span class=\"p\">,</span> content<span class=\"p\">,</span> save<span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>car<span class=\"o\">.</span>save<span class=\"p\">()</span>\n</code></pre></div>\n<p>are equivalent to:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>car<span class=\"o\">.</span>photo<span class=\"o\">.</span>save<span class=\"p\">(</span><span class=\"s1\">&#39;myphoto.jpg&#39;</span><span class=\"p\">,</span> content<span class=\"p\">,</span> save<span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Note that the <code class=\"docutils literal notranslate\">content</code> argument must be an instance of either\n<a class=\"reference internal\" href=\"#django.core.files.File\" title=\"django.core.files.File\"><code class=\"xref py py-class docutils literal notranslate\">File</code></a> or of a subclass of <a class=\"reference internal\" href=\"#django.core.files.File\" title=\"django.core.files.File\"><code class=\"xref py py-class docutils literal notranslate\">File</code></a>, such as\n<a class=\"reference internal\" href=\"#django.core.files.base.ContentFile\" title=\"django.core.files.base.ContentFile\"><code class=\"xref py py-class docutils literal notranslate\">ContentFile</code></a>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.files.File.delete\">\n<span class=\"sig-prename descclassname\">File.</span><span class=\"sig-name descname\">delete</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">save<span class=\"o\">=</span><span class=\"default_value\">True</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.files.File.delete\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Removes the file from the model instance and deletes the underlying file.\nIf <code class=\"docutils literal notranslate\">save</code> is <code class=\"docutils literal notranslate\">True</code>, the model’s <code class=\"docutils literal notranslate\">save()</code> method will be called once\nthe file is deleted.</p>\n</dd></dl>\n\n</section>","rootId":"the-file-object","toc":[{"title":"The File class","anchor":"the-file-class","children":[]},{"title":"The ContentFile class","anchor":"the-contentfile-class","children":[]},{"title":"The ImageFile class","anchor":"the-imagefile-class","children":[]},{"title":"Additional methods on files attached to objects","anchor":"additional-methods-on-files-attached-to-objects","children":[]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/es/2.1/ref/"},{"docname":"ref/files/index","title":"File handling","url":"/es/2.1/ref/files/"}],"prev":{"docname":"ref/files/index","title":"File handling","url":"/es/2.1/ref/files/"},"next":{"docname":"ref/files/storage","title":"File storage API","url":"/es/2.1/ref/files/storage/"},"formats":{"html":"/es/2.1/ref/files/file/","markdown":"/es/2.1/ref/files/file.md","json":"/es/2.1/ref/files/file.json"},"source":"https://github.com/django/django/blob/stable/2.1.x/docs/ref/files/file.txt","official":"https://docs.djangoproject.com/es/2.1/ref/files/file/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9"],"inLocales":["en","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}