Manage files more easily
Project description
doFolder
doFolder is a powerful, intuitive, and cross-platform file system management library that provides a high-level, object-oriented interface for working with files and directories. Built on Python's pathlib, it simplifies common file operations while offering advanced features like hashing, content manipulation, and directory tree operations.
✨ Key Features
- 🎯 Object-oriented Design: Work with files and directories as Python objects
- 🌐 Cross-platform Compatibility: Seamlessly works on Windows, macOS, and Linux
- 🛤️ Advanced Path Handling: Built on Python's pathlib for robust path management
- 📁 Complete File Operations: Create, move, copy, delete, and modify files and directories
- 📝 Content Management: Read and write file content with encoding support
- 🌳 Directory Tree Operations: Navigate and manipulate directory structures
- 🔍 File Comparison: Compare files and directories with various comparison modes
- 🔒 Hash Support: Generate and verify file hashes for integrity checking
- ⚠️ Flexible Error Handling: Comprehensive error modes for different use cases
- 🏷️ Type Safety: Full type hints for better IDE support and code reliability
📦 Installation
pip install doFolder
Requirements: Python 3.8+
🚀 Quick Start
from doFolder import File, Directory, ItemType
# Create directory and file objects
project_dir = Directory("./my_project")
config_file = project_dir["config.json"]
# Create a new file in the directory
readme = project_dir.create("README.md", ItemType.FILE)
readme_zh = project_dir.createFile("README.zh-cn.md")
# Write content to the file
readme.content = "# My Project\n\nWelcome to my project!".encode("utf-8")
# Create a subdirectory
src_dir = project_dir.create("src", ItemType.DIR)
# Copy and move files
backup_config = config_file.copy("./backup/")
config_file.move("./settings/")
# List directory contents
for item in project_dir:
print(f"{item.name} ({'Directory' if item.isDir else 'File'})")
📖 Usage Examples
Working with Files
from doFolder import File
# Create a file object
file = File("data.txt")
# Work with binary content
print(file.content) # Reads content as bytes
file.content = "Binary data here".encode("utf-8") # Writes content as bytes
# JSON operations
file.saveAsJson({"name": "John", "age": 30})
data = file.loadAsJson()
# Quickly open file
with file.open("w", encoding="utf-8") as f:
f.write("Hello, World!")
# File information
print(f"Size: {file.state.st_size} bytes")
print(f"Modified: {file.state.st_mtime}")
# File hashing
print(f"Hash: {file.hash()}")
Working with Directories
from doFolder import Directory, ItemType
# Create a directory object
d = Directory("./workspace")
# Create nested directory structure
d.create("src/utils", ItemType.DIR)
d.create("tests", ItemType.DIR)
d.createDir("docs")
d.createFile("README.md")
# Create files
main_file = d.create("src/main.py", ItemType.FILE)
test_file = d.create("tests/test_main.py", ItemType.FILE)
# List all items (non-recursive)
for item in d:
print(item.path)
# List all items recursively
for item in d.recursiveTraversal(hideDirectory=False):
print(f"{'📁' if item.isDir else '📄'} {item.path}")
# Find specific sub items
py_files = ['__init__.py']
Advanced Operations
from doFolder import File, Directory, compare
# File comparison
file1 = File("version1.txt")
file2 = File("version2.txt")
if compare.compare(file1, file2):
print("Files are identical")
else:
print("Files differ")
# Directory comparison
dir1 = Directory("./project_v1")
dir2 = Directory("./project_v2")
diff=getDifference(dir1, dir2)
# Hash verification
file = File("important_data.txt")
original_hash = file.hash()
# ... file operations ...
if file.hash() == original_hash:
print("File integrity verified")
# Safe operations with error handling
from doFolder import UnExistsMode
safe_file = File("might_not_exist.txt", unExists=UnExistsMode.CREATE)
# File will be created if it doesn't exist
Path Utilities
from doFolder import Path
# Enhanced path operations
path = Path("./documents/projects/my_app/src/main.py")
print(f"Project root: {path.parents[3]}") # ./documents/projects/my_app
print(f"Relative to project: {path.relative_to_parent(3)}") # src/main.py
print(f"Extension: {path.suffix}") # .py
print(f"Filename: {path.stem}") # main
# Path manipulation
config_path = path.sibling("config.json") # Same directory, different file
backup_path = path.with_name(f"{path.stem}_backup{path.suffix}")
🔧 Advanced Features
Error Handling Modes
doFolder provides flexible error handling through UnExistsMode:
from doFolder import File, UnExistsMode
# Different modes for handling non-existent files
file1 = File("missing.txt", unExistsMode=UnExistsMode.ERROR) # Raises exception
file2 = File("missing.txt", unExistsMode=UnExistsMode.WARN) # Issues warning
file3 = File("missing.txt", unExistsMode=UnExistsMode.IGNORE) # Silent handling
file4 = File("missing.txt", unExistsMode=UnExistsMode.CREATE) # Creates if missing
File System Item Types
from doFolder import ItemType, createItem
# Factory function to create appropriate objects
item1 = createItem("./some_path", ItemType.FILE) # Creates File object
item2 = createItem("./some_path", ItemType.DIR) # Creates Directory object
item3 = createItem("./some_path") # Auto-detects type
🔄 Migration from v1.x.x
doFolder v2.x.x introduces several improvements while maintaining backward compatibility:
- Enhanced Path Management: Now uses Python's built-in
pathlib - Renamed Classes:
Folder→Directory(backward compatibility maintained) - Flexible File Creation:
Fileclass can handle directory paths with redirection - Improved Type Safety: Full type hints throughout the codebase
Migration Example
# v1.x.x style (still works)
from doFolder import Folder
folder = Folder("./my_directory")
# v2.x.x recommended style
from doFolder import Directory
directory = Directory("./my_directory")
# Both work identically!
📚 Documentation
- Full API Documentation: https://do-folder.doc.kuankuan.site
- GitHub Repository: https://github.com/kuankuan2007/do-folder
- Issue Tracker: https://github.com/kuankuan2007/do-folder/issues
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
📄 License
This project is licensed under the MulanPSL-2.0 License - see the LICENSE file for details.
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 dofolder-2.2.5.tar.gz.
File metadata
- Download URL: dofolder-2.2.5.tar.gz
- Upload date:
- Size: 36.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9e8cd44ed80e38b54cc29be84ffc2844904436e910a2f3644c94a40045949928
|
|
| MD5 |
d4133cb590fce420f2d5d119a98eb387
|
|
| BLAKE2b-256 |
5c8c10a0d1fc4e2e63194c998ce122e689b9ed4f5b68ce47f96d901fb7255094
|
Provenance
The following attestation bundles were made for dofolder-2.2.5.tar.gz:
Publisher:
python-package-publish.yml on kuankuan2007/do-folder
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dofolder-2.2.5.tar.gz -
Subject digest:
9e8cd44ed80e38b54cc29be84ffc2844904436e910a2f3644c94a40045949928 - Sigstore transparency entry: 404044483
- Sigstore integration time:
-
Permalink:
kuankuan2007/do-folder@ac2dc22a65463d06003f1be7acb86de4d528f270 -
Branch / Tag:
refs/tags/v2.2.5 - Owner: https://github.com/kuankuan2007
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-package-publish.yml@ac2dc22a65463d06003f1be7acb86de4d528f270 -
Trigger Event:
release
-
Statement type:
File details
Details for the file dofolder-2.2.5-py3-none-any.whl.
File metadata
- Download URL: dofolder-2.2.5-py3-none-any.whl
- Upload date:
- Size: 36.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df0fb3a143d2b49c81f84344d7a97562c05f1766c60ed89ac44fce3307c6f6f8
|
|
| MD5 |
b56817c90007152799977e05d783fa0e
|
|
| BLAKE2b-256 |
191140de032422f3fcdbb2cdefcae7adcb23de8b373fc5dec86b4c171c4db6d2
|
Provenance
The following attestation bundles were made for dofolder-2.2.5-py3-none-any.whl:
Publisher:
python-package-publish.yml on kuankuan2007/do-folder
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dofolder-2.2.5-py3-none-any.whl -
Subject digest:
df0fb3a143d2b49c81f84344d7a97562c05f1766c60ed89ac44fce3307c6f6f8 - Sigstore transparency entry: 404044485
- Sigstore integration time:
-
Permalink:
kuankuan2007/do-folder@ac2dc22a65463d06003f1be7acb86de4d528f270 -
Branch / Tag:
refs/tags/v2.2.5 - Owner: https://github.com/kuankuan2007
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-package-publish.yml@ac2dc22a65463d06003f1be7acb86de4d528f270 -
Trigger Event:
release
-
Statement type: