From 0a1ab156373f827c4cb4c8667c923c539f8bcfe8 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Mon, 4 Oct 2021 17:19:20 -0700 Subject: [PATCH] Add convenient pathclass.read, write. --- voussoirkit/pathclass.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/voussoirkit/pathclass.py b/voussoirkit/pathclass.py index 867964f..881c412 100644 --- a/voussoirkit/pathclass.py +++ b/voussoirkit/pathclass.py @@ -275,6 +275,20 @@ class Path: parent = os.path.dirname(self.absolute_path) return self.spawn(parent) + def read(self, mode, **kwargs): + ''' + Shortcut function for opening the file handle and reading data from it. + ''' + with self.open(mode, **kwargs) as handle: + return handle.read() + + def readlines(self, mode, **kwargs): + ''' + Shortcut function for opening the file handle and reading lines from it. + ''' + with self.open(mode, **kwargs) as handle: + return handle.readlines() + @property def relative_path(self): return self.relative_to(os.getcwd()) @@ -358,6 +372,13 @@ class Path: def with_child(self, basename): return self.join(os.path.basename(basename)) + def write(self, mode, data, **kwargs): + ''' + Shortcut function for opening the file handle and writing data into it. + ''' + with self.open(mode, **kwargs) as handle: + return handle.write(data) + def common_path(paths, fallback): ''' Given a list of file paths, determine the deepest path which all