Easiest way to read/write a file's content in Python
<p>In Ruby you can read from a file using <code>s = File.read(filename)</code>. The shortest and clearest I know in Python is</p>

<pre><code>with file(filename) as f:
    s = f.read()
</code></pre>

<p>Is there any other way to do it that makes it even shorter (preferably one line) and more readable?</p>

<p>Note: initially I phrased the question as "doing this in a single line of code". As pointed by S.Lott, shorter doesn't necessary mean more readable. So I rephrased my question just to make clear what I meant. I think the Ruby code is better and more readable not necessarily because it's one line versus two (though that matters as well), but also because it's a class method as opposed to an instance method, which poses no question about who closes the file, how to make sure it gets closed even if an exception is raised, etc. As pointed in the answers below, you can rely on the GC to close your file (thus making this a one-liner), but that makes the code worse even though it's shorter. Not only by being unportable, but by making it unclear.</p>
