Skip to main content

Unified Python package for folder and file management

Project description

AviTwilTools

AviTwilTools is a Python utility library that provides a unified API for handling multiple file formats and an in-memory folder graph that mirrors your filesystem. It makes working with files and directories simple, consistent, and Pythonic.

Author: Avi Twil GitHub: avitwil/AT_tools PyPI: AviTwilTools


✨ Features

  • Unified File API All file types support: read(), write(data), append(data), copy(dst), move(dst), delete(), exist()

  • Supported file formats

    • TxtFile.txt
    • JsonFile.json
    • CsvFile.csv
    • YamlFile.yml, .yaml
    • XmlFile.xml
    • ByteFile.bin, .img, .exe, .jpg
    • DillFile.dill
    • VarDBFile.db (default scope = globals())
  • Folder abstraction

    • Each Folder object represents a real directory.
    • Instances are cached (singleton-per-path).
    • Auto-creates directories if missing.
    • Navigate with attributes (folder.sub) or indexing (folder["file.txt"]).
    • Print tree structure with print_tree().
    • Supports copy(), move(), remove(), recursive_delete(), etc.
  • Factory function file("name.ext") returns the correct File subclass automatically.


📦 Installation

Install directly from PyPI:

pip install AviTwilTools

No extra dependencies required.


⚡ Quick Examples

Text and JSON

from AviTwilTools import TxtFile, JsonFile

# TXT
txt = TxtFile("hello.txt")
txt.write("Hello")
txt.append(" World")
print(txt.read())   # -> "Hello World"

# JSON
j = JsonFile("data.json")
j.write({"a": 1})
j.append({"b": 2})
print(j.read())     # -> {"a": 1, "b": 2}

CSV, YAML, XML

from AviTwilTools import CsvFile, YamlFile, XmlFile
import xml.etree.ElementTree as ET

# CSV
c = CsvFile("table.csv")
c.write([["id", "name"], [1, "Alice"]])
c.append([2, "Bob"])
print(c.read())

# YAML
y = YamlFile("config.yaml")
y.write({"debug": True})
y.append({"workers": 4})
print(y.read())

# XML
root = ET.Element("root")
x = XmlFile("doc.xml")
x.write(root)
x.append(ET.Element("child"))
print(x.read().tag)   # -> "root"

Binary & Dill (Python objects)

from AviTwilTools import ByteFile, DillFile

# Binary file
b = ByteFile("blob.bin")
b.write(b"\x00\xFF")
b.append(b"\x10")
print(len(b.read()))

# Dill (Python object serialization)
d = DillFile("obj.dill")
d.write({"k": [1, 2, 3]})
print(d.read())

VarDBFile

from AviTwilTools import VarDBFile

x = 1
y = 2
db = VarDBFile("store.db",scope=globals())
db.write(x)
db.append(y)

print(db.read())   # -> {"x": 1, "y": 2}
print(x)           # x is injected into globals()

Folder Navigation

from AviTwilTools import Folder, TxtFile

root = Folder("project")
sub = Folder("project/folder1/folder2")

TxtFile("myfile.txt", sub).write("Nested Hello")

print(root.folder1.folder2["myfile.txt"].read())
# -> "Nested Hello"

root.print_tree()

Factory Function

from AviTwilTools import file

f = file("notes.txt")    # Returns TxtFile
f.write("Hello from factory")
print(f.read())

🧩 Mini Project Example

from AviTwilTools import Folder, TxtFile, JsonFile, CsvFile, YamlFile, file

root = Folder("project")
data = Folder("project/data")
reports = Folder("project/reports")

# Create files in different formats
TxtFile("readme.txt", root).write("This is my project")
JsonFile("config.json", root).write({"version": "1.0", "author": "Avi Twil"})
CsvFile("users.csv", data).write([["id", "name"], [1, "Alice"], [2, "Bob"]])
YamlFile("settings.yaml", data).write({"debug": True, "workers": 4})
file("log.txt", reports).write("System started...")

# Read files using folder navigation
print(root["readme.txt"].read())         
print(root["config.json"].read())        
print(root.data["users.csv"].read())     
print(root.data["settings.yaml"].read()) 
print(root.reports["log.txt"].read())    

# Show tree
root.print_tree()

Example output:

project/
├─ data/
│  ├─ users.csv
│  └─ settings.yaml
├─ reports/
│  └─ log.txt
├─ readme.txt
└─ config.json

📚 API Reference

📂 Folder

Represents a filesystem folder mirrored in memory. Each path is cached (singleton-per-path). Auto-creates directories if missing.

Constructor

Folder(path: str = None)

Key methods

  • add(item: File | Folder)
  • remove(folder_name: str)
  • move(target_folder: Folder)
  • copy(target_folder: Folder) -> Folder
  • recursive_delete()
  • parent() -> Folder
  • list_dir() -> dict
  • print_tree(prefix="", is_last=True)
  • _map_folder()

Special access

  • folder.subfolder
  • folder["file.txt"]
  • item in folder

📄 File (base class)

Defines a unified interface for all file types.

Common methods

  • read() -> Any
  • write(data: Any) -> None
  • append(data: Any) -> None
  • exist() -> bool
  • copy(destination: str) -> str
  • move(destination: str) -> str
  • delete() -> None

📑 TxtFile

  • read() -> str
  • write(data: str)
  • append(data: str)

📑 JsonFile

  • read() -> Any
  • write(data: Any)
  • append(data: dict | list)

📑 CsvFile

  • read() -> List[List[str]]
  • write(data: List[List[str]])
  • append(data: List[str])

📑 YamlFile

  • read() -> Any
  • write(data: Any)
  • append(data: dict | list)

📑 XmlFile

  • read() -> Element
  • write(data: Element)
  • append(data: Element)

📑 ByteFile

  • read() -> bytes
  • write(data: bytes)
  • append(data: bytes)

📑 DillFile

  • read() -> Any
  • write(data: Any)
  • append(data: Any)

📑 VarDBFile

  • Default scope = globals()
  • read(scope=None) -> dict
  • write(data: Any) – clears and writes.
  • append(data: Any) – adds to VariableDB.

🏭 Factory Function

from AviTwilTools import file

f = file("notes.txt")     # TxtFile
f2 = file("data.json")    # JsonFile
f3 = file("store.db")     # VarDBFile

📝 License

MIT License. See GitHub repo for details.

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

avitwiltools-1.0.5.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

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

avitwiltools-1.0.5-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file avitwiltools-1.0.5.tar.gz.

File metadata

  • Download URL: avitwiltools-1.0.5.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for avitwiltools-1.0.5.tar.gz
Algorithm Hash digest
SHA256 8d23f777945836fd548065ba0648e997730b97ab0739cc23f772e2515d3c83ab
MD5 096dd0e0b24a014d88f3f6be78e4e3fd
BLAKE2b-256 b1c146fe1637e11cfde1bbf1f4409395a21d9e03e5e8ebf3de3e099f561498a2

See more details on using hashes here.

File details

Details for the file avitwiltools-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: avitwiltools-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for avitwiltools-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 d054070ff44a9b15c4b0f3133905f06690ff38f7157ee5d2499f8fcdb444d944
MD5 de1730dca7ef2cc81d0c71715c86a77c
BLAKE2b-256 73ee3a86224d728ac59066dab4395d12b6bc6849906d2968138d677bcf1c2dc4

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