---
title: "File オブジェクト"
version: 6.0
locale: ja
source: https://docs.djangoproject.com/ja/6.0/ref/files/file/
canonical: https://djangodocs.dev/ja/6.0/ref/files/file/
---
# `File` オブジェクト

[`django.core.files`](/ja/6.0/ref/files/#module-django.core.files) モジュールとそのサブモジュールでは、Django の基本的なファイルハンドリングに関するビルトインクラスが定義されています。

## `File` クラス

#### `class File(file_object, name=None)`

The [`File`](#django.core.files.File) class is a thin wrapper around a Python
[file object](https://docs.python.org/3/glossary.html#term-file-object) with some Django-specific additions.
Internally, Django uses this class when it needs to represent a file.

[`File`](#django.core.files.File) オブジェクトには次の属性とメソッドを持ちます。

#### `name`

[`MEDIA_ROOT`](/ja/6.0/ref/settings/#std-setting-MEDIA_ROOT) からの相対パスを含むファイル名です。

#### `size`

バイト単位で表されたファイルサイズ。

#### `file`

The underlying [file object](https://docs.python.org/3/glossary.html#term-file-object) that this class wraps.

> **この属性をサブクラスで扱う場合には注意が必要です。**
>
> Some subclasses of [`File`](#django.core.files.File), including
> [`ContentFile`](#django.core.files.base.ContentFile) and
> [`FieldFile`](/ja/6.0/ref/models/fields/#django.db.models.fields.files.FieldFile), may replace this
> attribute with an object other than a Python [file
> object](https://docs.python.org/3/glossary.html#term-file-object). In these cases, this attribute may itself be a
> [`File`](#django.core.files.File) subclass (and not necessarily the same subclass).
> Whenever possible, use the attributes and methods of the subclass
> itself rather than those of the subclass's `file` attribute.

#### `mode`

ファイルの読み込み/書き込みのモードです。

#### `open(mode=None, *args, **kwargs)`

Open or reopen the file (which also does `File.seek(0)`).
The `mode` argument allows the same values
as Python's built-in [`open()`](https://docs.python.org/3/library/functions.html#open). `*args` and `**kwargs`
are passed after `mode` to Python's built-in [`open()`](https://docs.python.org/3/library/functions.html#open).

ファイルを再オープンする際、 `mode` はファイルが元々開かれていたモードを上書きします。 `None` はオリジナルのモードで再オープンすることを意味します。

コンテキストマネージャとして使用できます。たとえば、 `with file.open() as f:` のようにです。

#### `__iter__()`

ファイルをイテレートして、1行ずつ返します。

#### `chunks(chunk_size=None)`

ファイルをイテレートし、指定されたサイズの「チャンク」を生成します。 `chunk_size` のデフォルトは 64 KB です。

これは非常に大きなファイルに特に便利であり、ディスクからストリーミングしてメモリ全体にファイルを保存することを避けることができます。

#### `multiple_chunks(chunk_size=None)`

`chunk_size` (一括サイズ) で指定されたファイルの全内容にアクセスするために複数のチャンクが必要な場合は `True` を返します。

#### `close()`

ファイルを閉じてください。

リストされたメソッドに加えて、[`File`](#django.core.files.File) は、その `file` オブジェクトの以下の属性とメソッドを公開しています: `encoding`, `fileno`, `flush`, `isatty`, `newlines`, `read`, `readinto`, `readline`, `readlines`, `seek`, `tell`, `truncate`, `write`, `writelines`, `readable()`, `writable()`, および `seekable()` 。

## `ContentFile` クラス

#### `class ContentFile(content, name=None)`

`ContentFile` クラスは [`File`](#django.core.files.File) を継承していますが、[`File`](#django.core.files.File) とは異なり、実際のファイルではなく文字列コンテンツ(バイトもサポート)で操作します。例えば：

```
from django.core.files.base import ContentFile

f1 = ContentFile("esta frase está en español")
f2 = ContentFile(b"these are bytes")
```

## `ImageFile` クラス

#### `class ImageFile(file_object, name=None)`

Django には画像専用の組み込みクラスがあります。 [`django.core.files.images.ImageFile`](#django.core.files.images.ImageFile) は [`File`](#django.core.files.File) のすべての属性とメソッドを継承し、さらに次のものを提供します:

#### `width`

画像の幅 (ピクセル単位)。

#### `height`

画像の高さ(ピクセル単位)。

## オブジェクトに添付されたファイルに関する追加メソッド

オブジェクト（以下の `Car.photo` のように）に関連付けられた [`File`](#django.core.files.File) には、いくつかの追加メソッドも用意されています。

#### `File.save(name, content, save=True)`

提供されたファイル名と内容で新しいファイルを保存します。これは既存のファイルを置き換えるものではありませんが、新しいファイルを作成し、オブジェクトがそれを指すように更新します。 `save` が `True` の場合、ファイルが保存されたらモデルの `save()` メソッドが一度呼び出されます。つまり、次の二行です:

```pycon
>>> car.photo.save("myphoto.jpg", content, save=False)
>>> car.save()
```

これは以下と同等です:

```pycon
>>> car.photo.save("myphoto.jpg", content, save=True)
```

`content` 引数は、 [`File`](#django.core.files.File) または [`File`](#django.core.files.File) のサブクラス（例えば、 [`ContentFile`](#django.core.files.base.ContentFile) など）のインスタンスでなければなりません。

#### `File.delete(save=True)`

モデルインスタンスからファイルを削除し、基になるファイルも削除します。 `save` が `True` の場合、ファイルが削除された後にモデルの `save()` メソッドが一度呼び出されます。
