Python Tempfile モジュール

tempfile モジュールは、テンポラリファイルとテンポラリディレクトリを安全に作成します。

import tempfile

tempfile は、テスト出力、ダウンロード、中間ファイルのように、プログラムの実行中だけ存在すればよいデータに使います。

TemporaryFile()

TemporaryFile は、自動的に片付けられるファイルオブジェクトを作成します。

import tempfile

with tempfile.TemporaryFile(mode='w+t') as temp:
    temp.write('hello')
    temp.seek(0)
    print(temp.read())
hello

with ブロックが終わると、ファイルは自動的に削除されます。

NamedTemporaryFile()

NamedTemporaryFile は、ディスク上のパスを返します。

import tempfile

with tempfile.NamedTemporaryFile(mode='w+t') as temp:
    print(bool(temp.name))
True

これは、別の API がファイルオブジェクトではなくファイル名を必要とするときに便利です。

TemporaryDirectory()

テンポラリディレクトリは、テストや短命の作業領域に便利です。

import tempfile
from pathlib import Path

with tempfile.TemporaryDirectory() as directory:
    file_path = Path(directory) / 'notes.txt'
    file_path.write_text('draft')
    print(file_path.exists())
True

ディレクトリ内のファイルは、ブロック終了時にディレクトリと一緒に削除されます。

テンポラリの場所を選ぶ

gettempdir() は、Python がデフォルトで使うディレクトリを返します。

import tempfile

print(bool(tempfile.gettempdir()))
True

関連リンク