Add convenience methods to Python Pathlib via monkey patching.
Pathlibext is available in PyPI. To Install it use pip:
pip install pathlibextJust import pathlibext once and the Path class will be extended with a series of convenience methods:
from pathlib import Path
import pathlibext # pylint: disable=unused-import
if __name__ == '__main__':
tmp = Path.tmpdir() # from pathlibext
print(f"Create temp directory {tmp}")
for f in [
tmp / "dir1" / "a.txt",
tmp / "dir1" / "b.txt",
tmp / "dir2" / "a.txt",
tmp / "dir2" / "b.txt",
]:
f.parent.mkdir(exist_ok=True)
f.touch()
print(f"Create file {f}")
for d in tmp.find(type_="d", name="dir2"): # from pathlibext
d.rmtree() # from pathlibext
print(f"Remove non-empty directory {d}")
print("Existing directories after rmtree()")
for d in tmp.find(type_="d"): # from pathlibext
print(f"{d}")
tmp.rmtree() # from pathlibext
print(f"Remove non-empty temp dir {tmp}")Extends the Path object with find method which resembles POSIX find.
The find method returns a generator.
from pathlib import Path
import pathlibext # pylint: disable=unused-import
print(list(Path(".").resolve().find("f", name="*.py", maxdepth=4)))Test whether the Path object matches the pattern string, returning a boolean.
Match is not case sensitive on Windows.
Same as fnmatch.match.
import os
from pathlib import Path
import pathlibext # pylint: disable=unused-import
print(Path("/foo/BAR.txt").match("*BAR*")) # True
if os.name == "nt":
# On windows match() is case insensitive
print(Path("/foo/BAR.txt").match("*bar*")) # True
else:
# On POSIX match() is case sensitive
print(Path("/foo/BAR.txt").match("*bar*")) # FalseTest whether the Path object matches pattern, returning boolean.
The comparison is case-sensitive.
Same as fnmatch.macthcase.
from pathlib import Path
import pathlibext # pylint: disable=unused-import
print(Path("/foo/BAR.txt").matchcase("*BAR*")) # True
print(Path("/foo/BAR.txt").matchcase("*bar*")) # FalseDelete non empty directory tree starting from the root represented as Path object.
from pathlib import Path
import pathlibext # pylint: disable=unused-import
tmp = Path.tmpdir()
f = tmp / "foo" / "test.txt"
d = f.parent
d.mkdir()
f.touch() # touchpath !
tmp.rmtree()
print(f.exists()) # False
print(d.exists()) # FalseCopies the file represented as Path object to destination file or directory provided as Path object.
Same as shutil.copy.
To preserve metadata use copy_preserve_metadata() which is imlpemented with shutil.copy2.
from pathlib import Path
import pathlibext # pylint: disable=unused-import
src = Path.tmpdir()
dst = Path.tmpdir()
f = src / "test.txt"
f.touch()
copied = f.copy(dst)
print(f.exists()) # True
print(copied.exists()) # True
src.rmtree() # cleanup
dst.rmtree() # cleanupMove the file represented by the Path object to another location again represented as Path object. It is implemented with shutil.move.
from pathlib import Path
import pathlibext # pylint: disable=unused-import
src = Path.tmpdir()
dst = Path.tmpdir()
f = src / "test.txt"
f.touch()
moved = f.move(dst)
print(f.exists()) # False
print(moved.exists()) # True
src.rmtree() # cleanup
dst.rmtree() # cleanupReturn the file size in bytes or in the specified unit of measure. Short hand for Path.stat().st_size.
from pathlib import Path
import pathlibext # pylint: disable=unused-import
f = Path.tmpdir() / "test.txt"
f.write_text("abc")
print(f.size()) # 3
print(f.size("MB")) # 2.86102294921875e-06access_time() and modification_time() are short hand methods for Path().stat().st_atime and Path().stat().st_mtime.
creation_time() is available only on Windows and returns the file creation, same as Path().stat().st_ctime.
metadatachange_time() is available only on POSIX and returns the file metadata change time, same as Path().stat().st_ctime.
All methods and are wrapper around Path().stat().
CAUTION! All methods return time as datetime objects and are local time.
from pathlib import Path
import pathlibext # pylint: disable=unused-import
f = Path.tmpfile()
f.write_text("abc")
print(f.access_time())
f.unlink()Return OS temporary directory as Path.
from pathlib import Path
import pathlibext # pylint: disable=unused-import
print(Path.systmpdir()) # Path('/tmp')Return a temporary directory as Path. This is a wrapper of tempfile.mkdtemp.
from pathlib import Path
import pathlibext # pylint: disable=unused-import
print(Path.tmpdir())Creates a temporary directory as Path object inside an existing directory represented as Path.
from pathlib import Path
import pathlibext # pylint: disable=unused-import
print(Path("/tmp").tmpdir_in())Convinient method to get the file path as Path object of the currently executed file.
See examples/current_file.py on how to use it.
Convinient method to get the directory as Path object of the currently executed file.
See examples/current_file.py on how to use it.