Skip to main content

A tool that guarantees fully intact files by writing them atomically.

Project description

English description follows Japanese.

atomic-save

概要

  • atomic-save は、ファイルを 破損させない安全な保存 を実現する Python の小型ツールです。
  • 保存途中にプログラムが落ちても、ファイルは 完全に書き込まれた状態保存前の状態 のどちらかになります。

使い方の例

import atomic_save

# 文字列を atomic に保存
atomic_save["./testfile.txt"] = "hello!"

# バイト列を atomic に保存
atomic_save["./testfile.txt"] = b"hello!"

動作原理

データはまず 一時ファイル に完全に書き込まれます。 その後、書き込みが正常終了した段階で、一時ファイルが元ファイルと atomic に置き換え られます。 これにより、途中まで書かれた壊れたファイルが残ることはありません。

  • 一時ファイル名の形式: <元ファイル名>.<6桁16進ランダム>.atomic_save_temp
  • 保存できる型: str(文字列), bytes(バイト列)
  • 元ファイルが存在しない場合も問題なく保存可能 途中で落ちても「元々ファイルがなかった状態」に戻ります。

保存後の読み込みは通常どおり open() で行ってください。 成功した保存結果は常に元のファイル名に収まります。


atomic-save

Overview

  • atomic-save is a lightweight Python utility that guarantees corruption-free file writing.
  • Even if a crash occurs mid-save, the file will end up either fully written or completely unchanged.

Usage Example

import atomic_save

# Save text atomically
atomic_save["./testfile.txt"] = "hello!"

# Save bytes atomically
atomic_save["./testfile.txt"] = b"hello!"

How It Works

atomic-save writes all data into a temporary file first. After the write finishes successfully, it atomically replaces the original file with the completed temporary file. This design ensures no partially written file ever appears.

  • Temporary file format: <original_filename>.<6-digit hex random>.atomic_save_temp
  • Allowed value types: str, bytes
  • When the original file does not exist, saving works normally. If a crash occurs, the state cleanly falls back to “file did not exist.”

Reading the file requires no special API—just open the file normally. If the save succeeds, the completed data always appears under the original name.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

atomic_save-0.0.0.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

atomic_save-0.0.0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file atomic_save-0.0.0.tar.gz.

File metadata

  • Download URL: atomic_save-0.0.0.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.3

File hashes

Hashes for atomic_save-0.0.0.tar.gz
Algorithm Hash digest
SHA256 844dea2de878c6a1be8f960c88050da874c7f16af372db1fc6c5971f69ee2de0
MD5 18484bd7373a89c8bcf74c88e1f08c0b
BLAKE2b-256 623ce1e7d6f04d0d8298132ec2af3612773978976cbad6251e72ac1815b9c18f

See more details on using hashes here.

File details

Details for the file atomic_save-0.0.0-py3-none-any.whl.

File metadata

  • Download URL: atomic_save-0.0.0-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.3

File hashes

Hashes for atomic_save-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 49756ec9ca35b2a15908cbaed340ffcff5e57cdc7b7394b37d26d7de90700ed4
MD5 995c6e59ce2fb34be28d45c2ad22c022
BLAKE2b-256 09595a9eb86b9e8ff5dfdcc36afeefd2fba165d5877d2ea8220d17093cf01cbb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page