Write data to a given ``filepath`` with 'w' mode. Note: ``write_text`` will create a directory if the directory of ``filepath`` does not exist. Args: obj (str): Data to be written. filepath (str or Path): Path to write data.
(self, obj: str, filepath: Union[str, Path], encoding: str = "utf-8")
| 375 | f.write(obj) |
| 376 | |
| 377 | def write_text(self, obj: str, filepath: Union[str, Path], encoding: str = "utf-8") -> None: |
| 378 | """Write data to a given ``filepath`` with 'w' mode. |
| 379 | |
| 380 | Note: |
| 381 | ``write_text`` will create a directory if the directory of |
| 382 | ``filepath`` does not exist. |
| 383 | |
| 384 | Args: |
| 385 | obj (str): Data to be written. |
| 386 | filepath (str or Path): Path to write data. |
| 387 | encoding (str): The encoding format used to open the ``filepath``. |
| 388 | Default: 'utf-8'. |
| 389 | """ |
| 390 | dirname = os.path.dirname(filepath) |
| 391 | if dirname and not os.path.exists(dirname): |
| 392 | os.makedirs(dirname, exist_ok=True) |
| 393 | |
| 394 | with open(filepath, "w", encoding=encoding) as f: |
| 395 | f.write(obj) |
| 396 | |
| 397 | @contextlib.contextmanager |
| 398 | def as_local_path(self, filepath: Union[str, Path]) -> Generator[Union[str, Path], None, None]: |