@@ -609,7 +609,7 @@ def open(self, mode="r", encoding=None, *args, **kwargs):
609609 ProperPath open first resolves the whole path and then simply returns pathlib.Path.open.
610610 This method is mainly overloaded to log exceptions.
611611 """
612- file = super (). resolve ()
612+ file = self . _expanded
613613 try :
614614 return Path (file ).open (
615615 mode = mode ,
@@ -632,5 +632,65 @@ def open(self, mode="r", encoding=None, *args, **kwargs):
632632 self .PathException = os_exception
633633 raise e
634634
635+ # noinspection PyIncorrectDocstring
636+ def get_text (
637+ self ,
638+ encoding : Optional [str ] = None ,
639+ errors : Optional [str ] = None ,
640+ newline : Optional [str ] = None ,
641+ default : Optional [str ] = None ,
642+ ) -> Optional [str ]:
643+ """
644+ `get_text` method is basically [`read_text`](https://docs.python.org/3.13/library/pathlib.html#pathlib.Path.read_text)
645+ with support for extra `defaults` parameter. `get_text` passes optional arguments `encoding`, `errors`, `newline` to
646+ `read_text`.
647+
648+ Args:
649+ encoding: The same meaning as the `encoding` argument for method
650+ [`open`](https://docs.python.org/3.13/library/functions.html#open).
651+ errors: The same meaning as the `encoding` argument for method
652+ [`open`](https://docs.python.org/3.13/library/functions.html#open).
653+ newline: The same meaning as the `encoding` argument for method
654+ [`open`](https://docs.python.org/3.13/library/functions.html#open).
655+ `newline` is only supported in Python 3.13 and above.
656+ default:
657+ If the file does not exist, then `default` is returned. By default, `default` is `None`.
658+
659+ Example:
660+ ```python
661+ import json
662+ from properpath import P
663+
664+ cache = json.loads(P("~/.cache/app/cache.json").get_text(encoding="utf-8", default='{}'))
665+ ```
666+
667+ Returns:
668+ The decoded contents of the pointed-to file as a string or default (when file does not exist).
669+ """
670+ try :
671+ if sys .version_info .minor >= 13 :
672+ return super ().read_text (encoding , errors , newline )
673+ return super ().read_text (encoding , errors )
674+ except FileNotFoundError :
675+ return default
676+
677+ # noinspection PyIncorrectDocstring
678+ def get_bytes (self , default : Optional [bytes ] = None ) -> Optional [bytes ]:
679+ """
680+ `get_bytes` method is basically [`read_bytes`](https://docs.python.org/3.13/library/pathlib.html#pathlib.Path.read_bytes)
681+ with support for extra `defaults` parameter.
682+
683+ Args:
684+ default:
685+ If the file does not exist, then `default` is returned. By default, `default` is `None`.
686+
687+ Returns:
688+ The binary contents of the pointed-to file as a bytes object or default (when file does not exist).
689+ """
690+ try :
691+ return super ().read_bytes ()
692+ except FileNotFoundError :
693+ return default
694+
635695
636696P = ProperPath
0 commit comments