{"title":"Managing files","version":"2.0","locale":"pt-br","docname":"topics/files","url":"/pt-br/2.0/topics/files/","canonical":"https://djangodocs.dev/pt-br/2.0/topics/files/","summary":"This document describes Django’s file access APIs for files such as those uploaded by a user.","html":"<h1>Managing files<a class=\"heading-anchor\" href=\"#managing-files\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>This document describes Django’s file access APIs for files such as those\nuploaded by a user. The lower level APIs are general enough that you could use\nthem for other purposes. If you want to handle “static files” (JS, CSS, etc.),\nsee <a class=\"reference internal\" href=\"/pt-br/2.0/howto/static-files/\"><span class=\"doc\">Gerenciamento de arquivos estáticos (ex.: imagens, JavaScript, CSS)</span></a>.</p>\n<p>By default, Django stores files locally, using the <a class=\"reference internal\" href=\"/pt-br/2.0/ref/settings/#std-setting-MEDIA_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\">MEDIA_ROOT</code></a> and\n<a class=\"reference internal\" href=\"/pt-br/2.0/ref/settings/#std-setting-MEDIA_URL\"><code class=\"xref std std-setting docutils literal notranslate\">MEDIA_URL</code></a> settings. The examples below assume that you’re using these\ndefaults.</p>\n<p>However, Django provides ways to write custom <a class=\"reference internal\" href=\"#file-storage\">file storage systems</a> that\nallow you to completely customize where and how Django stores files. The\nsecond half of this document describes how these storage systems work.</p>\n<section id=\"using-files-in-models\">\n<h2>Using files in models<a class=\"heading-anchor\" href=\"#using-files-in-models\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>When you use a <a class=\"reference internal\" href=\"/pt-br/2.0/ref/models/fields/#django.db.models.FileField\" title=\"django.db.models.FileField\"><code class=\"xref py py-class docutils literal notranslate\">FileField</code></a> or\n<a class=\"reference internal\" href=\"/pt-br/2.0/ref/models/fields/#django.db.models.ImageField\" title=\"django.db.models.ImageField\"><code class=\"xref py py-class docutils literal notranslate\">ImageField</code></a>, Django provides a set of APIs you can use\nto deal with that file.</p>\n<p>Consider the following model, using an <a class=\"reference internal\" href=\"/pt-br/2.0/ref/models/fields/#django.db.models.ImageField\" title=\"django.db.models.ImageField\"><code class=\"xref py py-class docutils literal notranslate\">ImageField</code></a> to\nstore a photo:</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.db</span> <span class=\"kn\">import</span> models\n\n<span class=\"k\">class</span> <span class=\"nc\">Car</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    name <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">255</span><span class=\"p\">)</span>\n    price <span class=\"o\">=</span> models<span class=\"o\">.</span>DecimalField<span class=\"p\">(</span>max_digits<span class=\"o\">=</span><span class=\"mi\">5</span><span class=\"p\">,</span> decimal_places<span class=\"o\">=</span><span class=\"mi\">2</span><span class=\"p\">)</span>\n    photo <span class=\"o\">=</span> models<span class=\"o\">.</span>ImageField<span class=\"p\">(</span>upload_to<span class=\"o\">=</span><span class=\"s1\">&#39;cars&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Any <code class=\"docutils literal notranslate\">Car</code> instance will have a <code class=\"docutils literal notranslate\">photo</code> attribute that you can use to get at\nthe details of the attached photo:</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> Car<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span>name<span class=\"o\">=</span><span class=\"s2\">&quot;57 Chevy&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>car<span class=\"o\">.</span>photo\n<span class=\"go\">&lt;ImageFieldFile: chevy.jpg&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>car<span class=\"o\">.</span>photo<span class=\"o\">.</span>name\n<span class=\"go\">&#39;cars/chevy.jpg&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>car<span class=\"o\">.</span>photo<span class=\"o\">.</span>path\n<span class=\"go\">&#39;/media/cars/chevy.jpg&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>car<span class=\"o\">.</span>photo<span class=\"o\">.</span>url\n<span class=\"go\">&#39;http://media.example.com/cars/chevy.jpg&#39;</span>\n</code></pre></div>\n<p>This object – <code class=\"docutils literal notranslate\">car.photo</code> in the example – is a <code class=\"docutils literal notranslate\">File</code> object, which means\nit has all the methods and attributes described below.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Nota</p>\n<p>The file is saved as part of saving the model in the database, so the actual\nfile name used on disk cannot be relied on until after the model has been\nsaved.</p>\n</aside>\n<p>For example, you can change the file name by setting the file’s\n<a class=\"reference internal\" href=\"/pt-br/2.0/ref/files/file/#django.core.files.File.name\" title=\"django.core.files.File.name\"><code class=\"xref py py-attr docutils literal notranslate\">name</code></a> to a path relative to the file storage’s\nlocation (<a class=\"reference internal\" href=\"/pt-br/2.0/ref/settings/#std-setting-MEDIA_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\">MEDIA_ROOT</code></a> if you are using the default\n<a class=\"reference internal\" href=\"/pt-br/2.0/ref/files/storage/#django.core.files.storage.FileSystemStorage\" title=\"django.core.files.storage.FileSystemStorage\"><code class=\"xref py py-class docutils literal notranslate\">FileSystemStorage</code></a>):</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><span class=\"kn\">import</span> <span class=\"nn\">os</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span> <span class=\"nn\">django.conf</span> <span class=\"kn\">import</span> settings\n<span class=\"gp\">&gt;&gt;&gt; </span>initial_path <span class=\"o\">=</span> car<span class=\"o\">.</span>photo<span class=\"o\">.</span>path\n<span class=\"gp\">&gt;&gt;&gt; </span>car<span class=\"o\">.</span>photo<span class=\"o\">.</span>name <span class=\"o\">=</span> <span class=\"s1\">&#39;cars/chevy_ii.jpg&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>new_path <span class=\"o\">=</span> settings<span class=\"o\">.</span>MEDIA_ROOT <span class=\"o\">+</span> car<span class=\"o\">.</span>photo<span class=\"o\">.</span>name\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"c1\"># Move the file on the filesystem</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>os<span class=\"o\">.</span>rename<span class=\"p\">(</span>initial_path<span class=\"p\">,</span> new_path<span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>car<span class=\"o\">.</span>save<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>car<span class=\"o\">.</span>photo<span class=\"o\">.</span>path\n<span class=\"go\">&#39;/media/cars/chevy_ii.jpg&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>car<span class=\"o\">.</span>photo<span class=\"o\">.</span>path <span class=\"o\">==</span> new_path\n<span class=\"go\">True</span>\n</code></pre></div>\n</section>\n<section id=\"the-file-object\">\n<h2>O <code class=\"docutils literal notranslate\">File</code> objeto<a class=\"heading-anchor\" href=\"#the-file-object\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Internally, Django uses a <a class=\"reference internal\" href=\"/pt-br/2.0/ref/files/file/#django.core.files.File\" title=\"django.core.files.File\"><code class=\"xref py py-class docutils literal notranslate\">django.core.files.File</code></a> instance any time it\nneeds to represent a file.</p>\n<p>Most of the time you’ll simply use a <code class=\"docutils literal notranslate\">File</code> that Django’s given you (i.e. a\nfile attached to a model as above, or perhaps an uploaded file).</p>\n<p>If you need to construct a <code class=\"docutils literal notranslate\">File</code> yourself, the easiest way is to create one\nusing a Python built-in <code class=\"docutils literal notranslate\">file</code> object:</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><span class=\"kn\">from</span> <span class=\"nn\">django.core.files</span> <span class=\"kn\">import</span> File\n\n<span class=\"go\"># Create a Python file object using open()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>f <span class=\"o\">=</span> <span class=\"nb\">open</span><span class=\"p\">(</span><span class=\"s1\">&#39;/path/to/hello.world&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;w&#39;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>myfile <span class=\"o\">=</span> File<span class=\"p\">(</span>f<span class=\"p\">)</span>\n</code></pre></div>\n<p>Now you can use any of the documented attributes and methods\nof the <a class=\"reference internal\" href=\"/pt-br/2.0/ref/files/file/#django.core.files.File\" title=\"django.core.files.File\"><code class=\"xref py py-class docutils literal notranslate\">File</code></a> class.</p>\n<p>Be aware that files created in this way are not automatically closed.\nThe following approach may be used to close files automatically:</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><span class=\"kn\">from</span> <span class=\"nn\">django.core.files</span> <span class=\"kn\">import</span> File\n\n<span class=\"go\"># Create a Python file object using open() and the with statement</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">with</span> <span class=\"nb\">open</span><span class=\"p\">(</span><span class=\"s1\">&#39;/path/to/hello.world&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;w&#39;</span><span class=\"p\">)</span> <span class=\"k\">as</span> f<span class=\"p\">:</span>\n<span class=\"gp\">... </span>    myfile <span class=\"o\">=</span> File<span class=\"p\">(</span>f<span class=\"p\">)</span>\n<span class=\"gp\">... </span>    myfile<span class=\"o\">.</span>write<span class=\"p\">(</span><span class=\"s1\">&#39;Hello World&#39;</span><span class=\"p\">)</span>\n<span class=\"gp\">...</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>myfile<span class=\"o\">.</span>closed\n<span class=\"go\">True</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>f<span class=\"o\">.</span>closed\n<span class=\"go\">True</span>\n</code></pre></div>\n<p>Closing files is especially important when accessing file fields in a loop\nover a large number of objects. If files are not manually closed after\naccessing them, the risk of running out of file descriptors may arise. This\nmay lead to the following error:</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=\"ne\">IOError</span><span class=\"p\">:</span> <span class=\"p\">[</span>Errno <span class=\"mi\">24</span><span class=\"p\">]</span> Too many <span class=\"nb\">open</span> files\n</code></pre></div>\n</section>\n<section id=\"file-storage\">\n<h2>File storage<a class=\"heading-anchor\" href=\"#file-storage\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Behind the scenes, Django delegates decisions about how and where to store files\nto a file storage system. This is the object that actually understands things\nlike file systems, opening and reading files, etc.</p>\n<p>Django’s default file storage is given by the <a class=\"reference internal\" href=\"/pt-br/2.0/ref/settings/#std-setting-DEFAULT_FILE_STORAGE\"><code class=\"xref std std-setting docutils literal notranslate\">DEFAULT_FILE_STORAGE</code></a>\nsetting; if you don’t explicitly provide a storage system, this is the one that\nwill be used.</p>\n<p>See below for details of the built-in default file storage system, and see\n<a class=\"reference internal\" href=\"/pt-br/2.0/howto/custom-file-storage/\"><span class=\"doc\">Escrevendo um sistema de armazenamento personalizado</span></a> for information on writing your own file\nstorage system.</p>\n<section id=\"storage-objects\">\n<h3>Storage objects<a class=\"heading-anchor\" href=\"#storage-objects\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Though most of the time you’ll want to use a <code class=\"docutils literal notranslate\">File</code> object (which delegates to\nthe proper storage for that file), you can use file storage systems directly.\nYou can create an instance of some custom file storage class, or – often more\nuseful – you can use the global default storage system:</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><span class=\"kn\">from</span> <span class=\"nn\">django.core.files.base</span> <span class=\"kn\">import</span> ContentFile\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span> <span class=\"nn\">django.core.files.storage</span> <span class=\"kn\">import</span> default_storage\n\n<span class=\"gp\">&gt;&gt;&gt; </span>path <span class=\"o\">=</span> default_storage<span class=\"o\">.</span>save<span class=\"p\">(</span><span class=\"s1\">&#39;/path/to/file&#39;</span><span class=\"p\">,</span> ContentFile<span class=\"p\">(</span><span class=\"s1\">&#39;new content&#39;</span><span class=\"p\">))</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>path\n<span class=\"go\">&#39;/path/to/file&#39;</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span>default_storage<span class=\"o\">.</span>size<span class=\"p\">(</span>path<span class=\"p\">)</span>\n<span class=\"go\">11</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>default_storage<span class=\"o\">.</span>open<span class=\"p\">(</span>path<span class=\"p\">)</span><span class=\"o\">.</span>read<span class=\"p\">()</span>\n<span class=\"go\">&#39;new content&#39;</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span>default_storage<span class=\"o\">.</span>delete<span class=\"p\">(</span>path<span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>default_storage<span class=\"o\">.</span>exists<span class=\"p\">(</span>path<span class=\"p\">)</span>\n<span class=\"go\">False</span>\n</code></pre></div>\n<p>See <a class=\"reference internal\" href=\"/pt-br/2.0/ref/files/storage/\"><span class=\"doc\">File storage API</span></a> for the file storage API.</p>\n</section>\n<section id=\"the-built-in-filesystem-storage-class\">\n<span id=\"builtin-fs-storage\"></span><h3>The built-in filesystem storage class<a class=\"heading-anchor\" href=\"#the-built-in-filesystem-storage-class\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Django ships with a <a class=\"reference internal\" href=\"/pt-br/2.0/ref/files/storage/#django.core.files.storage.FileSystemStorage\" title=\"django.core.files.storage.FileSystemStorage\"><code class=\"xref py py-class docutils literal notranslate\">django.core.files.storage.FileSystemStorage</code></a> class\nwhich implements basic local filesystem file storage.</p>\n<p>For example, the following code will store uploaded files under\n<code class=\"docutils literal notranslate\">/media/photos</code> regardless of what your <a class=\"reference internal\" href=\"/pt-br/2.0/ref/settings/#std-setting-MEDIA_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\">MEDIA_ROOT</code></a> setting is:</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.storage</span> <span class=\"kn\">import</span> FileSystemStorage\n<span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n\nfs <span class=\"o\">=</span> FileSystemStorage<span class=\"p\">(</span>location<span class=\"o\">=</span><span class=\"s1\">&#39;/media/photos&#39;</span><span class=\"p\">)</span>\n\n<span class=\"k\">class</span> <span class=\"nc\">Car</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    <span class=\"o\">...</span>\n    photo <span class=\"o\">=</span> models<span class=\"o\">.</span>ImageField<span class=\"p\">(</span>storage<span class=\"o\">=</span>fs<span class=\"p\">)</span>\n</code></pre></div>\n<p><a class=\"reference internal\" href=\"/pt-br/2.0/howto/custom-file-storage/\"><span class=\"doc\">Custom storage systems</span></a> work the same way:\nyou can pass them in as the <code class=\"docutils literal notranslate\">storage</code> argument to a\n<a class=\"reference internal\" href=\"/pt-br/2.0/ref/models/fields/#django.db.models.FileField\" title=\"django.db.models.FileField\"><code class=\"xref py py-class docutils literal notranslate\">FileField</code></a>.</p>\n</section>\n</section>","rootId":"managing-files","toc":[{"title":"Using files in models","anchor":"using-files-in-models","children":[]},{"title":"O File objeto","anchor":"the-file-object","children":[]},{"title":"File storage","anchor":"file-storage","children":[{"title":"Storage objects","anchor":"storage-objects","children":[]},{"title":"The built-in filesystem storage class","anchor":"the-built-in-filesystem-storage-class","children":[]}]}],"breadcrumbs":[{"docname":"topics/index","title":"Usando o Django","url":"/pt-br/2.0/topics/"}],"prev":{"docname":"topics/migrations","title":"Migrations","url":"/pt-br/2.0/topics/migrations/"},"next":{"docname":"topics/testing/index","title":"Testando em Django","url":"/pt-br/2.0/topics/testing/"},"formats":{"html":"/pt-br/2.0/topics/files/","markdown":"/pt-br/2.0/topics/files.md","json":"/pt-br/2.0/topics/files.json"},"source":"https://github.com/django/django/blob/stable/2.0.x/docs/topics/files.txt","official":"https://docs.djangoproject.com/pt-br/2.0/topics/files/","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"]}