Module tenlib.fs
Classes and function related to the filesystem (FS).
Heavily based on pathlib, this submodule overrides some methods to allow
easier and faster scripting.
Examples
For simple operations, use the function wrappers, such as:
>>> read_text('/tmp/file.txt')
'contents of file'
>>> read_bytes('/tmp/file.txt')
b'contents of file'
>>> # The write method supports both bytes and str.
>>> write('/tmp/file.txt', 'new contents')
>>> write('/tmp/file.txt', b'new contents in bytes')
>>> exists('/tmp/other_file.txt')
False
Otherwise, you can use the Path class:
>>> p = Path('/tmp/random_dir/file.txt')
>>> p.exists()
False
>>> # Creates the file, and superdirectories if required
>>> p.touch(dirs=True)
>>> p.exists()
True
>>> p.write('some contents\n')
>>> p.append('additional contents')
>>> p.read_text()
'some_contents\nadditional contents'
You can handle directories using the standard pathlib API:
>>> p = Path('/tmp/')
>>> p2 = p / 'sub_directory'
>>> list(p2.glob('**/*.py'))
[
fs.Path('/tmp/sub_directory/file1.py'),
fs.Path('/tmp/sub_directory/dir/file2.py'),
fs.Path('/tmp/sub_directory/file3.py')
]
Functions
def read_text(path: str) ‑> str-
Returns the contents of file
pathasstr. def read_bytes(path: str) ‑> bytes-
Returns the contents of file
pathasbytes. def mkdir(path: str, mode: int = 511, parents: bool = False, exist_ok: bool = False) ‑> Path-
Creates a directory
path. def exists(path: str) ‑> bool-
Returns whether file
pathexists. def write(path: str, data: StrOrBytes) ‑> Path-
Writes
datato filepath.Args
path:str- Path to the file
data:str, bytes- Data to write in the file
Classes
class Path (*args, **kwargs)-
Wrapper around
pathlib.Paththat provides a few extra functions.Expand source code
class Path(pathlib.PosixPath): """Wrapper around `pathlib.Path` that provides a few extra functions.""" def write(self, data: StrOrBytes) -> int: """Writes `data` to file. Args: data (str, bytes): Data to write in the file """ if isinstance(data, (bytes, bytearray)): return self.write_bytes(data) else: return self.write_text(data) def append(self, data: StrOrBytes) -> int: """Append `data` to file. Args: data (str, bytes): Data to append to the file """ mode = isinstance(data, (bytes, bytearray)) and "ab" or "a" with self.open(mode=mode) as f: return f.write(data) def touch( self, mode: int = 0o666, exist_ok: bool = True, parents: bool = False ) -> Path: """Wrapper for `Path.touch` that returns the instance, and creates sub- directories if `parents` is set (default: `False`). Returns: itself """ if parents: self.parent.mkdir(parents=True, exist_ok=True) super().touch(mode, exist_ok) return self def ptouch(self, mode: int = 0o666, exist_ok: bool = True) -> Path: """Wrapper for `Path.touch` that creates every sub directory. Returns: itself """ return self.touch(mode, exist_ok, parents=True)Ancestors
- pathlib.PosixPath
- pathlib.Path
- pathlib.PurePosixPath
- pathlib.PurePath
Methods
def write(self, data: StrOrBytes) ‑> int-
Writes
datato file.Args
data:str, bytes- Data to write in the file
def append(self, data: StrOrBytes) ‑> int-
Append
datato file.Args
data:str, bytes- Data to append to the file
def touch(self, mode: int = 438, exist_ok: bool = True, parents: bool = False) ‑> Path-
Wrapper for
Path.touch()that returns the instance, and creates sub- directories ifparentsis set (default:False).Returns
itself
def ptouch(self, mode: int = 438, exist_ok: bool = True) ‑> Path