pathier

 1import griddle
 2import noiftimer
 3import printbuddies
 4
 5from .pathier import Pathier, Pathish, Pathy
 6
 7__all__ = ["Pathier", "Pathy", "Pathish"]
 8
 9
10@noiftimer.time_it()
11def sizeup():
12    """Print the sub-directories and their sizes of the current working directory."""
13    sizes = {}
14    folders = [folder for folder in Pathier.cwd().iterdir() if folder.is_dir()]
15    print(f"Sizing up {len(folders)} directories...")
16    with printbuddies.ProgBar(len(folders)) as prog:
17        for folder in folders:
18            prog.display(f"Scanning '{folder.name}'")
19            sizes[folder.name] = folder.size
20    total_size = sum(sizes[folder] for folder in sizes)
21    sizes = [
22        (folder, Pathier.format_bytes(sizes[folder]))
23        for folder in sorted(list(sizes.keys()), key=lambda f: sizes[f], reverse=True)
24    ]
25    print(griddle.griddy(sizes, ["Dir", "Size"]))
26    print(f"Total size of '{Pathier.cwd()}': {Pathier.format_bytes(total_size)}")
class Pathier(pathlib.Path):
 16class Pathier(pathlib.Path):
 17    """Subclasses the standard library pathlib.Path class."""
 18
 19    def __new__(cls, *args, **kwargs):
 20        if cls is Pathier:
 21            cls = WindowsPath if os.name == "nt" else PosixPath
 22        self = cls._from_parts(args)
 23        if not self._flavour.is_supported:
 24            raise NotImplementedError(
 25                "cannot instantiate %r on your system" % (cls.__name__,)
 26            )
 27        if "convert_backslashes" in kwargs:
 28            self.convert_backslashes = kwargs["convert_backslashes"]
 29        else:
 30            self.convert_backslashes = True
 31        return self
 32
 33    @property
 34    def convert_backslashes(self) -> bool:
 35        """If True, when `self.__str__()`/`str(self)` is called, string representations will have double backslashes converted to a forward slash.
 36
 37        Only affects Windows paths."""
 38        try:
 39            return self._convert_backslashes
 40        except Exception as e:
 41            return True
 42
 43    @convert_backslashes.setter
 44    def convert_backslashes(self, should_convert: bool):
 45        self._convert_backslashes = should_convert
 46
 47    def __str__(self) -> str:
 48        path = super().__new__(pathlib.Path, self).__str__()
 49        if self.convert_backslashes:
 50            path = path.replace("\\", "/")
 51        return path
 52
 53    # ===============================================stats===============================================
 54    @property
 55    def dob(self) -> datetime.datetime | None:
 56        """Returns the creation date of this file or directory as a `dateime.datetime` object."""
 57        if self.exists():
 58            return datetime.datetime.fromtimestamp(self.stat().st_ctime)
 59        else:
 60            return None
 61
 62    @property
 63    def age(self) -> float | None:
 64        """Returns the age in seconds of this file or directory."""
 65        if self.exists():
 66            return (datetime.datetime.now() - self.dob).total_seconds()
 67        else:
 68            return None
 69
 70    @property
 71    def mod_date(self) -> datetime.datetime | None:
 72        """Returns the modification date of this file or directory as a `datetime.datetime` object."""
 73        if self.exists():
 74            return datetime.datetime.fromtimestamp(self.stat().st_mtime)
 75        else:
 76            return None
 77
 78    @property
 79    def mod_delta(self) -> float | None:
 80        """Returns how long ago in seconds this file or directory was modified."""
 81        if self.exists():
 82            return (datetime.datetime.now() - self.mod_date).total_seconds()
 83        else:
 84            return None
 85
 86    @property
 87    def last_read_time(self) -> datetime.datetime | None:
 88        """Returns the last time this object made a call to `self.read_text()`, `self.read_bytes()`, or `self.open(mode="r"|"rb")`.
 89        Returns `None` if the file hasn't been read from.
 90
 91        Note: This property is only relative to the lifetime of this `Pathier` instance, not the file itself.
 92        i.e. This property will reset if you create a new `Pathier` object pointing to the same file."""
 93        if self._last_read_time:
 94            return datetime.datetime.fromtimestamp(self._last_read_time)
 95        else:
 96            return self._last_read_time
 97
 98    @property
 99    def modified_since_last_read(self) -> bool:
100        """Returns `True` if this file hasn't been read from or has been modified since the last time this object
101        made a call to `self.read_text()`, `self.read_bytes()`, or `self.open(mode="r"|"rb")`.
102
103        Note: This property is only relative to the lifetime of this `Pathier` instance, not the file itself.
104        i.e. This property will reset if you create a new `Pathier` object pointing to the same file.
105
106        #### Caveat:
107        May not be accurate if the file was modified within a couple of seconds of checking this property.
108        (For instance, on my machine `self.mod_date` is consistently 1-1.5s in the future from when `self.write_text()` was called according to `time.time()`.)
109        """
110
111        return self.last_read_time is None or self.mod_date > self.last_read_time
112
113    @property
114    def size(self) -> int:
115        """Returns the size in bytes of this file or directory.
116
117        If this path doesn't exist, `0` will be returned."""
118        if not self.exists():
119            return 0
120        elif self.is_file():
121            return self.stat().st_size
122        elif self.is_dir():
123            return sum(file.stat().st_size for file in self.rglob("*.*"))
124        return 0
125
126    @property
127    def formatted_size(self) -> str:
128        """The size of this file or directory formatted with `self.format_bytes()`."""
129        return self.format_bytes(self.size)
130
131    @staticmethod
132    def format_bytes(size: int) -> str:
133        """Format `size` with common file size abbreviations and rounded to two decimal places.
134        >>> 1234 -> "1.23 kb" """
135        for unit in ["bytes", "kb", "mb", "gb", "tb", "pb"]:
136            if unit != "bytes":
137                size *= 0.001
138            if size < 1000 or unit == "pb":
139                return f"{round(size, 2)} {unit}"
140
141    def is_larger(self, path: Self) -> bool:
142        """Returns whether this file or folder is larger than the one pointed to by `path`."""
143        return self.size > path.size
144
145    def is_older(self, path: Self) -> bool:
146        """Returns whether this file or folder is older than the one pointed to by `path`."""
147        return self.dob < path.dob
148
149    def modified_more_recently(self, path: Self) -> bool:
150        """Returns whether this file or folder was modified more recently than the one pointed to by `path`."""
151        return self.mod_date > path.mod_date
152
153    # ===============================================navigation===============================================
154    def mkcwd(self):
155        """Make this path your current working directory."""
156        os.chdir(self)
157
158    @property
159    def in_PATH(self) -> bool:
160        """Return `True` if this path is in `sys.path`."""
161        return str(self) in sys.path
162
163    def add_to_PATH(self, index: int = 0):
164        """Insert this path into `sys.path` if it isn't already there.
165
166        #### :params:
167
168        `index`: The index of `sys.path` to insert this path at."""
169        path = str(self)
170        if not self.in_PATH:
171            sys.path.insert(index, path)
172
173    def append_to_PATH(self):
174        """Append this path to `sys.path` if it isn't already there."""
175        path = str(self)
176        if not self.in_PATH:
177            sys.path.append(path)
178
179    def remove_from_PATH(self):
180        """Remove this path from `sys.path` if it's in `sys.path`."""
181        if self.in_PATH:
182            sys.path.remove(str(self))
183
184    def moveup(self, name: str) -> Self:
185        """Return a new `Pathier` object that is a parent of this instance.
186
187        `name` is case-sensitive and raises an exception if it isn't in `self.parts`.
188        >>> p = Pathier("C:\some\directory\in\your\system")
189        >>> print(p.moveup("directory"))
190        >>> "C:\some\directory"
191        >>> print(p.moveup("yeet"))
192        >>> "Exception: yeet is not a parent of C:\some\directory\in\your\system" """
193        if name not in self.parts:
194            raise Exception(f"{name} is not a parent of {self}")
195        return Pathier(*(self.parts[: self.parts.index(name) + 1]))
196
197    def __sub__(self, levels: int) -> Self:
198        """Return a new `Pathier` object moved up `levels` number of parents from the current path.
199        >>> p = Pathier("C:\some\directory\in\your\system")
200        >>> new_p = p - 3
201        >>> print(new_p)
202        >>> "C:\some\directory" """
203        path = self
204        for _ in range(levels):
205            path = path.parent
206        return path
207
208    def move_under(self, name: str) -> Self:
209        """Return a new `Pathier` object such that the stem is one level below the given folder `name`.
210
211        `name` is case-sensitive and raises an exception if it isn't in `self.parts`.
212        >>> p = Pathier("a/b/c/d/e/f/g")
213        >>> print(p.move_under("c"))
214        >>> 'a/b/c/d'"""
215        if name not in self.parts:
216            raise Exception(f"{name} is not a parent of {self}")
217        return self - (len(self.parts) - self.parts.index(name) - 2)
218
219    def separate(self, name: str, keep_name: bool = False) -> Self:
220        """Return a new `Pathier` object that is the relative child path after `name`.
221
222        `name` is case-sensitive and raises an exception if it isn't in `self.parts`.
223
224        #### :params:
225
226        `keep_name`: If `True`, the returned path will start with `name`.
227        >>> p = Pathier("a/b/c/d/e/f/g")
228        >>> print(p.separate("c"))
229        >>> 'd/e/f/g'
230        >>> print(p.separate("c", True))
231        >>> 'c/d/e/f/g'"""
232        if name not in self.parts:
233            raise Exception(f"{name} is not a parent of {self}")
234        if keep_name:
235            return Pathier(*self.parts[self.parts.index(name) :])
236        return Pathier(*self.parts[self.parts.index(name) + 1 :])
237
238    # ============================================write and read============================================
239    def mkdir(self, mode: int = 511, parents: bool = True, exist_ok: bool = True):
240        """Create this directory.
241
242        Same as `Path().mkdir()` except `parents` and `exist_ok` default to `True` instead of `False`."""
243        super().mkdir(mode, parents, exist_ok)
244
245    def touch(self):
246        """Create file (and parents if necessary)."""
247        self.parent.mkdir()
248        super().touch()
249
250    def open(self, mode="r", buffering=-1, encoding=None, errors=None, newline=None):
251        """
252        Open the file pointed by this path and return a file object, as
253        the built-in open() function does.
254        """
255        stream = super().open(mode, buffering, encoding, errors, newline)
256        if "r" in mode:
257            self._last_read_time = time.time()
258        return stream
259
260    def write_text(
261        self,
262        data: Any,
263        encoding: Any | None = None,
264        errors: Any | None = None,
265        newline: Any | None = None,
266        parents: bool = True,
267    ):
268        """Write data to file.
269
270        If a `TypeError` is raised, the function  will attempt to cast `data` to a `str` and try the write again.
271
272        If a `FileNotFoundError` is raised and `parents = True`, `self.parent` will be created."""
273        write = functools.partial(
274            super().write_text,
275            encoding=encoding,
276            errors=errors,
277            newline=newline,
278        )
279        try:
280            write(data)
281        except TypeError:
282            data = str(data)
283            write(data)
284        except FileNotFoundError:
285            if parents:
286                self.parent.mkdir(parents=True)
287                write(data)
288            else:
289                raise
290        except Exception as e:
291            raise
292
293    def write_bytes(self, data: bytes, parents: bool = True):
294        """Write bytes to file.
295
296        #### :params:
297
298        `parents`: If `True` and the write operation fails with a `FileNotFoundError`,
299        make the parent directory and retry the write."""
300        try:
301            super().write_bytes(data)
302        except FileNotFoundError:
303            if parents:
304                self.parent.mkdir(parents=True)
305                super().write_bytes(data)
306            else:
307                raise
308        except Exception as e:
309            raise
310
311    def append(self, data: str, new_line: bool = True, encoding: Any | None = None):
312        """Append `data` to the file pointed to by this `Pathier` object.
313
314        #### :params:
315
316        `new_line`: If `True`, add `\\n` to `data`.
317
318        `encoding`: The file encoding to use."""
319        if new_line:
320            data += "\n"
321        with self.open("a", encoding=encoding) as file:
322            file.write(data)
323
324    def replace_strings(
325        self,
326        substitutions: list[tuple[str, str]],
327        count: int = -1,
328        encoding: Any | None = None,
329    ):
330        """For each pair in `substitutions`, replace the first string with the second string.
331
332        #### :params:
333
334        `count`: Only replace this many occurences of each pair.
335        By default (`-1`), all occurences are replaced.
336
337        `encoding`: The file encoding to use.
338
339        e.g.
340        >>> path = Pathier("somefile.txt")
341        >>>
342        >>> path.replace([("hello", "yeet"), ("goodbye", "yeehaw")])
343        equivalent to
344        >>> path.write_text(path.read_text().replace("hello", "yeet").replace("goodbye", "yeehaw"))"""
345        text = self.read_text(encoding)
346        for sub in substitutions:
347            text = text.replace(sub[0], sub[1], count)
348        self.write_text(text, encoding=encoding)
349
350    def join(self, data: list[str], encoding: Any | None = None, sep: str = "\n"):
351        """Write a list of strings, joined by `sep`, to the file pointed at by this instance.
352
353        Equivalent to `Pathier("somefile.txt").write_text(sep.join(data), encoding=encoding)`
354
355        #### :params:
356
357        `encoding`: The file encoding to use.
358
359        `sep`: The separator to use when joining `data`."""
360        self.write_text(sep.join(data), encoding=encoding)
361
362    def split(self, encoding: Any | None = None, keepends: bool = False) -> list[str]:
363        """Returns the content of the pointed at file as a list of strings, splitting at new line characters.
364
365        Equivalent to `Pathier("somefile.txt").read_text(encoding=encoding).splitlines()`
366
367        #### :params:
368
369        `encoding`: The file encoding to use.
370
371        `keepend`: If `True`, line breaks will be included in returned strings."""
372        return self.read_text(encoding=encoding).splitlines(keepends)
373
374    def json_loads(self, encoding: Any | None = None, errors: Any | None = None) -> Any:
375        """Load json file."""
376        return json.loads(self.read_text(encoding, errors))
377
378    def json_dumps(
379        self,
380        data: Any,
381        encoding: Any | None = None,
382        errors: Any | None = None,
383        newline: Any | None = None,
384        sort_keys: bool = False,
385        indent: Any | None = None,
386        default: Any | None = None,
387        parents: bool = True,
388    ) -> Any:
389        """Dump `data` to json file."""
390        self.write_text(
391            json.dumps(data, indent=indent, default=default, sort_keys=sort_keys),
392            encoding,
393            errors,
394            newline,
395            parents,
396        )
397
398    def toml_loads(self, encoding: Any | None = None, errors: Any | None = None) -> Any:
399        """Load toml file."""
400        return tomlkit.loads(self.read_text(encoding, errors)).unwrap()
401
402    def toml_dumps(
403        self,
404        data: Any,
405        encoding: Any | None = None,
406        errors: Any | None = None,
407        newline: Any | None = None,
408        sort_keys: bool = False,
409        parents: bool = True,
410    ):
411        """Dump `data` to toml file."""
412        self.write_text(
413            tomlkit.dumps(data, sort_keys), encoding, errors, newline, parents
414        )
415
416    def loads(self, encoding: Any | None = None, errors: Any | None = None) -> Any:
417        """Load a json or toml file based off this instance's suffix."""
418        match self.suffix:
419            case ".json":
420                return self.json_loads(encoding, errors)
421            case ".toml":
422                return self.toml_loads(encoding, errors)
423
424    def dumps(
425        self,
426        data: Any,
427        encoding: Any | None = None,
428        errors: Any | None = None,
429        newline: Any | None = None,
430        sort_keys: bool = False,
431        indent: Any | None = None,
432        default: Any | None = None,
433        parents: bool = True,
434    ):
435        """Dump `data` to a json or toml file based off this instance's suffix."""
436        match self.suffix:
437            case ".json":
438                self.json_dumps(
439                    data, encoding, errors, newline, sort_keys, indent, default, parents
440                )
441            case ".toml":
442                self.toml_dumps(data, encoding, errors, newline, sort_keys, parents)
443
444    def delete(self, missing_ok: bool = True):
445        """Delete the file or folder pointed to by this instance.
446
447        Uses `self.unlink()` if a file and uses `shutil.rmtree()` if a directory."""
448        if self.is_file():
449            self.unlink(missing_ok)
450        elif self.is_dir():
451            shutil.rmtree(self)
452
453    def copy(
454        self, new_path: Self | pathlib.Path | str, overwrite: bool = False
455    ) -> Self:
456        """Copy the path pointed to by this instance
457        to the instance pointed to by `new_path` using `shutil.copyfile`
458        or `shutil.copytree`.
459
460        Returns the new path.
461
462        #### :params:
463
464        `new_path`: The copy destination.
465
466        `overwrite`: If `True`, files already existing in `new_path` will be overwritten.
467        If `False`, only files that don't exist in `new_path` will be copied."""
468        new_path = Pathier(new_path)
469        if self.is_dir():
470            if overwrite or not new_path.exists():
471                shutil.copytree(self, new_path, dirs_exist_ok=True)
472            else:
473                files = self.rglob("*.*")
474                for file in files:
475                    dst = new_path.with_name(file.name)
476                    if not dst.exists():
477                        shutil.copyfile(file, dst)
478        elif self.is_file():
479            if overwrite or not new_path.exists():
480                shutil.copyfile(self, new_path)
481        return new_path
482
483    def backup(self, timestamp: bool = False) -> Self | None:
484        """Create a copy of this file or directory with `_backup` appended to the path stem.
485        If the path to be backed up doesn't exist, `None` is returned.
486        Otherwise a `Pathier` object for the backup is returned.
487
488        #### :params:
489
490        `timestamp`: Add a timestamp to the backup name to prevent overriding previous backups.
491
492        >>> path = Pathier("some_file.txt")
493        >>> path.backup()
494        >>> list(path.iterdir())
495        >>> ['some_file.txt', 'some_file_backup.txt']
496        >>> path.backup(True)
497        >>> list(path.iterdir())
498        >>> ['some_file.txt', 'some_file_backup.txt', 'some_file_backup_04-28-2023-06_25_52_PM.txt']"""
499        if not self.exists():
500            return None
501        backup_stem = f"{self.stem}_backup"
502        if timestamp:
503            backup_stem = f"{backup_stem}_{datetime.datetime.now().strftime('%m-%d-%Y-%I_%M_%S_%p')}"
504        backup_path = self.with_stem(backup_stem)
505        self.copy(backup_path, True)
506        return backup_path
507
508    def execute(self, command: str = "", args: str = "") -> int:
509        """Make a call to `os.system` using the path pointed to by this Pathier object.
510
511        #### :params:
512
513        `command`: Program/command to precede the path with.
514
515        `args`: Any arguments that should come after the path.
516
517        :returns: The integer output of `os.system`.
518
519        e.g.
520        >>> path = Pathier("mydirectory") / "myscript.py"
521        then
522        >>> path.execute("py", "--iterations 10")
523        equivalent to
524        >>> os.system(f"py {path} --iterations 10")"""
525        return os.system(f"{command} {self} {args}")

Subclasses the standard library pathlib.Path class.

Pathier()
convert_backslashes: bool

If True, when self.__str__()/str(self) is called, string representations will have double backslashes converted to a forward slash.

Only affects Windows paths.

dob: datetime.datetime | None

Returns the creation date of this file or directory as a dateime.datetime object.

age: float | None

Returns the age in seconds of this file or directory.

mod_date: datetime.datetime | None

Returns the modification date of this file or directory as a datetime.datetime object.

mod_delta: float | None

Returns how long ago in seconds this file or directory was modified.

last_read_time: datetime.datetime | None

Returns the last time this object made a call to self.read_text(), self.read_bytes(), or self.open(mode="r"|"rb"). Returns None if the file hasn't been read from.

Note: This property is only relative to the lifetime of this Pathier instance, not the file itself. i.e. This property will reset if you create a new Pathier object pointing to the same file.

modified_since_last_read: bool

Returns True if this file hasn't been read from or has been modified since the last time this object made a call to self.read_text(), self.read_bytes(), or self.open(mode="r"|"rb").

Note: This property is only relative to the lifetime of this Pathier instance, not the file itself. i.e. This property will reset if you create a new Pathier object pointing to the same file.

Caveat:

May not be accurate if the file was modified within a couple of seconds of checking this property. (For instance, on my machine self.mod_date is consistently 1-1.5s in the future from when self.write_text() was called according to time.time().)

size: int

Returns the size in bytes of this file or directory.

If this path doesn't exist, 0 will be returned.

formatted_size: str

The size of this file or directory formatted with self.format_bytes().

@staticmethod
def format_bytes(size: int) -> str:
131    @staticmethod
132    def format_bytes(size: int) -> str:
133        """Format `size` with common file size abbreviations and rounded to two decimal places.
134        >>> 1234 -> "1.23 kb" """
135        for unit in ["bytes", "kb", "mb", "gb", "tb", "pb"]:
136            if unit != "bytes":
137                size *= 0.001
138            if size < 1000 or unit == "pb":
139                return f"{round(size, 2)} {unit}"

Format size with common file size abbreviations and rounded to two decimal places.

>>> 1234 -> "1.23 kb"
def is_larger(self, path: Self) -> bool:
141    def is_larger(self, path: Self) -> bool:
142        """Returns whether this file or folder is larger than the one pointed to by `path`."""
143        return self.size > path.size

Returns whether this file or folder is larger than the one pointed to by path.

def is_older(self, path: Self) -> bool:
145    def is_older(self, path: Self) -> bool:
146        """Returns whether this file or folder is older than the one pointed to by `path`."""
147        return self.dob < path.dob

Returns whether this file or folder is older than the one pointed to by path.

def modified_more_recently(self, path: Self) -> bool:
149    def modified_more_recently(self, path: Self) -> bool:
150        """Returns whether this file or folder was modified more recently than the one pointed to by `path`."""
151        return self.mod_date > path.mod_date

Returns whether this file or folder was modified more recently than the one pointed to by path.

def mkcwd(self):
154    def mkcwd(self):
155        """Make this path your current working directory."""
156        os.chdir(self)

Make this path your current working directory.

in_PATH: bool

Return True if this path is in sys.path.

def add_to_PATH(self, index: int = 0):
163    def add_to_PATH(self, index: int = 0):
164        """Insert this path into `sys.path` if it isn't already there.
165
166        #### :params:
167
168        `index`: The index of `sys.path` to insert this path at."""
169        path = str(self)
170        if not self.in_PATH:
171            sys.path.insert(index, path)

Insert this path into sys.path if it isn't already there.

:params:

index: The index of sys.path to insert this path at.

def append_to_PATH(self):
173    def append_to_PATH(self):
174        """Append this path to `sys.path` if it isn't already there."""
175        path = str(self)
176        if not self.in_PATH:
177            sys.path.append(path)

Append this path to sys.path if it isn't already there.

def remove_from_PATH(self):
179    def remove_from_PATH(self):
180        """Remove this path from `sys.path` if it's in `sys.path`."""
181        if self.in_PATH:
182            sys.path.remove(str(self))

Remove this path from sys.path if it's in sys.path.

def moveup(self, name: str) -> Self:
184    def moveup(self, name: str) -> Self:
185        """Return a new `Pathier` object that is a parent of this instance.
186
187        `name` is case-sensitive and raises an exception if it isn't in `self.parts`.
188        >>> p = Pathier("C:\some\directory\in\your\system")
189        >>> print(p.moveup("directory"))
190        >>> "C:\some\directory"
191        >>> print(p.moveup("yeet"))
192        >>> "Exception: yeet is not a parent of C:\some\directory\in\your\system" """
193        if name not in self.parts:
194            raise Exception(f"{name} is not a parent of {self}")
195        return Pathier(*(self.parts[: self.parts.index(name) + 1]))

Return a new Pathier object that is a parent of this instance.

name is case-sensitive and raises an exception if it isn't in self.parts.

>>> p = Pathier("C:\some\directory\in\your\system")
>>> print(p.moveup("directory"))
>>> "C:\some\directory"
>>> print(p.moveup("yeet"))
>>> "Exception: yeet is not a parent of C:\some\directory\in\your\system"
def move_under(self, name: str) -> Self:
208    def move_under(self, name: str) -> Self:
209        """Return a new `Pathier` object such that the stem is one level below the given folder `name`.
210
211        `name` is case-sensitive and raises an exception if it isn't in `self.parts`.
212        >>> p = Pathier("a/b/c/d/e/f/g")
213        >>> print(p.move_under("c"))
214        >>> 'a/b/c/d'"""
215        if name not in self.parts:
216            raise Exception(f"{name} is not a parent of {self}")
217        return self - (len(self.parts) - self.parts.index(name) - 2)

Return a new Pathier object such that the stem is one level below the given folder name.

name is case-sensitive and raises an exception if it isn't in self.parts.

>>> p = Pathier("a/b/c/d/e/f/g")
>>> print(p.move_under("c"))
>>> 'a/b/c/d'
def separate(self, name: str, keep_name: bool = False) -> Self:
219    def separate(self, name: str, keep_name: bool = False) -> Self:
220        """Return a new `Pathier` object that is the relative child path after `name`.
221
222        `name` is case-sensitive and raises an exception if it isn't in `self.parts`.
223
224        #### :params:
225
226        `keep_name`: If `True`, the returned path will start with `name`.
227        >>> p = Pathier("a/b/c/d/e/f/g")
228        >>> print(p.separate("c"))
229        >>> 'd/e/f/g'
230        >>> print(p.separate("c", True))
231        >>> 'c/d/e/f/g'"""
232        if name not in self.parts:
233            raise Exception(f"{name} is not a parent of {self}")
234        if keep_name:
235            return Pathier(*self.parts[self.parts.index(name) :])
236        return Pathier(*self.parts[self.parts.index(name) + 1 :])

Return a new Pathier object that is the relative child path after name.

name is case-sensitive and raises an exception if it isn't in self.parts.

:params:

keep_name: If True, the returned path will start with name.

>>> p = Pathier("a/b/c/d/e/f/g")
>>> print(p.separate("c"))
>>> 'd/e/f/g'
>>> print(p.separate("c", True))
>>> 'c/d/e/f/g'
def mkdir(self, mode: int = 511, parents: bool = True, exist_ok: bool = True):
239    def mkdir(self, mode: int = 511, parents: bool = True, exist_ok: bool = True):
240        """Create this directory.
241
242        Same as `Path().mkdir()` except `parents` and `exist_ok` default to `True` instead of `False`."""
243        super().mkdir(mode, parents, exist_ok)

Create this directory.

Same as Path().mkdir() except parents and exist_ok default to True instead of False.

def touch(self):
245    def touch(self):
246        """Create file (and parents if necessary)."""
247        self.parent.mkdir()
248        super().touch()

Create file (and parents if necessary).

def open( self, mode='r', buffering=-1, encoding=None, errors=None, newline=None):
250    def open(self, mode="r", buffering=-1, encoding=None, errors=None, newline=None):
251        """
252        Open the file pointed by this path and return a file object, as
253        the built-in open() function does.
254        """
255        stream = super().open(mode, buffering, encoding, errors, newline)
256        if "r" in mode:
257            self._last_read_time = time.time()
258        return stream

Open the file pointed by this path and return a file object, as the built-in open() function does.

def write_text( self, data: Any, encoding: typing.Any | None = None, errors: typing.Any | None = None, newline: typing.Any | None = None, parents: bool = True):
260    def write_text(
261        self,
262        data: Any,
263        encoding: Any | None = None,
264        errors: Any | None = None,
265        newline: Any | None = None,
266        parents: bool = True,
267    ):
268        """Write data to file.
269
270        If a `TypeError` is raised, the function  will attempt to cast `data` to a `str` and try the write again.
271
272        If a `FileNotFoundError` is raised and `parents = True`, `self.parent` will be created."""
273        write = functools.partial(
274            super().write_text,
275            encoding=encoding,
276            errors=errors,
277            newline=newline,
278        )
279        try:
280            write(data)
281        except TypeError:
282            data = str(data)
283            write(data)
284        except FileNotFoundError:
285            if parents:
286                self.parent.mkdir(parents=True)
287                write(data)
288            else:
289                raise
290        except Exception as e:
291            raise

Write data to file.

If a TypeError is raised, the function will attempt to cast data to a str and try the write again.

If a FileNotFoundError is raised and parents = True, self.parent will be created.

def write_bytes(self, data: bytes, parents: bool = True):
293    def write_bytes(self, data: bytes, parents: bool = True):
294        """Write bytes to file.
295
296        #### :params:
297
298        `parents`: If `True` and the write operation fails with a `FileNotFoundError`,
299        make the parent directory and retry the write."""
300        try:
301            super().write_bytes(data)
302        except FileNotFoundError:
303            if parents:
304                self.parent.mkdir(parents=True)
305                super().write_bytes(data)
306            else:
307                raise
308        except Exception as e:
309            raise

Write bytes to file.

:params:

parents: If True and the write operation fails with a FileNotFoundError, make the parent directory and retry the write.

def append( self, data: str, new_line: bool = True, encoding: typing.Any | None = None):
311    def append(self, data: str, new_line: bool = True, encoding: Any | None = None):
312        """Append `data` to the file pointed to by this `Pathier` object.
313
314        #### :params:
315
316        `new_line`: If `True`, add `\\n` to `data`.
317
318        `encoding`: The file encoding to use."""
319        if new_line:
320            data += "\n"
321        with self.open("a", encoding=encoding) as file:
322            file.write(data)

Append data to the file pointed to by this Pathier object.

:params:

new_line: If True, add \n to data.

encoding: The file encoding to use.

def replace_strings( self, substitutions: list[tuple[str, str]], count: int = -1, encoding: typing.Any | None = None):
324    def replace_strings(
325        self,
326        substitutions: list[tuple[str, str]],
327        count: int = -1,
328        encoding: Any | None = None,
329    ):
330        """For each pair in `substitutions`, replace the first string with the second string.
331
332        #### :params:
333
334        `count`: Only replace this many occurences of each pair.
335        By default (`-1`), all occurences are replaced.
336
337        `encoding`: The file encoding to use.
338
339        e.g.
340        >>> path = Pathier("somefile.txt")
341        >>>
342        >>> path.replace([("hello", "yeet"), ("goodbye", "yeehaw")])
343        equivalent to
344        >>> path.write_text(path.read_text().replace("hello", "yeet").replace("goodbye", "yeehaw"))"""
345        text = self.read_text(encoding)
346        for sub in substitutions:
347            text = text.replace(sub[0], sub[1], count)
348        self.write_text(text, encoding=encoding)

For each pair in substitutions, replace the first string with the second string.

:params:

count: Only replace this many occurences of each pair. By default (-1), all occurences are replaced.

encoding: The file encoding to use.

e.g.

>>> path = Pathier("somefile.txt")
>>>
>>> path.replace([("hello", "yeet"), ("goodbye", "yeehaw")])
equivalent to
>>> path.write_text(path.read_text().replace("hello", "yeet").replace("goodbye", "yeehaw"))
def join( self, data: list[str], encoding: typing.Any | None = None, sep: str = '\n'):
350    def join(self, data: list[str], encoding: Any | None = None, sep: str = "\n"):
351        """Write a list of strings, joined by `sep`, to the file pointed at by this instance.
352
353        Equivalent to `Pathier("somefile.txt").write_text(sep.join(data), encoding=encoding)`
354
355        #### :params:
356
357        `encoding`: The file encoding to use.
358
359        `sep`: The separator to use when joining `data`."""
360        self.write_text(sep.join(data), encoding=encoding)

Write a list of strings, joined by sep, to the file pointed at by this instance.

Equivalent to Pathier("somefile.txt").write_text(sep.join(data), encoding=encoding)

:params:

encoding: The file encoding to use.

sep: The separator to use when joining data.

def split( self, encoding: typing.Any | None = None, keepends: bool = False) -> list[str]:
362    def split(self, encoding: Any | None = None, keepends: bool = False) -> list[str]:
363        """Returns the content of the pointed at file as a list of strings, splitting at new line characters.
364
365        Equivalent to `Pathier("somefile.txt").read_text(encoding=encoding).splitlines()`
366
367        #### :params:
368
369        `encoding`: The file encoding to use.
370
371        `keepend`: If `True`, line breaks will be included in returned strings."""
372        return self.read_text(encoding=encoding).splitlines(keepends)

Returns the content of the pointed at file as a list of strings, splitting at new line characters.

Equivalent to Pathier("somefile.txt").read_text(encoding=encoding).splitlines()

:params:

encoding: The file encoding to use.

keepend: If True, line breaks will be included in returned strings.

def json_loads( self, encoding: typing.Any | None = None, errors: typing.Any | None = None) -> Any:
374    def json_loads(self, encoding: Any | None = None, errors: Any | None = None) -> Any:
375        """Load json file."""
376        return json.loads(self.read_text(encoding, errors))

Load json file.

def json_dumps( self, data: Any, encoding: typing.Any | None = None, errors: typing.Any | None = None, newline: typing.Any | None = None, sort_keys: bool = False, indent: typing.Any | None = None, default: typing.Any | None = None, parents: bool = True) -> Any:
378    def json_dumps(
379        self,
380        data: Any,
381        encoding: Any | None = None,
382        errors: Any | None = None,
383        newline: Any | None = None,
384        sort_keys: bool = False,
385        indent: Any | None = None,
386        default: Any | None = None,
387        parents: bool = True,
388    ) -> Any:
389        """Dump `data` to json file."""
390        self.write_text(
391            json.dumps(data, indent=indent, default=default, sort_keys=sort_keys),
392            encoding,
393            errors,
394            newline,
395            parents,
396        )

Dump data to json file.

def toml_loads( self, encoding: typing.Any | None = None, errors: typing.Any | None = None) -> Any:
398    def toml_loads(self, encoding: Any | None = None, errors: Any | None = None) -> Any:
399        """Load toml file."""
400        return tomlkit.loads(self.read_text(encoding, errors)).unwrap()

Load toml file.

def toml_dumps( self, data: Any, encoding: typing.Any | None = None, errors: typing.Any | None = None, newline: typing.Any | None = None, sort_keys: bool = False, parents: bool = True):
402    def toml_dumps(
403        self,
404        data: Any,
405        encoding: Any | None = None,
406        errors: Any | None = None,
407        newline: Any | None = None,
408        sort_keys: bool = False,
409        parents: bool = True,
410    ):
411        """Dump `data` to toml file."""
412        self.write_text(
413            tomlkit.dumps(data, sort_keys), encoding, errors, newline, parents
414        )

Dump data to toml file.

def loads( self, encoding: typing.Any | None = None, errors: typing.Any | None = None) -> Any:
416    def loads(self, encoding: Any | None = None, errors: Any | None = None) -> Any:
417        """Load a json or toml file based off this instance's suffix."""
418        match self.suffix:
419            case ".json":
420                return self.json_loads(encoding, errors)
421            case ".toml":
422                return self.toml_loads(encoding, errors)

Load a json or toml file based off this instance's suffix.

def dumps( self, data: Any, encoding: typing.Any | None = None, errors: typing.Any | None = None, newline: typing.Any | None = None, sort_keys: bool = False, indent: typing.Any | None = None, default: typing.Any | None = None, parents: bool = True):
424    def dumps(
425        self,
426        data: Any,
427        encoding: Any | None = None,
428        errors: Any | None = None,
429        newline: Any | None = None,
430        sort_keys: bool = False,
431        indent: Any | None = None,
432        default: Any | None = None,
433        parents: bool = True,
434    ):
435        """Dump `data` to a json or toml file based off this instance's suffix."""
436        match self.suffix:
437            case ".json":
438                self.json_dumps(
439                    data, encoding, errors, newline, sort_keys, indent, default, parents
440                )
441            case ".toml":
442                self.toml_dumps(data, encoding, errors, newline, sort_keys, parents)

Dump data to a json or toml file based off this instance's suffix.

def delete(self, missing_ok: bool = True):
444    def delete(self, missing_ok: bool = True):
445        """Delete the file or folder pointed to by this instance.
446
447        Uses `self.unlink()` if a file and uses `shutil.rmtree()` if a directory."""
448        if self.is_file():
449            self.unlink(missing_ok)
450        elif self.is_dir():
451            shutil.rmtree(self)

Delete the file or folder pointed to by this instance.

Uses self.unlink() if a file and uses shutil.rmtree() if a directory.

def copy( self, new_path: Union[Self, pathlib.Path, str], overwrite: bool = False) -> Self:
453    def copy(
454        self, new_path: Self | pathlib.Path | str, overwrite: bool = False
455    ) -> Self:
456        """Copy the path pointed to by this instance
457        to the instance pointed to by `new_path` using `shutil.copyfile`
458        or `shutil.copytree`.
459
460        Returns the new path.
461
462        #### :params:
463
464        `new_path`: The copy destination.
465
466        `overwrite`: If `True`, files already existing in `new_path` will be overwritten.
467        If `False`, only files that don't exist in `new_path` will be copied."""
468        new_path = Pathier(new_path)
469        if self.is_dir():
470            if overwrite or not new_path.exists():
471                shutil.copytree(self, new_path, dirs_exist_ok=True)
472            else:
473                files = self.rglob("*.*")
474                for file in files:
475                    dst = new_path.with_name(file.name)
476                    if not dst.exists():
477                        shutil.copyfile(file, dst)
478        elif self.is_file():
479            if overwrite or not new_path.exists():
480                shutil.copyfile(self, new_path)
481        return new_path

Copy the path pointed to by this instance to the instance pointed to by new_path using shutil.copyfile or shutil.copytree.

Returns the new path.

:params:

new_path: The copy destination.

overwrite: If True, files already existing in new_path will be overwritten. If False, only files that don't exist in new_path will be copied.

def backup(self, timestamp: bool = False) -> Optional[Self]:
483    def backup(self, timestamp: bool = False) -> Self | None:
484        """Create a copy of this file or directory with `_backup` appended to the path stem.
485        If the path to be backed up doesn't exist, `None` is returned.
486        Otherwise a `Pathier` object for the backup is returned.
487
488        #### :params:
489
490        `timestamp`: Add a timestamp to the backup name to prevent overriding previous backups.
491
492        >>> path = Pathier("some_file.txt")
493        >>> path.backup()
494        >>> list(path.iterdir())
495        >>> ['some_file.txt', 'some_file_backup.txt']
496        >>> path.backup(True)
497        >>> list(path.iterdir())
498        >>> ['some_file.txt', 'some_file_backup.txt', 'some_file_backup_04-28-2023-06_25_52_PM.txt']"""
499        if not self.exists():
500            return None
501        backup_stem = f"{self.stem}_backup"
502        if timestamp:
503            backup_stem = f"{backup_stem}_{datetime.datetime.now().strftime('%m-%d-%Y-%I_%M_%S_%p')}"
504        backup_path = self.with_stem(backup_stem)
505        self.copy(backup_path, True)
506        return backup_path

Create a copy of this file or directory with _backup appended to the path stem. If the path to be backed up doesn't exist, None is returned. Otherwise a Pathier object for the backup is returned.

:params:

timestamp: Add a timestamp to the backup name to prevent overriding previous backups.

>>> path = Pathier("some_file.txt")
>>> path.backup()
>>> list(path.iterdir())
>>> ['some_file.txt', 'some_file_backup.txt']
>>> path.backup(True)
>>> list(path.iterdir())
>>> ['some_file.txt', 'some_file_backup.txt', 'some_file_backup_04-28-2023-06_25_52_PM.txt']
def execute(self, command: str = '', args: str = '') -> int:
508    def execute(self, command: str = "", args: str = "") -> int:
509        """Make a call to `os.system` using the path pointed to by this Pathier object.
510
511        #### :params:
512
513        `command`: Program/command to precede the path with.
514
515        `args`: Any arguments that should come after the path.
516
517        :returns: The integer output of `os.system`.
518
519        e.g.
520        >>> path = Pathier("mydirectory") / "myscript.py"
521        then
522        >>> path.execute("py", "--iterations 10")
523        equivalent to
524        >>> os.system(f"py {path} --iterations 10")"""
525        return os.system(f"{command} {self} {args}")

Make a call to os.system using the path pointed to by this Pathier object.

:params:

command: Program/command to precede the path with.

args: Any arguments that should come after the path.

:returns: The integer output of os.system.

e.g.

>>> path = Pathier("mydirectory") / "myscript.py"
then
>>> path.execute("py", "--iterations 10")
equivalent to
>>> os.system(f"py {path} --iterations 10")
Inherited Members
pathlib.Path
cwd
home
samefile
iterdir
glob
rglob
absolute
resolve
stat
owner
group
read_bytes
read_text
chmod
lchmod
rmdir
lstat
rename
replace
exists
is_dir
is_file
is_mount
is_block_device
is_char_device
is_fifo
is_socket
expanduser
pathlib.PurePath
as_posix
as_uri
drive
root
anchor
name
suffix
suffixes
stem
with_name
with_stem
with_suffix
relative_to
is_relative_to
parts
joinpath
parent
parents
is_absolute
is_reserved
match
Pathy = pathier.Pathier | pathlib.Path
Pathish = pathier.Pathier | pathlib.Path | str