Bio.File module

Code for more fancy file handles.

Bio.File defines private classes used in Bio.SeqIO and Bio.SearchIO for indexing files. These are not intended for direct use.

Bio.File.as_handle(handleish, mode='r', **kwargs)

Context manager to ensure we are using a handle.

Context manager for arguments that can be passed to SeqIO and AlignIO read, write, and parse methods: either file objects or path-like objects (strings, pathlib.Path instances, or more generally, anything that can be handled by the builtin ‘open’ function).

When given a path-like object, returns an open file handle to that path, with provided mode, which will be closed when the manager exits.

All other inputs are returned, and are not closed.

  • handleish - Either a file handle or path-like object (anything which can be

    passed to the builtin ‘open’ function, such as str, bytes, pathlib.Path, and os.DirEntry objects)

  • mode - Mode to open handleish (used only if handleish is a string)

  • kwargs - Further arguments to pass to open(…)


>>> from Bio import File
>>> import os
>>> with File.as_handle('seqs.fasta', 'w') as fp:
...     fp.write('>test\nACGT')
>>> fp.closed
>>> handle = open('seqs.fasta', 'w')
>>> with File.as_handle(handle) as fp:
...     fp.write('>test\nACGT')
>>> fp.closed
>>> fp.close()
>>> os.remove("seqs.fasta")  # tidy up