To read a file’s contents, call f.read(size), which reads some quantity of data and returns it as a string. size is an optional numeric argument. When size is omitted or negative, the entire contents of the file will be read and returned; it’s your problem if the file is twice as large as your machine’s memory. Otherwise, at most size bytes are read and returned. If the end of the file has been reached, f.read() will return an empty string ("").
def expanduser(path: Union[str, Path]):
"""
note: if you give in a path no need to get the output of this function because it mutates path. If you
give a string you do need to assign the output to a new variable
:param path:
:return:
"""
if not isinstance(path, Path):
# path: Path = Path(path).expanduser()
path: Path = Path(path).expanduser()
path = path.expanduser()
assert not '~' in str(path), f'Path username was not expanded properly see path: {path=}'
return path
def cat_file(path2filename: Union[str, Path]):
"""prints/displays file contents. Do path / filename or the like outside of this function. ~ is alright to use. """
path2filename = expanduser(path2filename)
with open(path2filename, 'r') as f:
print(f.read())