Read entire file

From Rosetta Code
Revision as of 15:19, 29 June 2010 by rosettacode>Kevin Reid (some omits)
Task
Read entire file
You are encouraged to solve this task according to the task description, using any language you may know.

Load the entire contents of some text file as a single string variable.

If applicable, discuss: encoding selection, the possibility of memory-mapping.

Of course, one should avoid reading an entire file at once if the file is large and the task can be accomplished incrementally instead (in which case check File IO); this is for those cases where having the entire file is actually what is wanted.

E

<lang e><file:foo.txt>.getText()</lang>

The file is assumed to be in the default encoding.

Python

<lang python>open(filename).read()</lang>

This returns a byte string and does not assume any particular encoding.