View difference between Paste ID: f43A7j5M and phGSaXjz
SHOW: | | - or go back to the newest paste.
1
with open('hello.txt', 'w+') as f:
2
	f.write('Hello world')
3
	f.seek(0)
4
	y = f.read()
5
	print y
6
7-
# with this format, the file will automatically close after the indent block. If you want to avoid this, use the following format:
7+
#with this format, the file will automatically close after the indent block. If you want to avoid #this, use the following format:
8
9
x = open('hello.txt', 'w+')
10
x.write('Hello world')
11
x.seek(0)
12
z = x.read()
13
print z
14
x.close()
15
16-
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.
16+
#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.