why am I getting a no output on file read and right option | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

why am I getting a no output on file read and right option

I don't know where I went wrong file = ("a.txt","w+") file.write ("hello world") cont = file.read() print(cont) file.close()

23rd Jul 2016, 1:55 AM
Wayne Gaebelein
Wayne Gaebelein - avatar
5 Answers
+ 1
Ah, fair point. After the write operation the file cursor is at the end of the file, which means that tge read is essentially reading from the end of the file till the end of the file ... Try moving the cursor to the beginning of the file before the read operation: file.seek(0)
23rd Jul 2016, 3:57 AM
Kristian Ahlmann-Ohlsen
Kristian Ahlmann-Ohlsen - avatar
0
Remember to open / create the file: file = open("a.txt", "w+")
23rd Jul 2016, 3:10 AM
Kristian Ahlmann-Ohlsen
Kristian Ahlmann-Ohlsen - avatar
0
file = open("a.txt","w+") file.write ("helloworld") cont = file.read() print (cont) file.close
23rd Jul 2016, 3:35 AM
Wayne Gaebelein
Wayne Gaebelein - avatar
0
still no output
23rd Jul 2016, 3:36 AM
Wayne Gaebelein
Wayne Gaebelein - avatar
0
that solved it it didn't go over that
23rd Jul 2016, 4:05 AM
Wayne Gaebelein
Wayne Gaebelein - avatar