Powerful and intuitive Python library for working with files, directories, and text data
Project description
FileAlchemy
FileAlchemy v. 2.1.1 — A powerful and intuitive Python library for working with files, directories, and text data. Provides two API styles: shell-style with automatic path expansion (UniShell) and Python-style (structures module).
Two API Styles
1. UniShell — Shell-style with Automatic Path Expansion
High-level interface inspired by Unix/Cmd command-line. Automatically expands paths with environment variables (%VAR%), special notations (~, .., .), and supports method chaining.
2. Structures — Python-style
Low-level classes for file system operations in object-oriented style. Full control over operations, without automatic path expansion.
Installation
pip install FileAlchemy
For Windows-specific features:
pip install FileAlchemy[windows]
Requirements:
- Python 3.10+ (3.11+ recommended)
- Core dependencies:
chardet,py7zr,pyzipper,rarfile - For Windows:
pywin32(optional, install with[windows])
Quick Start
UniShell API (Shell-style)
from FileAlchemy import UniShell
# Initialization
shell = UniShell(
current_dir=".", # Initial working directory
default_encoding="utf-8", # Default encoding
autodetect_encoding=False, # Auto-detect encoding (requires chardet)
sep="\n" # Separator for multi-file operations
)
# Automatic path expansion
shell.file("~/documents/test.txt") # Automatically expands ~
shell.file("%CURRENTDIR%/data.txt") # Uses environment variables
shell.file("../parent/file.txt") # Works with relative paths
# Working with files
shell.file("test.txt").content = "Hello, world!"
content = shell.file("test.txt").content
# Method chaining
shell.mkdir("backup").copy("data.txt", "backup/data.txt").make_archive("backup", "backup.zip")
# Working with multiple files
shell.files("1.txt", "2.txt", "3.txt") >> shell.file("combined.txt")
Structures API (Python-style)
from FileAlchemy.structures import File, Files, Dir, Archive
from pathlib import Path
# Working with file
file = File("test.txt")
file.content = "Hello, world!"
print(file.content)
print(file.sizeof()) # Size in bytes
print(file.metadata()) # All metadata
# Working with multiple files
files = Files("1.txt", "2.txt", "3.txt", sep="\n---\n")
combined = files.content # Combined content
# Working with directory
dir = Dir("my_project")
dir.create()
for item in dir: # Iterate over contents
print(item)
# Working with archives
archive = Archive("data.zip")
archive.extract("extracted/")
archive.add("new_file.txt")
print(list(archive.list_files()))
Detailed API Documentation
UniShell API
Initialization
shell = UniShell(
sep="\n", # Separator for files()
current_dir=os.getcwd(), # Current working directory
default_encoding="utf-8", # Default encoding
autodetect_encoding=False, # Auto-detect encoding
parms=ViewPort() # Environment variables object
)
Automatic Path Expansion
UniShell automatically expands paths:
~→ user's home directory..→ parent of current working directory.→ current working directory%VAR%→ environment variables fromshell.parms
shell.parms["MY_PATH"] = "/custom/path"
shell.file("%MY_PATH%/file.txt") # Automatically expanded
UniShell Methods
Creating File Objects
-
file(path, encoding=None)→File- Creates File object with automatic path expansion
- If
encodingnot specified, usesdefault_encodingor auto-detection (if enabled)
-
files(*files, encoding=None, sep="\n")→Files- Creates Files object for working with multiple files
sep— separator when combining content
File Operations
mkfile(path)/touch(path)— create empty filermfile(path)— delete filenano(path, edit_txt="notepad")— open file in text editorrecode(file_path, to_encoding, from_encoding=None)— re-encode file
Directory Operations
mkdir(path, mode=0o777, parents=False, exist_ok=False)— create directoryrmdir(path)— delete directory (recursively)ls(path=".", details=False)— list files and directories- If
details=True, returns dictionary with metadata
- If
File and Directory Operations
copy(from_path, to_path, follow_symlinks=True)— copyremove(path)/rm(path)— delete file or directorymake(path, is_file=None)— create full path (all parent directories)chmod(path, mode)— change access permissions
Archive Operations
-
make_archive(from_path, to_path=None, format="zip")/mkarch()/mk_archive()- Create archive from file or directory
- If
to_pathnot specified, created next to source
-
extract_archive(archive_path, extract_dir=None, format=None)/unparch()/extarch()- Extract archive
- If
extract_dirnot specified, extracts to current directory
Path Operations
cd(path)— change current working directoryto_abspath(path)— convert path to absolute with variable expansion
Environment Variables (shell.parms)
parms["VAR"] = value— set local variableparms.sets({"VAR1": val1, "VAR2": val2})— set multiple variablesparms.set_gl("VAR") = value— set global variableparms.del("VAR")— delete variableparms.all()— list all variables
All UniShell methods return self for method chaining and have ignore_errors=False parameter.
UniShell Method Aliases
touch=mkfilerm=removermtree=rmdirmkarch/mk_archive=make_archiveunparch/unp_arch/extarch/extarch/unpack_archive=extract_archiveconvert_encoding=recode
Structures API
File Class
Class for working with a single file.
Initialization
file = File(path, encoding=None)
# path can be: str, Path, or another File object
Properties
-
content(str) — file content (read/write/delete)- On read, automatically detects encoding on error
- On write, automatically selects minimal suitable encoding
del file.content— clear content
-
path(Path) — file path -
encoding(str) — file encoding -
name(str) — file name -
extension(str) — file extension (all suffixes) -
parent(Dir) — parent directory -
hidden(bool) — whether file is hidden
Methods
create(mode=438, ignore_errors=True)— create empty fileremove()— delete filerecode(to_encoding=None, from_encoding=None)— re-encode- If
to_encodingnot specified, determines minimal suitable encoding
- If
chmod(mode)— change access permissionsnano(edit_txt="notepad")— open in text editoron_disk()→ bool — whether file exists on disksizeof()→ int — size in bytescreated_utc()→ datetime — creation time (UTC)modified_utc()→ datetime — modification time (UTC)accessed_utc()→ datetime — access time (UTC)created_lcl()→ datetime — creation time (local)modified_lcl()→ datetime — modification time (local)accessed_lcl()→ datetime — access time (local)is_symlink()→ bool — whether is symbolic linkmetadata()→ dict — all file metadata
Files Class
Class for working with multiple files.
Initialization
files = Files(*files, encoding=None, sep="\n")
# files can be: str, Path, or File
Properties
content(str) — combined content of all files separated bysepfiles(list[File]) — list of File objectssep(str) — separator
Dir Class
Class for working with directories.
Initialization
dir = Dir(path)
# path can be: str, Path, or another Dir object
Properties
path(Path) — directory pathname(str) — directory name (can be modified)parent(Dir) — parent directory (can be modified to move)hidden(bool) — whether directory is hidden (can be modified)
Methods
create(mode=0o777, parents=False, ignore_errors=False)— create directoryadd(path)— add file or directory to directory (copy)move_to(dir)— move directoryrename(new_name)— renamechmod(mode)— change access permissionson_disk()→ bool — whether directory existssizeof(recursive=True, symlink=False)→ int — size in byteslen_files_and_dirs(recursive=True, symlinks=False)→ dict — item countcreated_utc()→ datetime — creation time (UTC)modified_utc()→ datetime — modification time (UTC)accessed_utc()→ datetime — access time (UTC)created_lcl()→ datetime — creation time (local)modified_lcl()→ datetime — modification time (local)accessed_lcl()→ datetime — access time (local)is_symlink()→ bool — whether is symbolic linkmetadata()→ dict — all directory metadata
Operators
dir / "filename"→ File or Dir — access file/subdirectoryfor item in dir:— iterate over contents (returns File or Dir)
Archive Class
Universal interface for working with archives.
Supported Formats
- zip — create, add, extract (with passwords via pyzipper)
- 7z — create, add, extract (with passwords)
- tar — create, add, extract
- tar.gz / tgz — create, add, extract
- tar.bz2 / tbz2 — create, add, extract
- gz — read and extract only (single file)
- bz2 — read and extract only (single file)
- rar — read and extract only (with passwords, requires rarfile)
Initialization
archive = Archive(path, format=None, password=None)
# format is automatically determined by extension if not specified
Methods
add(path, arcname=None)— add file or directory to archiveextract(path, member=None)— extract archive or specific memberlist_files()→ list[str] — list files in archivecreate()— create empty archivecleanup()— remove temporary files (for 7z and tar)
Class Methods
Archive.create_from(path, format, files, password=None)— create archive with files
Iteration
for filename in archive:
print(filename) # File names in archive
Stream Operators (>>, >, <<, <)
File and Files classes support operators for content manipulation:
a > b— copies content fromatob(overwritesb)a >> b— appends content fromato end ofb(respectssep)a < b— copies content frombtoa(overwritesa)a << b— appends content frombto end ofa(respectssep)
from FileAlchemy.structures import File, Files
file1 = File("a.txt")
file2 = File("b.txt")
file1 > file2 # Copies content from a.txt to b.txt
files = Files("1.txt", "2.txt")
combined = File("combined.txt")
files >> combined # Appends content from all files to combined.txt
Usage Examples
Example 1: Backup (UniShell)
from FileAlchemy import UniShell
import datetime
shell = UniShell()
today = datetime.datetime.now().strftime("%Y-%m-%d")
shell.make_archive("project", f"backups/project_{today}.zip")
Example 2: Logging (Structures)
from FileAlchemy.structures import File
import datetime
log_file = File("app.log")
log_entry = f"{datetime.datetime.now()}: New event\n"
log_file.content = log_file.content + log_entry
Example 3: Working with Config Files (UniShell)
from FileAlchemy import UniShell
import json
shell = UniShell()
config = shell.file("config.json").content
settings = json.loads(config)
settings["timeout"] = 30
shell.file("config.json").content = json.dumps(settings, indent=2)
Example 4: Processing Multiple Files (Structures)
from FileAlchemy.structures import Files, File
files = Files("file1.txt", "file2.txt", "file3.txt", sep="\n---\n")
all_content = files.content # Combined content
File("combined.txt").content = all_content
Example 5: Working with Archives (Structures)
from FileAlchemy.structures import Archive
# Create archive
archive = Archive("backup.zip")
archive.add("file1.txt")
archive.add("directory/")
# Extract
archive.extract("extracted/")
# List files
print(archive.list_files())
Windows-Specific Features
The FileAlchemy.Windows.regedit module provides Windows registry and user account management (requires pywin32):
from FileAlchemy.Windows.regedit import CurrentUser, User, Users, PATH, AutoRun
# Current user
cur_user = CurrentUser()
print(cur_user.PATH.all()) # Current user's PATH
# Working with PATH
cur_user.PATH.add("C:/new/path")
cur_user.PATH.pop("C:/old/path")
# AutoRun
cur_user.AutoRun.add("MyApp", "C:/app.exe")
cur_user.AutoRun.remove("MyApp")
# Working with users
user = User(name="username")
users = Users.local() # List of local users
Implementation Features
- Lazy operations — files are read only when needed
- Auto-recovery — automatic encoding detection on read errors
- Safety — file existence checks before operations
- Cross-platform — unified API for Windows, Linux, and macOS
- Two-level API — shell-style (UniShell) and Python-style (structures)
- Stream operations — support for >>, >, <<, < operators for content manipulation
License
FileAlchemy is distributed under the MIT License. See LICENSE file for details.
Dependency Licenses
The project uses the following dependencies with their licenses:
- chardet — LGPL
- py7zr — LGPL-2.1-or-later
- pyzipper — MIT
- rarfile — ISC
- pywin32 (optional) — PSF (Python Software Foundation License)
All dependencies are compatible with MIT License. For detailed information, see LICENSES_DEPENDENCIES.md.
Author
GimpNiK (https://github.com/GimpNiK) — Developer and maintainer of the project.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file unishell-2.1.3.tar.gz.
File metadata
- Download URL: unishell-2.1.3.tar.gz
- Upload date:
- Size: 56.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
18c212aba0f1ec80ff50b520144f6de47806a61d889a221fa862f01358fe1bbb
|
|
| MD5 |
289c1c7582dd20b005a6f2a90d880581
|
|
| BLAKE2b-256 |
6c69d068c0b804f3e6a03e48cfec79345b4fa572a83a43f0c633f3485cf34f4c
|
Provenance
The following attestation bundles were made for unishell-2.1.3.tar.gz:
Publisher:
python-publish.yml on GimpNiK/UniShell
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
unishell-2.1.3.tar.gz -
Subject digest:
18c212aba0f1ec80ff50b520144f6de47806a61d889a221fa862f01358fe1bbb - Sigstore transparency entry: 774607729
- Sigstore integration time:
-
Permalink:
GimpNiK/UniShell@bcf23c9008c234a78c93a8a073d54a25ddde579f -
Branch / Tag:
refs/tags/v2.1.3 - Owner: https://github.com/GimpNiK
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@bcf23c9008c234a78c93a8a073d54a25ddde579f -
Trigger Event:
release
-
Statement type:
File details
Details for the file unishell-2.1.3-py3-none-any.whl.
File metadata
- Download URL: unishell-2.1.3-py3-none-any.whl
- Upload date:
- Size: 58.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fe35e1262eca23673f4c33cf97174ed2a4d5b32d7e35dc6989f7441dadcc59e1
|
|
| MD5 |
43dbbedf1cb1720a18641af04c8ffd78
|
|
| BLAKE2b-256 |
1118f89930bb5cfd4c20dfe5b789f38a890b637417312db0758e9599c0a7cd60
|
Provenance
The following attestation bundles were made for unishell-2.1.3-py3-none-any.whl:
Publisher:
python-publish.yml on GimpNiK/UniShell
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
unishell-2.1.3-py3-none-any.whl -
Subject digest:
fe35e1262eca23673f4c33cf97174ed2a4d5b32d7e35dc6989f7441dadcc59e1 - Sigstore transparency entry: 774607731
- Sigstore integration time:
-
Permalink:
GimpNiK/UniShell@bcf23c9008c234a78c93a8a073d54a25ddde579f -
Branch / Tag:
refs/tags/v2.1.3 - Owner: https://github.com/GimpNiK
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@bcf23c9008c234a78c93a8a073d54a25ddde579f -
Trigger Event:
release
-
Statement type: