Ex16 in powershell acting weird

*TL;DR is below:
Was going over ex16 trying ex15 study drill out where you plug your code into python line by line.
I came across something that, at the time, blew my mind. I had my three variables line1, line2, line3 and was trying to write them to file. I had my file open on the side screen and when I ran ex16.py from command line, I could see the file lines disappear when truncated and whatever l entered into python would write to file.
so I am running python, made three variables, target = open(filename, 'w') watched lines in text disappear, target.write(line1 + '\n' + line2 + '\n' + line3 + '\n') and it python prints 42.
OK , not 42 but a number, and no lines were writing to text file as before. I tried writing one line at a time. Samething, only different numbers.
I could not , for the life of me, figure out why python was printing numbers when print function was not called and not writing to file when write function was.
SO after many tries, trying different print statements, and geting interesting, yet unhelpful, info (like <_io.TextIOWrapper name='text.txt' mode='w' encoding='cp1252'>)
I gave up for the night. Before exiting, I statred calling .close() on everything I .open(), just incase and for practice. As I did my text file suddenly filled with EVERY line I had tried to write to it.
I haven’t found much online to explain anything. What I mean is nothing. But Just before writing this I tried print(len)) and apparently those numbers are the length.

*TL;DR
lines from powershell:

>>> line1 = input()
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
>>> line2 = input()
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
>>>
>>> line3 = input()
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
>>>
>>>
>>>
>>>filename = input()
text.txt
>>> target = open(filename, 'w')
>>> target.write(line1 + '\n' + line2 + '\n' + line3 + '\n')
546
>>> print(repr(line1))
'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.'
>>> print(len(line1))
144
>>> print(len(line1 + line2 + line3))
543
>>> print(len(line1 + '\n' + line2 + '\n' + line3 + '\n'))
546
>>> target.close()

After all that, why does it print (return? display?) a number? in this case length.
And not write to file until close() is called?
Thanks.Hope that wasn’t too long, boring or incoherent.

When you open a file, what you actually get is a file stream. That’s an internal object, sort of a buffer layer above the actual file, where Python stores everything you write. Only when you close the stream (or tell Python explicitly to do so with file.flush()), the state of the buffer is written to the file.

It’s done this way because writing to the disk is expensive (slow), so it’s more efficient to write everything in one go.

In the REPL the numbers are printed because file.write() returns the number of characters written.

Thanks @florian. I really thought I had found some crazy glitch in python or something. It now seems less ominous. I now have a file.flushrabbit to follow. lets see how deep the rabbit hole goes. Why didn’t I take the blue pill.

If you’re feeling brave, you might want to read this page once:


You’re not going to understand much of it right away, but you’ll get the general picture. Also, I think the key concept to understand here is the difference between hardware storage (the “disk”) and memory/RAM. Maybe read up on that, too.

The rabbit hole might prove very instructive. :wink: