Add convenient pathclass.read, write.

master
voussoir 2021-10-04 17:19:20 -07:00
parent 39d6140138
commit 0a1ab15637
No known key found for this signature in database
GPG Key ID: 5F7554F8C26DACCB
1 changed files with 21 additions and 0 deletions

View File

@ -275,6 +275,20 @@ class Path:
parent = os.path.dirname(self.absolute_path) parent = os.path.dirname(self.absolute_path)
return self.spawn(parent) 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 @property
def relative_path(self): def relative_path(self):
return self.relative_to(os.getcwd()) return self.relative_to(os.getcwd())
@ -358,6 +372,13 @@ class Path:
def with_child(self, basename): def with_child(self, basename):
return self.join(os.path.basename(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): def common_path(paths, fallback):
''' '''
Given a list of file paths, determine the deepest path which all Given a list of file paths, determine the deepest path which all