augur.io.file module
- augur.io.file.create_parent_directories(file)
Create missing parent directories for path-like files.
>>> import tempfile >>> path = tempfile.mkdtemp() + "/nested/output.txt" >>> create_parent_directories(path) >>> os.path.isdir(os.path.dirname(path)) True
- augur.io.file.is_write_mode(mode)
Return whether
modeis a write mode.Only modes containing
"w"or"a"are treated as write modes. Exclusive creation mode"x"is intentionally excluded because xopen does not support it.Examples
>>> is_write_mode("w") True >>> is_write_mode("wb") True >>> is_write_mode("a") True >>> is_write_mode("r") False >>> is_write_mode("x") False
- augur.io.file.open_file(path_or_buffer, mode='r', **kwargs)
Opens a given file path and returns the handle.
Transparently handles compressed inputs and outputs.
- Parameters:
path_or_buffer -- Name of the file to open or an existing IO buffer
mode (str) -- Mode to open file (read or write)
- Returns:
File handle object
- Return type:
IO
Examples
Pass through an existing buffer unchanged.
>>> from io import StringIO >>> buf = StringIO("hello") >>> with open_file(buf) as handle: ... handle.read() 'hello'
Open a normal text file. Parent directories are created.
>>> from tempfile import TemporaryDirectory >>> from pathlib import Path >>> with TemporaryDirectory() as d: ... parent = Path(d) / "nested" ... path = parent / "example.txt" ... with open_file(path, mode="w") as handle: ... _ = handle.write("hello") ... path.read_text(encoding=ENCODING) ... parent.exists() 'hello' True
‘x’ mode is not supported.
>>> with TemporaryDirectory() as d: ... parent = Path(d) / "nested" ... path = parent / "example.txt" ... try: ... with open_file(path, mode="x") as handle: ... _ = handle.write("hello") ... except ValueError as e: ... print(e) ... parent.exists() Mode 'x' not supported False