Skip to content

poziel/file-io

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fileio

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.

Installation

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.)

Usage

Here’s how to use the File class:

Import the module

from pz_fileio import File

Creating a File instance

f = File("path/to/your/file.txt")

Basic operations

  • 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

Reading and writing content

  • 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.")

JSON and CSV handling

  • 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"]])

Hashing and backup

  • 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}")

Context manager usage

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()

License

MIT License


Want to tweak this further (like adding examples for CSV, JSON, or advanced usage)? Let me know and I’ll extend it! 🚀

About

fileio is a lightweight, object-oriented utility library for convenient file management in Python.

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages