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
  Unexpected output Starter 2 439 Nov-22-2023, 12:08 AM
Last Post: Starter
  Unexpected Output - Python Dataframes: Filtering based on Overlapping Dates Xensor 5 655 Nov-15-2023, 06:54 PM
Last Post: deanhystad
  Unexpected output while using random.randint with def terickson2367 1 469 Oct-24-2023, 05:56 AM
Last Post: buran
  Unexpected output from df.loc when indexing by label idratherbecoding 6 1,126 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 1,046 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
Photo how I write the output into xml file in python? 3lnyn0 1 2,165 Oct-31-2021, 05:40 PM
Last Post: Gribouillis
  unexpected output asyrafcc99 0 1,479 Oct-24-2020, 02:40 PM
Last Post: asyrafcc99
  Unexpected output: symbols for derivative not being displayed saucerdesigner 0 2,012 Jun-22-2020, 10:06 PM
Last Post: saucerdesigner
  can we write command output to new csv file using Panda package? PythonBeginner_2020 3 2,322 Mar-13-2020, 12:38 PM
Last Post: ndc85430
  Unexpected output palladium 4 2,696 Jan-11-2020, 03:26 PM
Last Post: palladium

Forum Jump:

User Panel Messages

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