Handles are mentioned quite frequently throughout this documentation, and are also fairly confusing (at least to me!). Basically, you can think of a handle as being a ``wrapper’’ around text information.
Handles provide (at least) two benefits over plain text information:
- They provide a standard way to deal with information stored in different ways. The text information can be in a file, or in a string stored in memory, or at some remote website, but the handle provides a common way of dealing with information in all of these formats.
- They allow text information to be read incrementally, instead of all at once. This is really important when you are dealing with huge text files which would use up all of your memory if you had to load them all.
Handles can deal with text information that is being read (i. e. reading from a file) or written (i. e. writing information to a file). In the case of a ``read’’ handle, commonly used functions are
read(), which reads the entire text information from the handle, and
readline(), which reads information one line at a time. For ``write'' handles, the function
write() is regularly used.
The most common usage for handles is reading information from a file, which is done using the built-in python function
>>> handle = open("m_cold.fasta", "r")
">gi|8332116|gb|BE037100.1|BE037100 MP14H09 MP Mesembryanthemum crystallinum cDNA 5' similar to cold acclimation protein, mRNA sequence\n"