Add convenient pathclass.read, write.

This commit is contained in:
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

View file

@ -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