Package: pathlibext
Pathlib ExtensionsAdd convenience methods to PythonPathlibviamonkey patching.InstallationPathlibext is available in PyPI. To Install it usepip:pipinstallpathlibextUsageJust import pathlibext once and the Path class will be extended with a series of convenience methods:frompathlibimportPathimportpathlibext# pylint: disable=unused-importif__name__=='__main__':tmp=Path.tmpdir()# from pathlibextprint(f"Create temp directory{tmp}")forfin[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}")fordintmp.find(type_="d",name="dir2"):# from pathlibextd.rmtree()# from pathlibextprint(f"Remove non-empty directory{d}")print("Existing directories after rmtree()")fordintmp.find(type_="d"):# from pathlibextprint(f"{d}")tmp.rmtree()# from pathlibextprint(f"Remove non-empty temp dir{tmp}")List of pathlibext methodsfindExtends thePathobject withfindmethod which resembles POSIX find.
Thefindmethod returns agenerator.frompathlibimportPathimportpathlibext# pylint: disable=unused-importprint(list(Path(".").resolve().find("f",name="*.py",maxdepth=4)))matchTest whether thePathobject matches the pattern string, returning a boolean.
Match is not case sensitive on Windows.Same asfnmatch.match.importosfrompathlibimportPathimportpathlibext# pylint: disable=unused-importprint(Path("/foo/BAR.txt").match("*BAR*"))# Trueifos.name=="nt":# On windows match() is case insensitiveprint(Path("/foo/BAR.txt").match("*bar*"))# Trueelse:# On POSIX match() is case sensitiveprint(Path("/foo/BAR.txt").match("*bar*"))# FalsematchcaseTest whether thePathobject matches pattern, returning boolean.
The comparison is case-sensitive.
Same asfnmatch.macthcase.frompathlibimportPathimportpathlibext# pylint: disable=unused-importprint(Path("/foo/BAR.txt").matchcase("*BAR*"))# Trueprint(Path("/foo/BAR.txt").matchcase("*bar*"))# FalsermtreeDelete non empty directory tree starting from the root represented asPathobject.frompathlibimportPathimportpathlibext# pylint: disable=unused-importtmp=Path.tmpdir()f=tmp/"foo"/"test.txt"d=f.parentd.mkdir()f.touch()# touchpath !tmp.rmtree()print(f.exists())# Falseprint(d.exists())# Falsecopy and copy_preserve_metadataCopies the file represented asPathobject to destination file or directory provided asPathobject.
Same asshutil.copy.
To preserve metadata use copy_preserve_metadata() which is imlpemented withshutil.copy2.frompathlibimportPathimportpathlibext# pylint: disable=unused-importsrc=Path.tmpdir()dst=Path.tmpdir()f=src/"test.txt"f.touch()copied=f.copy(dst)print(f.exists())# Trueprint(copied.exists())# Truesrc.rmtree()# cleanupdst.rmtree()# cleanupmoveMove the file represented by thePathobject to another location again represented asPathobject. It is implemented withshutil.move.frompathlibimportPathimportpathlibext# pylint: disable=unused-importsrc=Path.tmpdir()dst=Path.tmpdir()f=src/"test.txt"f.touch()moved=f.move(dst)print(f.exists())# Falseprint(moved.exists())# Truesrc.rmtree()# cleanupdst.rmtree()# cleanupsizeReturn the file size in bytes or in the specified unit of measure. Short hand forPath.stat().st_size.frompathlibimportPathimportpathlibext# pylint: disable=unused-importf=Path.tmpdir()/"test.txt"f.write_text("abc")print(f.size())# 3print(f.size("MB"))# 2.86102294921875e-06access_time, modification_time, creation_time and metadatachange_timeaccess_time()andmodification_time()are short hand methods forPath().stat().st_atimeandPath().stat().st_mtime.creation_time()is available only on Windows and returns the file creation, same asPath().stat().st_ctime.metadatachange_time()is available only on POSIX and returns the file metadata change time, same asPath().stat().st_ctime.All methods and are wrapper aroundPath().stat().CAUTION! All methods return time asdatetime objectsand arelocal time.frompathlibimportPathimportpathlibext# pylint: disable=unused-importf=Path.tmpfile()f.write_text("abc")print(f.access_time())f.unlink()systmpdirReturn OS temporary directory asPath.frompathlibimportPathimportpathlibext# pylint: disable=unused-importprint(Path.systmpdir())# Path('/tmp')tmpdirReturn a temporary directory asPath. This is a wrapper oftempfile.mkdtemp.frompathlibimportPathimportpathlibext# pylint: disable=unused-importprint(Path.tmpdir())tmpdir_inCreates a temporary directory asPathobject inside an existing directory represented asPath.frompathlibimportPathimportpathlibext# pylint: disable=unused-importprint(Path("/tmp").tmpdir_in())current_file_pathConvinient method to get the file path asPathobject of the currently executed file.
Seeexamples/current_file.pyon how to use it.current_file_dirConvinient method to get the directory asPathobject of the currently executed file.
Seeexamples/current_file.pyon how to use it.