Python Forum
file handling Newbee question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
file handling Newbee question
#1
I am a pascal programmer learning python
Raspberry pi 2, python 3.9
f = open("temps.txt")
f.write("attemt1\r\n")
f.close
Nothing is written to the file. When I add the next lines to the script
f = open("temps.txt","r")
Now there is a new line in the file
What do I do wrong?
Thanks for your attention. Teunis
Reply
#2
I suggest to read Reading and Writing files on python.org

You didn't specified mode while opening the file and it defaults to 'r' (read). But you want to write, so you need provide 'w' or similar ('a', 'r+', etc).

And keep in mind:

Quote:Warning Calling f.write() without using the with keyword or calling f.close() might result in the arguments of f.write() not being completely written to the disk, even if the program exits successfully.

So you probably better off changing your habits and open files with open('tempts.txt', 'w') as f:
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
Thanks for the advice!
The mode was "a+" I forgot to write it
the hint to use
with openfile() as f: 
it solved my problem
Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  File Handling not working properly TheLummen 8 755 Feb-17-2024, 07:47 PM
Last Post: TheLummen
Star python exception handling handling .... with traceback mg24 3 1,287 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  File handling issue GiggsB 4 1,450 Mar-31-2022, 09:35 PM
Last Post: GiggsB
  How can I solve this file handling issue? GiggsB 17 3,594 Feb-14-2022, 04:37 AM
Last Post: GiggsB
  How to solve this file handling issue? GiggsB 3 1,705 Jan-10-2022, 09:36 AM
Last Post: Gribouillis
  File handling knollfinder 3 2,058 Jun-28-2020, 07:39 PM
Last Post: knollfinder
  Newbee Question chas777 4 2,256 Apr-01-2020, 06:52 PM
Last Post: chas777
  Useless Newbee craigpusey 2 69,474 Mar-04-2020, 02:13 PM
Last Post: craigpusey
  file handling sivareddy 1 1,643 Feb-23-2020, 07:28 PM
Last Post: jefsummers
  delete file with handling 3Pinter 1 2,110 Oct-17-2019, 04:06 PM
Last Post: 3Pinter

Forum Jump:

User Panel Messages

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