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