Bio.File module¶
Code for more fancy file handles.
- Classes:
UndoHandle File object decorator with support for undo-like operations.
Additional private classes used in Bio.SeqIO and Bio.SearchIO for indexing files are also defined under Bio.File but 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.
- Arguments:
- handleish - Either a file handle or path-like object (anything which can be
passed to the builtin ‘open’ function: str, bytes, and under Python >= 3.6, pathlib.Path, os.DirEntry)
mode - Mode to open handleish (used only if handleish is a string)
kwargs - Further arguments to pass to open(…)
Examples
>>> from Bio import File >>> with File.as_handle('seqs.fasta', 'w') as fp: ... # Python2/3 docstring workaround, revise for 'Python 3 only'. ... _ = fp.write('>test\nACGT') ... >>> fp.closed True
>>> handle = open('seqs.fasta', 'w') >>> with File.as_handle(handle) as fp: ... # Python 2/3 docstring workaround, revise for 'Python 3 only'. ... _ = fp.write('>test\nACGT') ... >>> fp.closed False >>> fp.close()
Note that if the mode argument includes U (for universal new lines) this will be removed under Python 3 where is is redundant and has been deprecated (this happens automatically in text mode).
-
class
Bio.File.
UndoHandle
(handle)¶ Bases:
object
A Python handle that adds functionality for saving lines.
Saves lines in a LIFO fashion.
- Added methods:
saveline Save a line to be returned next time.
peekline Peek at the next line without consuming it.
-
__init__
(self, handle)¶ Initialize the class.
-
__iter__
(self)¶ Iterate over the lines in the File.
-
__next__
(self)¶ Return the next line.
-
readlines
(self, *args, **keywds)¶ Read all the lines from the file as a list of strings.
-
readline
(self, *args, **keywds)¶ Read the next line from the file as string.
-
read
(self, size=- 1)¶ Read the File.
-
saveline
(self, line)¶ Store a line in the cache memory for later use.
This acts to undo a readline, reflecting the name of the class: UndoHandle.
-
peekline
(self)¶ Return the next line in the file, but do not move forward though the file.
-
tell
(self)¶ Return the current position of the file read/write pointer within the File.
-
seek
(self, *args)¶ Set the current position at the offset specified.
-
__getattr__
(self, attr)¶ Return File attribute.
-
__enter__
(self)¶ Call special method when opening the file using a with-statement.
-
__exit__
(self, type, value, traceback)¶ Call special method when closing the file using a with-statement.