fileio is a lightweight, object-oriented utility library for convenient file management in Python. It provides a robust and intuitive API to perform file operations such as creation, reading, writing, appending, deleting, backup, and hashing, along with JSON and CSV support.
fileio is a standalone Python package. To use it, simply download the File.py module and place it in your project directory.
Alternatively, if you plan to publish it on PyPI, you could install it via pip:
pip install fileio(Assuming the package is published under that name.)
Here’s how to use the File class:
from pz_fileio import Filef = File("path/to/your/file.txt")-
Check if file exists
exists = f.Exists()
-
Create a new file
f.Create()
-
Delete a file
f.Delete()
-
Recreate the file (delete and create a new empty one)
f.Recreate()
-
Get basic information
print(f.GetBasename()) # filename only print(f.GetDirname()) # directory name print(f.GetAbsolutePath()) # absolute path print(f.GetFileSize()) # file size in bytes print(f.GetMimeType()) # file MIME type
-
Read the entire file content
content = f.Read()
-
Read lines
lines = f.ReadLines()
-
Append content
f.Append("This is a new line.")
-
Overwrite content
f.Overwrite("This will replace everything.")
-
Read as JSON
data = f.ReadAsJson()
-
Write as JSON
f.WriteAsJson({"name": "fileio", "version": 1.0})
-
Read as CSV
rows = f.ReadAsCsv()
-
Write as CSV
f.WriteAsCsv([["name", "value"], ["fileio", "awesome"]])
-
Calculate file hash
file_hash = f.Hash() # Defaults to SHA-256
-
Create a backup
backup_path = f.Backup() print(f"Backup created at: {backup_path}")
The File class implements the context management protocol (with statement). Use it for read-only scenarios:
with File("path/to/your/file.txt") as f:
content = f.Read()MIT License
Want to tweak this further (like adding examples for CSV, JSON, or advanced usage)? Let me know and I’ll extend it! 🚀