Python Forum
Strange write()/File behavior
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Strange write()/File behavior
#1
Hello everyone,
I ran into some strange behavior I don't understand while completing this online course. I was hoping you could help me understand it.

# Reading and writing at the same time
in_file = open('c:/users/*****/desktop/mydata.txt', 'r+')
print(in_file.readline())
in_file.seek(0)
in_file.write('Hi!')
print(in_file.readline())
in_file.close()
The file mydata.txt contains the text "Hello\nWorld!".
In the above code, for reasons I don't understand, it doesn't write to the file. I would have expected it to write "Hi!" over the first three characters and then print the remaining characters "lo", but instead it doesn't write at all and the second readline() prints "Hello" a second time

Output:
Hello Hello
So I wanted to figure out why it wasn't working and decided to add the line "print(in_file.tell())"
# Reading and writing at the same time
in_file = open('c:/users/*****/desktop/mydata.txt', 'r+')
print(in_file.readline())
in_file.seek(0)
in_file.write('Hi!')
print(in_file.tell())
print(in_file.readline())
in_file.close()
Output:
Hello 3 lo
Well now it works as expected. But it only works if I use either tell() or seek() on it after the write() keyword. This works for my assignment because I was required to seek() back to the beginning of the file anyway, but I'm curious why it doesn't write at all in the first example.

Is this the compiler omitting to write the file because I didn't utilized the file/variable enough?
Reply
#2
This is an OS/stdio library thing and python doesn't do anything to modify the behavior.

If you have a file you're both reading and writing, you need to flush the buffer or reposition between operations Something like seek(tell(...)) would be fine. You'd have the same limitation in C.

Basically you executed the write(), but it's written to a buffer rather than the file itself. Then when you read() you updated the buffer pointer. Later when the file was closed, the write() buffer is flushed and it wrote at the new position rather than where you intended. Your file was being updated, but since you only printed the first line you didn't notice.

An in_file.flush() after the write and before the read would also work.
BashBedlam likes this post
Reply
#3
Thanks for your reply.

After you mentioned flush() I remembered it was mentioned earlier in my lesson. I went back and reread that section.

Thanks again for the help :-)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Last record in file doesn't write to newline gonksoup 3 365 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  strange behavior of chess library in Python max22 1 267 Jan-18-2024, 06:35 PM
Last Post: deanhystad
  write to csv file problem jacksfrustration 11 1,375 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,314 Nov-09-2023, 10:56 AM
Last Post: mg24
  How do I read and write a binary file in Python? blackears 6 6,018 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,048 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Program running on RPi 3b+ Very Strange Behavior - Out of Bound Index MadMacks 26 3,055 Mar-07-2023, 09:50 PM
Last Post: MadMacks
  Read text file, modify it then write back Pavel_47 5 1,501 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  how to read txt file, and write into excel with multiply sheet jacklee26 14 9,523 Jan-21-2023, 06:57 AM
Last Post: jacklee26
  How to write in text file - indented block Joni_Engr 4 6,363 Jul-18-2022, 09:09 AM
Last Post: Hathemand

Forum Jump:

User Panel Messages

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