inherits from pathlib.Path with methods for hashing, copying, deleting and more
Project description
pathlibutil
pathlibutil.Path
inherits from pathlib.Path
with some useful built-in python functions from shutil
and hashlib
Path.hexdigest()
to calculate andPath.verify()
for verification of hexdigest from a filePath.default_hash
to configurate default hash algorithm forPath
class (default: 'md5')Path.size()
to get size in bytes of a file or directorybyteint
function decorator converts the return value ofint
to aByteInt
object
Path.read_lines()
to yield over all lines from a file until EOFcontextmanager
to change current working directory withwith
statementPath.copy()
copy a file or directory to a new path destinationPath.delete()
delete a file or directory-treePath.move()
move a file or directory to a new path destinationPath.make_archive()
creates andPath.unpack_archive()
uncompresses an archive from a file or directoryPath.archive_formats
to get all available archive formatsPath.stat()
returns aStatResult
object to get file or directory information containingTimeInt
objects foratime
,ctime
,mtime
andbirthtime
ByteInt
object forsize
Path.relative_to()
to get relative path from a file or directory,walk_up
to walk up the directory tree.Path.with_suffix()
to change the multiple suffixes of a filePath.cwd()
to get the current working directory or executable path when script is bundled, e.g. withpyinstaller
Path.resolve()
to resolve a unc path to a mapped windows drive.Path.walk()
to walk over a directory tree likeos.walk()
Path.iterdir()
withrecursive
all files from the directory tree will be yielded andexclude_dirs
via callable.
JSON serialization of Path
objects is supported in pathlibutil.json
.
pathlibutil.json.dumps()
andpathlibutil.json.dump()
to serializePath
objects as posix paths.
Installation
pip install pathlibutil
7zip support
to handle 7zip archives, an extra package py7zr>=0.20.2
is required!
# install as extra dependency
pip install pathlibutil[7z]
Usage
from pathlibutil import Path
readme = Path('README.md')
print(f'File size: {readme.size()} Bytes')
Example 1
Read a file and print its content and some file information to stdout.
Path.read_lines()
from pathlibutil import Path
readme = Path("README.md")
print(f"File size: {readme.size()} Bytes")
print(f'File sha1: {readme.hexdigest("sha1")}')
print("File content".center(80, "="))
for line in readme.read_lines(encoding="utf-8"):
print(line, end="")
print("EOF".center(80, "="))
Example 2
Write a file with md5 checksums of all python files in the pathlibutil-directory.
Path.hexdigest()
from pathlibutil import Path
file = Path("pathlibutil.md5")
with file.open("w") as f:
f.write(
f"# MD5 checksums generated with pathlibutil "
"(https://pypi.org/project/pathlibutil/)\n\n"
)
i = 0
for i, filename in enumerate(Path("./pathlibutil").glob("*.py"), start=1):
f.write(f"{filename.hexdigest()} *{filename}\n")
print(f"\nwritten: {i:>5} {file.default_hash}-hashes to: {file}")
Example 3
Read a file with md5 checksums and verify them.
Path.verify()
,Path.default_hash
andcontextmanager
from pathlibutil import Path
file = Path("pathlibutil.md5")
def no_comment(line: str) -> bool:
return not line.startswith("#")
with file.parent as cwd:
miss = 0
ok = 0
fail = 0
for line in filter(no_comment, file.read_lines()):
try:
digest, filename = line.strip().split(" *")
verification = Path(filename).verify(digest, "md5")
except ValueError as split_failed:
continue
except FileNotFoundError as verify_failed:
tag = "missing"
miss += 1
else:
if verification:
tag = "ok"
ok += 1
else:
tag = "fail"
fail += 1
print(f'{tag.ljust(len(digest), ".")} *{filename}')
print(f"\nok: {ok:<5} fail: {fail:<5} missing: {miss}")
Example 4
Search all pycache directories and free the memory and display the number of deleted directories and the amount of memory freed in MB.
Path.delete()
,Path.size()
andByteInt
from pathlibutil import ByteInt, Path
mem = ByteInt(0)
i = 0
for i, cache in enumerate(Path(".").rglob("*/__pycache__/"), start=1):
cache_size = cache.size()
try:
cache.delete(recursive=True)
except OSError:
print(f"Failed to delete {cache}")
else:
mem += cache_size
print(f"{i} cache directories deleted, {mem:.1mb} MB freed.")
Example 5
Inherit from pathlibutil.Path
to register new a archive format. Specify a
archive
as keyword argument in the new subclass, which has to be the suffix
without .
of the archives. Implement a classmethod _register_archive_format()
to register new archive formats.
Path.make_archive(), Path.archive_formats and Path.move()
import shutil
import pathlibutil
class RegisterFooBarFormat(pathlibutil.Path, archive="foobar"):
@classmethod
def _register_archive_format(cls):
"""
implement new register functions for given `archive`
"""
try:
import required_package_name
except ModuleNotFoundError:
raise ModuleNotFoundError("pip install <required_package_name>")
def pack_foobar(
base_name, base_dir, owner=None, group=None, dry_run=None, logger=None
) -> str:
"""callable that will be used to unpack archives.
Args:
base_name (`str`): name of the file to create
base_dir (`str`): directory to start archiving from, defaults to `os.curdir`
owner (`Any`, optional): as passed in `make_archive(*args, owner=None, **kwargs)`. Defaults to None.
group (`Any`, optional): as passed in `make_archive(*args, group=None, **kwargs)`. Defaults to None.
dry_run (`Any`, optional): as passed in `make_archive(*args, dry_run=None, **kwargs)`. Defaults to None.
logger (`logging.Logger`, optional): as passed in `make_archive(*args, logger=None, **kwargs)`. Defaults to None.
Returns:
str: path of the new created archive
"""
raise NotImplementedError("implement your own pack function")
def unpack_foobar(archive, path, filter=None, extra_args=None) -> None:
"""callable that will be used to unpack archives.
Args:
archive (`str`): path of the archive
path (`str`): directory the archive must be extracted to
filter (`Any`, optional): as passed in `unpack_archive(*args, filter=None, **kwargs)`. Defaults to None.
extra_args (`Sequence[Tuple[name, value]]`, optional): additional keyword arguments, specified by `register_unpack_format(*args, extra_args=None, **kwargs)`. Defaults to None.
"""
raise NotImplementedError("implement your own unpack function")
shutil.register_archive_format(
"foobar", pack_foobar, description="foobar archives"
)
shutil.register_unpack_format("foobar", [".foo.bar"], unpack_foobar)
file = pathlibutil.Path("README.md")
print(f"available archive formats: {file.archive_formats}")
archive = file.make_archive("README.foo.bar")
backup = archive.move("./backup/")
print(f"archive created: {archive.name} and moved to: {backup.parent}")
Example 6
Access the current working directory with optional parameter frozen
to determine
different directories when script is bundled to an executable,
e.g. with pyinstaller
.
Path.cwd()
>>> poetry run examples/example6.py -b
Building frozen: K:/pathlibutil/examples/example6.exe
Build succeeded: 0
>>> poetry run examples/example6.py
we are not frozen
bundle dir is K:/pathlibutil/examples
sys.argv[0] is K:/pathlibutil/examples/example6.py
sys.executable is K:/pathlibutil/.venv/Scripts/python.exe
os.getcwd is K:/pathlibutil
Path.cwd(frozen=True) is K:/pathlibutil
Path.cwd(frozen=False) is K:/pathlibutil
Path.cwd(frozen=_MEIPASS) is K:/pathlibutil
>>> examples/example6.exe
we are ever so frozen
bundle dir is C:/Users/CHRIST~1.DOE/AppData/Local/Temp/_MEI106042
sys.argv[0] is examples/example6.exe
sys.executable is K:/pathlibutil/examples/example6.exe
os.getcwd is K:/pathlibutil
Path.cwd(frozen=True) is K:/pathlibutil/examples
Path.cwd(frozen=False) is K:/pathlibutil
Path.cwd(frozen=_MEIPASS) is C:/Users/CHRIST~1.DOE/AppData/Local/Temp/_MEI106042
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
File details
Details for the file pathlibutil-0.2.0.tar.gz
.
File metadata
- Download URL: pathlibutil-0.2.0.tar.gz
- Upload date:
- Size: 16.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.9 Linux/6.5.0-1024-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d227973f1e890b29f7452495b2b437cf4853efe09dfbcede1ee61b3124a90206 |
|
MD5 | 03647184d18b804b6b9e59f3b743c35c |
|
BLAKE2b-256 | 3e2ba0e41a6a99f03864898cef717ffb07ca449d94c055ee00b15b3d3434dc7e |
File details
Details for the file pathlibutil-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: pathlibutil-0.2.0-py3-none-any.whl
- Upload date:
- Size: 16.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.9 Linux/6.5.0-1024-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a4fe740981fca7a8a543ada68fb9ce13b64850c1b486eb2f6bc691f0d5f8827b |
|
MD5 | c5c407d3f39c025a483b077522a6495a |
|
BLAKE2b-256 | 83740e909eb7f0ef097fa799a49a54da645606e4ec9779ec4e2b72caa9ecc871 |