Skip to main content

This is a library that reads and writes Python local files in the most efficient way possible, without the need for detailed configuration.

Project description

Abara File IO

Description

Pythonのローカルファイルの読み書きを、細々とした設定なしに最適な方法で実行するライブラリ。

text、ini、json、yaml、tomlに対応。

読み込み時は自動的に文字コードを判定。
書き込み時は常に文字コード UTF-8 、改行コード \n を使用。

Dependency

tomli-w

ruamel.yaml

charset-normalizer

Install

pip

pip install abara-file-io

uv

uv add abara-file-io

Usage

ファイルの読み込み

ファイルを読み込む場合はread_から始まる関数を使用する。

from abara_file_io import read_text, read_ini, read_json, read_yaml, read_toml

path = './parent/stem.suffix'

text_response: str = read_text(path)

ini_response: dict = read_ini(path)

json_response: dict = read_json(path)

yaml_response: dict = read_yaml(path)

toml_response: dict = read_toml(path)

読み込みは第1引数にファイルのパス( str か、pathlibの Path オブジェクト)を指定するだけで動作する。

エンコードは最初に UTF-8 を試し、違った場合はcharset-normalizerで自動的に判定して読み込むので煩わされる必要がない。

読み込み時に存在しないファイルパスを指定などしてエラーが出た場合もエラーで停止しない仕様。
その場合は空の str もしくは dict を返すので、必要に応じて処理を分岐させることができる。

ファイルの書き込み

書き込む場合はwrite_から始まる関数を使用する。

from abara_file_io import write_text, write_ini, write_json, write_yaml, write_toml

path = './parent/stem.suffix'

str_data = 'Beautiful is better than ugly.'

dict_data = {'foo': 'bar', 'baz': 'qux'}

write_text(str_data, path)

write_ini(dict_data, path)

write_json(dict_data, path)

write_yaml(dict_data, path)

write_toml(dict_data, path)

書き込みは第1引数に書き込むオブジェクト。
第2引数に保存するファイル拡張子まで含めたパス( str か、pathlibの Path オブジェクト)を入れる。

常に文字コード UTF-8 、改行コード \n で保存されるのでWindowsで指定を忘れてしまうことがなく安全。
拡張子を .bat.cmd にした場合のみ、 UTF-8 では動作しないため自動的にShift-JIS( CP932 )と \r\n で保存される。

書き込みに保存先パスの指定ミス等で処理が失敗してもエラーで停止しない仕様。
もし失敗時に何をさせたい場合、全ての関数は書き込みに成功したら True 、失敗したら False を返すので、必要に応じて処理を分岐させることができる。

Licence

MIT

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

abara_file_io-1.0.1.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

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

abara_file_io-1.0.1-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file abara_file_io-1.0.1.tar.gz.

File metadata

  • Download URL: abara_file_io-1.0.1.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.13

File hashes

Hashes for abara_file_io-1.0.1.tar.gz
Algorithm Hash digest
SHA256 3817edf29757cd6ea96994434af907e573dda304dfb7e993109962ca2a7be152
MD5 1fa6ec15914c76595aeb00fe86e99098
BLAKE2b-256 2a81a41c33b296c109a5dbd4f407d46e14091ac6e046268de1a10bd8c9e5926c

See more details on using hashes here.

File details

Details for the file abara_file_io-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for abara_file_io-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ba18896c39adb35b393c01404975d5041b8111f6106760c1693ce2652daca82f
MD5 4d37cb7d656c52f9a9999a57d0ede435
BLAKE2b-256 6607527d188e8d4848bfb6c71d0e9270e1d0d4f4266fa0949bf737dd9285cba5

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