Skip to main content

Utility function to read and write files

Project description

file-captain

A lightweight Python utility library for reading and writing files with support for JSON, pickle and text formats.

Features

  • Simple API for file operations
  • Automatic format detection based on file extensions
  • Support for JSON, pickle, plain text, TOML and YAML files
  • Built-in error handling and logging
  • Overwrite protection for safe file operations
  • Type hints for better development experience

Installation

For End Users

Install directly from the repository:

pip install file-captain

For Development

  1. Clone the repository:
git clone https://github.com/philipgautschi/file-captain.git
cd file-captain
  1. Create a virtual environment:
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
  1. Install in development mode with optional dependencies:
pip install -e ".[dev]"

⚠️ Security Considerations

Pickle Files - Security Warning

CRITICAL: Loading pickle files from untrusted sources is a serious security risk.

# 🚨 DANGEROUS - Never do this with untrusted files
data = load_file("untrusted_file.pickle") # Could execute malicious code!
# ✅ SAFE - Only load pickle files you created or from trusted sources
data = load_file("my_own_data.pickle") # Safe if you created this file

Why pickle is dangerous:

  • Pickle can execute arbitrary Python code during deserialization
  • Malicious pickle files can compromise your system
  • There's no way to safely validate pickle content without loading it

Recommendations:

  • Never load pickle files from untrusted sources (internet, email, external systems)
  • Use JSON format for data exchange when possible
  • Validate file sources - only load pickles you created or from verified trusted sources

Quick Start

from file_captain import load_file, save_file

# Save data to a file
dict_data = {"host": "localhost", "port": 8080}
save_file("dict.json", dict_data)

serializable_object = {"key1": "text-key", 2: "number-key"}
save_file("object.pickle", serializable_object)

dict_data = {"host": "localhost", "port": 8080}
save_file("dict.toml", dict_data)

text_data = "Hello, world and some unicode characters! 🧠 字 Ω"
save_file("text.txt", text_data)

dict_data = {"host": "localhost", "port": 8080}
save_file("dict.yaml", dict_data)

# Save with overwrite protection disabled
dict_data_new = {"host": "localhost", "port": 8080, "user": "admin"}
save_file("dict.yaml", dict_data_new, overwrite_protection=False)

# Load data from a file
loaded_dict = load_file("dict.json")
print(loaded_dict)
loaded_object = load_file("object.pickle")
print(loaded_object)
loaded_dict = load_file("dict.toml")
print(loaded_dict)
loaded_text = load_file("text.txt")
print(loaded_text)
loaded_dict = load_file("dict.yaml")
print(loaded_dict)

API Reference

load_file(path_string)

Returns data from the file system. Autodetects format based on file extension.

Parameters:

  • path_string (str | Path): Path to the file (absolute or relative).

Returns:

  • JSONType: parsed JSON data for .json files.
  • Any: Deserialized python object for .pickle/.pkl files.
  • TOMLType: Parsed TOML data for .toml files.
  • str: Raw string content for .txt files and unknown file extensions.
  • YAMLType: Parsed YAML data for .yaml/.yml files.
  • None: If an error occurs during reading, parsing.

save_file(path_string, data, overwrite_protection=True)

Writes data to the file system. Autodetects format based on file extension.

Parameters:

  • path_string (str | Path): Path to the file (absolute or relative).
  • data (Any): Data to be written.
  • overwrite_protection (bool, optional): If True, prevents overwriting existing files; defaults to True.

Returns:

  • bool: True if writing was successful, False, otherwise.

Supported File Types

  • JSON files (.json): Automatically parsed and formatted with 4-space indentation
  • Pickle files (.pickle or .pkl): Can handle all serializable objects.
  • TOML files (.toml): Automatically parsed
  • Text files (.txt and others): Handled as plain text with UTF-8 encoding
  • YAML files (.yaml or .yml): Automatically parsed and formatted with 2-space indentation

Requirements

  • Python 3.11 or higher
  • External dependencies: tomli-w >=1.0.0 and pyyaml >= 6.0.0

Development Dependencies

The following packages are available for development:

  • pytest (testing)
  • pytest-cov (coverage)
  • black (code formatting)
  • mypy (type checking)
  • isort (import sorting)

Before a pull request make sure the following tests are succesfull.

Run tests:

pytest --cov

Run isort:

isort .

Run black:

black .

Run mypy:

mypy .

License

MIT License - Copyright (c) 2025 Philip Gautschi, Nicolas Brehm

See LICENSE file for full license text.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

Support

For issues or questions, please contact the maintainer or create an issue in the repository.

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

file_captain-1.0.3.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

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

file_captain-1.0.3-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file file_captain-1.0.3.tar.gz.

File metadata

  • Download URL: file_captain-1.0.3.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for file_captain-1.0.3.tar.gz
Algorithm Hash digest
SHA256 2eb15486f50e69fb8352798362c5dbd7f2b6c1aca939372a636c15815abd7754
MD5 f593f661c11967c5b64bd294cf8485fe
BLAKE2b-256 0b24871750392a50bded6d20345d3cbbe46441351bfeac152693cfa1d7ce3b94

See more details on using hashes here.

File details

Details for the file file_captain-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: file_captain-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for file_captain-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9a99f19a945db7f7e36da5d787e65e7d6a0badd4022dfef1aee840aa89d921ce
MD5 2f119ee274179c41beeddf102c64ff17
BLAKE2b-256 49881990905342df0eae5a15176d3fe5036929d45b21a6cbbb49e395abcc3d92

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