By the way, is pasted code recognized automatically on the forum? I can't figure out how to preserve indents :(
Edit: Thanks pudquick!
<PRE>with open('hello.txt', 'w+') as f:
f.write('Hello world')
f.seek(0)
y = f.read()
print y</PRE>
With this format, the file will automatically close after the indent block. If you want to avoid this, use the following format:
<PRE>x = open('hello.txt', 'w+')
x.write('Hello world')
x.seek(0)
z = x.read()
print z
x.close()</PRE>
I used w+ because I wanted to read and write from the same file without closing and reopening it. In general, use 'a' to append (as opposed to writing over), 'r' to read, and 'w' to write.