Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unexpected output write
#1
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]
Reply
#2
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.
99 percent of computer problems exists between chair and keyboard.
Reply
#3
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
Reply
#4
Awesome makes sense. Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to run shell command, capture the output, then write it into textfile? tatahuft 4 865 Dec-20-2024, 02:13 PM
Last Post: Axel_Erfurt
  Unexpected output when trying to attach files in a mail application PythonU2Novel 0 1,012 May-17-2024, 02:59 AM
Last Post: PythonU2Novel
  Unexpected output Starter 2 1,200 Nov-22-2023, 12:08 AM
Last Post: Starter
  Unexpected Output - Python Dataframes: Filtering based on Overlapping Dates Xensor 5 2,003 Nov-15-2023, 06:54 PM
Last Post: deanhystad
  Unexpected output while using random.randint with def terickson2367 1 1,209 Oct-24-2023, 05:56 AM
Last Post: buran
  Unexpected output from df.loc when indexing by label idratherbecoding 6 3,447 Apr-19-2023, 12:11 AM
Last Post: deanhystad
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 2,074 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
Photo how I write the output into xml file in python? 3lnyn0 1 3,908 Oct-31-2021, 05:40 PM
Last Post: Gribouillis
  unexpected output asyrafcc99 0 1,940 Oct-24-2020, 02:40 PM
Last Post: asyrafcc99
  Unexpected output: symbols for derivative not being displayed saucerdesigner 0 2,566 Jun-22-2020, 10:06 PM
Last Post: saucerdesigner

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020