{"title":"File オブジェクト","version":"6.0","locale":"ja","docname":"ref/files/file","url":"/ja/6.0/ref/files/file/","canonical":"https://djangodocs.dev/ja/6.0/ref/files/file/","summary":"django.core.files モジュールとそのサブモジュールでは、Django の基本的なファイルハンドリングに関するビルトインクラスが定義されています。","html":"<h1><code class=\"docutils literal notranslate\">File</code> オブジェクト<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><a class=\"reference internal\" href=\"/ja/6.0/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> モジュールとそのサブモジュールでは、Django の基本的なファイルハンドリングに関するビルトインクラスが定義されています。</p>\n<section id=\"the-file-class\">\n<h2><code class=\"docutils literal notranslate\">File</code> クラス<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>, <em class=\"sig-param\">name<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\"><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=\"(in Python v3.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> オブジェクトには次の属性とメソッドを持ちます。</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><a class=\"reference internal\" href=\"/ja/6.0/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>バイト単位で表されたファイルサイズ。</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=\"(in Python v3.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\">この属性をサブクラスで扱う場合には注意が必要です。</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=\"/ja/6.0/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=\"(in Python v3.14)\"><span class=\"xref std std-term\">file\nobject</span></a>. In these cases, this attribute may itself be a\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> subclass (and not necessarily the same subclass).\nWhenever possible, use the attributes and methods of the subclass\nitself rather than 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>ファイルの読み込み/書き込みのモードです。</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>, <em class=\"sig-param\"><span class=\"o\">*</span>args</em>, <em class=\"sig-param\"><span class=\"o\">**</span>kwargs</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=\"(in Python v3.14)\"><code class=\"docutils literal notranslate\">open()</code></a>. <code class=\"docutils literal notranslate\">*args</code> and <code class=\"docutils literal notranslate\">**kwargs</code>\nare passed after <code class=\"docutils literal notranslate\">mode</code> to Python's built-in <a class=\"reference external\" href=\"https://docs.python.org/3/library/functions.html#open\" title=\"(in Python v3.14)\"><code class=\"docutils literal notranslate\">open()</code></a>.</p>\n<p>ファイルを再オープンする際、 <code class=\"docutils literal notranslate\">mode</code> はファイルが元々開かれていたモードを上書きします。 <code class=\"docutils literal notranslate\">None</code> はオリジナルのモードで再オープンすることを意味します。</p>\n<p>コンテキストマネージャとして使用できます。たとえば、 <code class=\"docutils literal notranslate\">with file.open() as f:</code> のようにです。</p>\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>ファイルをイテレートして、1行ずつ返します。</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>ファイルをイテレートし、指定されたサイズの「チャンク」を生成します。 <code class=\"docutils literal notranslate\">chunk_size</code> のデフォルトは 64 KB です。</p>\n<p>これは非常に大きなファイルに特に便利であり、ディスクからストリーミングしてメモリ全体にファイルを保存することを避けることができます。</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><code class=\"docutils literal notranslate\">chunk_size</code> (一括サイズ) で指定されたファイルの全内容にアクセスするために複数のチャンクが必要な場合は <code class=\"docutils literal notranslate\">True</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>ファイルを閉じてください。</p>\n</dd></dl>\n\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> は、その <code class=\"docutils literal notranslate\">file</code> オブジェクトの以下の属性とメソッドを公開しています: <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>, <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>, <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>, および <code class=\"docutils literal notranslate\">seekable()</code> 。</p>\n</dd></dl>\n\n</section>\n<section id=\"the-contentfile-class\">\n<h2><code class=\"docutils literal notranslate\">ContentFile</code> クラス<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\">content</em>, <em class=\"sig-param\">name<span class=\"o\">=</span><span class=\"default_value\">None</span></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><code class=\"docutils literal notranslate\">ContentFile</code> クラスは <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> を継承していますが、<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> とは異なり、実際のファイルではなく文字列コンテンツ(バイトもサポート)で操作します。例えば：</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><code class=\"docutils literal notranslate\">ImageFile</code> クラス<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>, <em class=\"sig-param\">name<span class=\"o\">=</span><span class=\"default_value\">None</span></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 には画像専用の組み込みクラスがあります。 <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> は <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> のすべての属性とメソッドを継承し、さらに次のものを提供します:</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>画像の幅 (ピクセル単位)。</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>画像の高さ(ピクセル単位)。</p>\n</dd></dl>\n\n</dd></dl>\n\n</section>\n<section id=\"additional-methods-on-files-attached-to-objects\">\n<h2>オブジェクトに添付されたファイルに関する追加メソッド<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>オブジェクト（以下の <code class=\"docutils literal notranslate\">Car.photo</code> のように）に関連付けられた <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> には、いくつかの追加メソッドも用意されています。</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>提供されたファイル名と内容で新しいファイルを保存します。これは既存のファイルを置き換えるものではありませんが、新しいファイルを作成し、オブジェクトがそれを指すように更新します。 <code class=\"docutils literal notranslate\">save</code> が <code class=\"docutils literal notranslate\">True</code> の場合、ファイルが保存されたらモデルの <code class=\"docutils literal notranslate\">save()</code> メソッドが一度呼び出されます。つまり、次の二行です:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console 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=\"s2\">&quot;myphoto.jpg&quot;</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>これは以下と同等です:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console 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=\"s2\">&quot;myphoto.jpg&quot;</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><code class=\"docutils literal notranslate\">content</code> 引数は、 <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> または <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> のサブクラス（例えば、 <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>モデルインスタンスからファイルを削除し、基になるファイルも削除します。 <code class=\"docutils literal notranslate\">save</code> が <code class=\"docutils literal notranslate\">True</code> の場合、ファイルが削除された後にモデルの <code class=\"docutils literal notranslate\">save()</code> メソッドが一度呼び出されます。</p>\n</dd></dl>\n\n</section>","rootId":"the-file-object","toc":[{"title":"File クラス","anchor":"the-file-class","children":[]},{"title":"ContentFile クラス","anchor":"the-contentfile-class","children":[]},{"title":"ImageFile クラス","anchor":"the-imagefile-class","children":[]},{"title":"オブジェクトに添付されたファイルに関する追加メソッド","anchor":"additional-methods-on-files-attached-to-objects","children":[]}],"breadcrumbs":[{"docname":"ref/index","title":"API リファレンス","url":"/ja/6.0/ref/"},{"docname":"ref/files/index","title":"ファイルの取り扱い","url":"/ja/6.0/ref/files/"}],"prev":{"docname":"ref/files/index","title":"ファイルの取り扱い","url":"/ja/6.0/ref/files/"},"next":{"docname":"ref/files/storage","title":"ファイルストレージ API","url":"/ja/6.0/ref/files/storage/"},"formats":{"html":"/ja/6.0/ref/files/file/","markdown":"/ja/6.0/ref/files/file.md","json":"/ja/6.0/ref/files/file.json"},"source":"https://github.com/django/django/blob/stable/6.0.x/docs/ref/files/file.txt","official":"https://docs.djangoproject.com/ja/6.0/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","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}