Read entire file: Difference between revisions

From Rosetta Code
Content added Content deleted
(New task)
 
(some omits)
Line 18: Line 18:


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

{{omit from|TI-83 BASIC|No filesystem.}}
{{omit from|TI-89 BASIC|No filesystem.}}

Revision as of 15:19, 29 June 2010

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.