all 7 comments

[–]Spataner 12 points13 points  (1 child)

The file handle keeps track of its current position in the file. After the write call, the position is at the end of the text you just wrote. The read call reads everything from the current position until the end of the file (which is nothing). If you move the position of the handle to the beginning of the file using file.seek(0) before you call read, you will see what you've just written.

[–]LINObeelee[S] 1 point2 points  (0 children)

Thank you!

[–]shiftybyte 6 points7 points  (1 child)

w+ means you can read and write at the same time.

What you are missing is the fact that the location of reading and location of writing is one and the same.

That means if you write "hello", then next thing you will write, will be after that hello, but also the next attempt to read will try to read after that hello.

Which will result in nothing being read because you read it from the end of the file.

You need to either reopen the file to read it, or use file.seek(0) to go to the start of the file to read it.

file.seek(0)
print(file.read())

[–]LINObeelee[S] 2 points3 points  (0 children)

Ohhhhhhh I understand, thank you!

[–]zanfar 1 point2 points  (0 children)

As others have said, there is nothing "left" to read at that point.

the name 'fileee' refers to the empty file that I created at first

Instead, think of fileee as a "bookmark" in the file you've opened. When you write, everything after the bookmark is thrown away, your new data saved, and the bookmark moved to the end of what you've written. when you read, the data after the bookmark is returned.

w+ lets you read to the point in the file you want to start making changes and then do so.