+ 2

'with' statement question of Python

I wrote the code using 'with' statement, like this. with open("D:/0. Python Practice/0. txt files/new text.txt", "w+") as text_file: text_file.write("abc") print(text_file.read()) When I run the above code, there isn't any error, but the result shows just blank. I wanna print the contents of the text_file. P.S. I don't understand how can I use the "w+" & "a+".

29th Dec 2018, 5:04 PM
Wisbeom
Wisbeom - avatar
1 Antwort
+ 5
Add text_file.seek(0) before the last line to jump back to the beginning of the text file before you start reading from it. /Edit: This even works in Sololearn: https://code.sololearn.com/cUV1TB6CSuLr/?ref=app You can read about a+ and w+ here (it's the python 2 doc, but it's the same in python 3): https://docs.python.org/2/library/functions.html#open
29th Dec 2018, 5:56 PM
Anna
Anna - avatar