Library for working with paths to files and folders
Cross-platform utility for path normalization, directory traversal, and file management.
smartpathlibrary — a cross-platform library for working with paths to files and folders. Provides tools for path normalization, directory traversal, file management, and recursive file discovery.
Key capabilities: Check file/folder existence, normalize paths for different operating systems, traverse directories recursively, count files and folders, and manage path collections.
Normalize paths for different operating systems:
from smartpathlibrary.tools import PathNormalizer
# Path normalization for different OS
normalized = PathNormalizer.normalize("/home/user/my file.txt")
# On Linux: '/home/user/my\ file.txt'
# On Windows: '/home/user/my file.txt'
from smartpathlibrary.tools import File, Folder
# Check existence
file = File("document.txt")
print(file.exists()) # False or True
folder = Folder("/home/user/docs")
print(folder.exists())
from smartpathlibrary.tools import Dir
dir_obj = Dir("/home/user/project")
# Get all files recursively
for file in dir_obj.get_files(recursive=True):
print(file)
# Only files in root folder (no subfolders)
for file in dir_obj.get_files(recursive=False):
print(file)
# Get all subdirectories
for subdir in dir_obj.get_dirs(recursive=True):
print(subdir)
# Count
print(f"Files: {dir_obj.get_count_files(recursive=True)}")
print(f"Folders: {dir_obj.get_count_dirs(recursive=True)}")
Manage multiple paths in one collection:
from smartpathlibrary.tools import PathManager
pm = PathManager()
# Add single paths
pm.add_path("/home/user/project")
pm.add_path("/home/user/notes.txt")
# Add multiple paths
pm.add_paths([
"/home/user/docs",
"/home/user/readme.md"
])
# Get all files (recursive through all added folders)
all_files = list(pm.get_files(recursive=True))
print(f"Files found: {len(all_files)}")
# Get all subfolders
all_dirs = list(pm.get_dirs(recursive=True))
# Statistics
print(f"Total paths added: {pm.count}")
print(f"Total files (recursive): {pm.get_count_files()}")
print(f"Total folders: {pm.get_count_dirs()}")
# Remove paths
pm.remove_path("/home/user/notes.txt")
pm.remove_paths(["/home/user/docs"])
print(pm) # Paths(2)
pip install smartpathlibrary
from smartpathlibrary.tools import PathManager, Dir, File, get_root_path, PathNormalizer, Counter, Folder
# ========== WORK WITH PATHS ==========
# Check existence
file = File("document.txt")
print(file.exists()) # False or True
folder = Folder("/home/user/docs")
print(folder.exists())
# Path normalization for different OS
normalized = PathNormalizer.normalize("/home/user/my file.txt")
# ========== DIRECTORY TRAVERSAL ==========
dir_obj = Dir("/home/user/project")
# Get all files recursively
for file in dir_obj.get_files(recursive=True):
print(file)
# Count
print(f"Files: {dir_obj.get_count_files(recursive=True)}")
print(f"Folders: {dir_obj.get_count_dirs(recursive=True)}")
# ========== PATH MANAGER ==========
pm = PathManager()
pm.add_path("/home/user/project")
pm.add_path("/home/user/notes.txt")
all_files = list(pm.get_files(recursive=True))
print(f"Files found: {len(all_files)}")
print(f"Total paths added: {pm.count}")
print(f"Total files (recursive): {pm.get_count_files()}")
# ========== GET ABSOLUTE PATH ==========
abs_path = get_root_path("data/config.json")
print(abs_path) # /full/path/to/script/folder/data/config.json
# ========== COUNT ITEMS IN ITERABLE ==========
counter = Counter()
items = [1, 2, 3, 4, 5]
print(counter.get_count(items)) # 5
| Class/Function | Description |
|---|---|
PathManager | Manage multiple paths in one collection |
Dir | Directory operations and traversal |
File | File existence and operations |
Folder | Folder existence and operations |
PathNormalizer | Cross-platform path normalization |
Counter | Count items in iterables |
get_root_path() | Get absolute path relative to script |
By using this software, you agree to the full disclaimer terms.
Software provided "AS IS" without warranty. You assume all risks.
Full legal disclaimer: See DISCLAIMER.md
License: BSD 3-Clause License