Read entire file: Difference between revisions

From Rosetta Code
Content added Content deleted
(some omits)
(added PowerShell)
Line 12: Line 12:


The file is assumed to be in the default encoding.
The file is assumed to be in the default encoding.

=={{header|PowerShell}}==
<lang powershell>Get-Content foo.txt</lang>
With explicit selection of encoding:
<lang powershell>Get-Content foo.txt -Encoding UTF8</lang>


=={{header|Python}}==
=={{header|Python}}==

Revision as of 16:17, 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.

PowerShell

<lang powershell>Get-Content foo.txt</lang> With explicit selection of encoding: <lang powershell>Get-Content foo.txt -Encoding UTF8</lang>

Python

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

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