Add class for existing files

This commit is contained in:
2026-04-09 19:51:39 +02:00
parent 74cb599ed6
commit a550ba6832
4 changed files with 40 additions and 2 deletions
+16 -1
View File
@@ -24,7 +24,7 @@ import types
class ExistingDirectory(pathlib.Path):
'''
pathlib.Path subclass that is a directory
pathlib.Path subclass that is a directory.
ExistingDirectory represents a filesystem path but it also checks
whether the path exists and is not a file but a directory. It
@@ -37,6 +37,21 @@ class ExistingDirectory(pathlib.Path):
raise NotADirectoryError(errno.ENOTDIR, 'not a directory', path)
super().__init__(*(path, *args), **kwargs)
class ExistingFile(pathlib.Path):
'''
pathlib.Path subclass that is a file.
ExistingFile represents a filesystem path but it also checks whether
the path exists and is not a directory but a file. It inherits from
pathlib.Path.
'''
def __init__(self, path, *args, **kwargs) -> None:
if not os.path.exists(path):
raise FileNotFoundError(errno.ENOENT, 'no such file or directory', path)
if not os.path.isfile(path):
raise IsADirectoryError(errno.EISDIR, 'is a directory', path)
super().__init__(*(path, *args), **kwargs)
def _quote(string: str, quotation_mark: str) -> str:
'''
Quotes a string.