Python Forum

Full Version: Unexpected output write
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've just taken up coding and have been teaching myself out of Simon Monk's "Programming the Raspberry Pi" book. On p79, he demonstrates how to write a file in the python shell. When I copy his code into my shell, instead of writing a file, it gives me the length of the string I tried to write.
I've tried different files, different variable names, restarting python, and trying different strings, but it's always the same result. I'm running python 3 on Raspbian.
My shell looks like this:
>>> f = open('test.txt', 'w')
>>> f.write('words')
5
>>>
hoping you can help
[email protected]
That the right result. If you total every write. Then you know how many chars are in the file.
You have to close file. closing a file will flush data. Saving it to file.
If you don't close file. It a memory leak.
better to use:
with open('test.txt', 'w') as f:
    f.write(...)
that way you don't have to remember to close as it's done automatically
Awesome makes sense. Thank you