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.1.tar.gz (9.3 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.1-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: file_captain-1.0.1.tar.gz
  • Upload date:
  • Size: 9.3 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.1.tar.gz
Algorithm Hash digest
SHA256 7bbf61d79f7dc4a4daad75ad7bd3bee7802e8a38443ed19612cc57e9fa31737d
MD5 f089f7256440b9b75b88d47b09b96667
BLAKE2b-256 4b0b3ce97a2b504bcf7db6169294e07094bc6f39f5eb89c58c1dcef0b8366028

See more details on using hashes here.

File details

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

File metadata

  • Download URL: file_captain-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.0 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8716d23808b5db32ac7c2c20ec4cfaab68decea535e33757457563a37169393a
MD5 9216166f42d1eb292d36613bbb7936ac
BLAKE2b-256 cda2b39b0ae52c12b3467f8697a3006b1b4515e8e46b3674b3a1e2fdf0c835ec

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