io
Module for reading and writing files¶
The functions in this module make it easy to:
- check if a file exists and is writable
- check if a file and its parent directories exist and are writable
- check if a file exists and is readable
- check if a path exists and is a directory
- open an appropriate reader or writer based on the file extension
- write items to a file, one per line
- read lines from a file
fgpyo.io Examples:¶
>>> import fgpyo.io as fio
>>> from fgpyo.io import write_lines, read_lines
>>> from pathlib import Path
Assert that a path exists and is readable:
>>> tmp_dir = Path(getfixture("tmp_path"))
>>> path_flat: Path = tmp_dir / "example.txt"
>>> fio.assert_path_is_readable(path_flat)
Traceback (most recent call last):
...
AssertionError: Cannot read non-existent path: ...
Write to and read from path:
>>> path_flat = tmp_dir / "example.txt"
>>> path_compressed = tmp_dir / "example.txt.gz"
>>> write_lines(path=path_flat, lines_to_write=["flat file", 10])
>>> write_lines(path=path_compressed, lines_to_write=["gzip file", 10])
Read lines from a path into a generator:
>>> lines = read_lines(path=path_flat)
>>> next(lines)
'flat file'
>>> next(lines)
'10'
>>> lines = read_lines(path=path_compressed)
>>> next(lines)
'gzip file'
>>> next(lines)
'10'
Functions¶
assert_directory_exists ¶
Asserts that a path exist and is a directory
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to check |
required |
Example
assert_directory_exists(path = Path("/example/directory/"))
Source code in fgpyo/io/__init__.py
assert_fasta_indexed ¶
Verify that a FASTA is readable and has the expected index files.
The existence of the FASTA index generated by samtools faidx will always be verified. The
existence of the index files generated by samtools dict and bwa index may be optionally
verified.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fasta
|
Path
|
Path to the FASTA file. |
required |
dictionary
|
bool
|
If True, check for the index file generated by |
False
|
bwa
|
bool
|
If True, check for the index files generated by |
False
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If the FASTA or any of the expected index files are missing or not readable. |
Source code in fgpyo/io/__init__.py
assert_path_is_readable ¶
Checks that file exists and returns True, else raises AssertionError
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
a Path to check |
required |
Example
assert_file_exists(path = Path("some_file.csv"))
Source code in fgpyo/io/__init__.py
assert_path_is_writable ¶
Assert that a filepath is writable.
Specifically:
- If the file exists then it must also be writable.
- Else if the path is not a file and parent_must_exist is true, then assert that the parent
directory exists and is writable.
- Else if the path is not a directory and parent_must_exist is false, then look at each parent
directory until one is found that exists and is writable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to check |
required |
parent_must_exist
|
bool
|
If True, the file's parent directory must exist. Otherwise, at least one directory in the path's components must exist. |
True
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If any of the above conditions are not met. |
Example
assert_path_is_writable(path = Path("example.txt"))
Source code in fgpyo/io/__init__.py
assert_path_is_writeable ¶
A deprecated alias for assert_path_is_writable().
Source code in fgpyo/io/__init__.py
read_lines ¶
Takes a path and reads each line into a generator, removing line terminators
along the way. By default, only line terminators (CR/LF) are stripped. The strip
parameter may be used to strip both leading and trailing whitespace from each line.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to read from |
required |
strip
|
bool
|
True to strip lines of all leading and trailing whitespace, False to only remove trailing CR/LF characters. |
False
|
threads
|
Optional[int]
|
the number of threads to use when decompressing gzip files |
None
|
Example
import fgpyo.io as fio read_back = fio.read_lines(path)
Source code in fgpyo/io/__init__.py
redirect_to_dev_null ¶
A context manager that redirects output of file handle to /dev/null
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_num
|
int
|
number of filehandle to redirect. |
required |
Source code in fgpyo/io/__init__.py
suppress_stderr ¶
A context manager that redirects output of stderr to /dev/null
to_reader ¶
Opens a Path for reading and based on extension uses open() or gzip_ng.open()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to read from |
required |
threads
|
Optional[int]
|
the number of threads to use when decompressing gzip files |
None
|
Example
import fgpyo.io as fio reader = fio.to_reader(path=Path("reader.txt")).readlines().close()
Source code in fgpyo/io/__init__.py
to_writer ¶
Opens a Path for writing (or appending) and based on extension uses open() or gzip_ng.open()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to write (or append) to |
required |
append
|
bool
|
open the file for appending |
False
|
threads
|
Optional[int]
|
the number of threads to use when compressing gzip files |
None
|
Example
import fgpyo.io as fio writer = fio.to_writer(path=Path("writer.txt")).write("something\n").close()
Source code in fgpyo/io/__init__.py
write_lines ¶
write_lines(path: Path, lines_to_write: Iterable[Any], append: bool = False, threads: Optional[int] = None) -> None
Writes (or appends) a file with one line per item in provided iterable
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to write (or append) to |
required |
lines_to_write
|
Iterable[Any]
|
items to write (or append) to file |
required |
append
|
bool
|
open the file for appending |
False
|
threads
|
Optional[int]
|
the number of threads to use when compressing gzip files |
None
|
Example
lines: List[Any] = ["things to write", 100] path_to_write_to: Path = Path("file_to_write_to.txt") fio.write_lines(path = path_to_write_to, lines_to_write = lines)