A simple, friendly Python library for file and directory operations.
Project description
pyfileops
pyfileops is a simple, friendly Python library for everyday file and directory operations.
No need to juggle os, shutil, pathlib, or zipfile — everything lives behind one clean object: fs.
from pyfileops import fs
fs.copy("report.pdf", "backup/")
fs.delete("temp.log")
print(fs.size("video.mp4"))
Installation
pip install pyfileops
Requires Python 3.8+. No external dependencies — only the standard library.
Quickstart
from pyfileops import fs
# ── Files ──────────────────────────────────────────────────────────────
fs.copy("notes.txt", "backup/") # copy into an existing folder
fs.copy("notes.txt", "backup/notes2.txt") # copy with a new name
fs.cut("draft.docx", "archive/") # move file
fs.rename("old.txt", "new.txt") # rename in place
fs.delete("trash.log") # delete a file
# ── Directories ────────────────────────────────────────────────────────
fs.mkdir("project/src/utils") # create (nested) directories
fs.rmdir("tmp") # remove directory and contents
# ── Compression ────────────────────────────────────────────────────────
fs.zip("project", "project_v1.zip") # zip a directory
fs.zip("report.pdf", "report.zip") # zip a single file
fs.unzip("project_v1.zip", "restored/") # extract a zip
# ── Queries ────────────────────────────────────────────────────────────
fs.exists("config.ini") # True or False
fs.size("video.mp4") # size in bytes (int)
fs.is_file("data.csv") # True or False
fs.is_dir("assets") # True or False
API Reference
File Operations
fs.copy(source, destination) → Path
Copies a file or directory to destination.
- If
destinationis an existing directory, the source is placed inside it. - If
destinationdoes not exist, it becomes the new file/folder name. - Missing intermediate directories are created automatically.
fs.copy("logo.png", "assets/") # → assets/logo.png
fs.copy("logo.png", "assets/icon.png") # → assets/icon.png
fs.copy("src/", "src_backup/") # copies whole directory tree
fs.cut(source, destination) → Path
Moves a file or directory to destination (copy + delete original).
fs.cut("uploads/photo.jpg", "gallery/")
fs.rename(path, new_name) → Path
Renames a file or directory in place (same parent directory).
To move to a different location, use cut().
fs.rename("report_draft.pdf", "report_final.pdf")
fs.delete(path) → None
Deletes a file or an entire directory tree.
fs.delete("temp.log") # file
fs.delete("cache/") # directory and all its contents
Directory Operations
fs.mkdir(path) → Path
Creates a directory (and any missing parent directories). Safe to call even if it already exists.
fs.mkdir("project/assets/images")
fs.rmdir(path) → None
Removes a directory and everything inside it.
fs.rmdir("build/")
Compression
fs.zip(source, zip_file) → Path
Compresses source (file or directory) into a ZIP archive at zip_file.
fs.zip("my_project", "my_project.zip")
fs.zip("data.csv", "data.zip")
fs.unzip(zip_file, destination) → Path
Extracts a ZIP archive into destination.
fs.unzip("my_project.zip", "restored/")
Queries
fs.exists(path) → bool
Returns True if path exists (file or directory).
if fs.exists("config.ini"):
print("Config found!")
fs.size(path) → int
Returns the size in bytes.
- For files: the file size.
- For directories: the total size of all contained files.
bytes_used = fs.size("video.mp4")
print(f"{bytes_used / 1_000_000:.2f} MB")
fs.is_file(path) → bool
Returns True if path is a regular file (returns False if it doesn't exist).
fs.is_file("README.md") # True
fs.is_file("src/") # False
fs.is_dir(path) → bool
Returns True if path is a directory (returns False if it doesn't exist).
fs.is_dir("src/") # True
fs.is_dir("README.md") # False
Error Handling
pyfileops raises clear, specific exceptions so you always know what went wrong.
| Exception | When it's raised |
|---|---|
FileOpsError |
Base class for all pyfileops errors |
PathNotFoundError |
Source path does not exist |
CopyError |
A copy or move operation failed |
DeleteError |
A delete operation failed |
ZipError |
A zip or unzip operation failed |
All exceptions inherit from FileOpsError, which inherits from Exception.
from pyfileops import fs
from pyfileops import PathNotFoundError, CopyError, ZipError, FileOpsError
# Catch a specific error
try:
fs.copy("missing.txt", "backup/")
except PathNotFoundError as e:
print(e) # Path not found: 'missing.txt'
# Catch any pyfileops error
try:
fs.zip("project", "output.zip")
fs.delete("tmp/")
except FileOpsError as e:
print(f"Operation failed: {e}")
Author
Made by Fede · github.com/FefinDev/pyfileops
License
Project details
Release history Release notifications | RSS feed
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 pyfileops-1.0.0.tar.gz.
File metadata
- Download URL: pyfileops-1.0.0.tar.gz
- Upload date:
- Size: 9.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d57a42791d60567d4ab87e22c7e5914c93dac1847475c74c59fa16f62888709f
|
|
| MD5 |
4e8fa7a9f945891528eb1c764667ce89
|
|
| BLAKE2b-256 |
7a11ac15ca5e6ce7d6888deaaf1f5583b694493df4f76ccaa8fbb1db92597eaa
|
File details
Details for the file pyfileops-1.0.0-py3-none-any.whl.
File metadata
- Download URL: pyfileops-1.0.0-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7fea8124edc3c4b19dee3d9f8512da9697ef1e97bfbaacff35eae86554b7ba41
|
|
| MD5 |
15d8447bcd496498dc2d7ee249f7af2e
|
|
| BLAKE2b-256 |
dcc1a15c02d202a1550732be10be1a822ca55be9cc709ee161941ed291ad733f
|