solo2500
Posts: 116
Joined: Sat Jul 09, 2016 12:38 am

Delay in writing to text file

Sat May 23, 2020 2:58 pm

I don't know much about programming so please bare with me.

I have a simple python program that writes a few bits of data to a text file every 30 minuets of so. The data does not show up until 2 or 3 entries have been made. Like it get stuck in a buffer or something?

Here is a bit of the code:

Code: Select all

path = '/home/pi/Desktop/clock_data.txt'
days_file = open(path,'a')
days_file.write(bob + "\n")
print('end of data')
days_file.close
Does anyone have an idea why this is happening?

Thanks for any and ALL help!
I'm a total novice, non-programer (...basically a hack.)

trejan
Posts: 1556
Joined: Tue Jul 02, 2019 2:28 pm

Re: Delay in writing to text file

Sat May 23, 2020 3:00 pm

You're using buffered I/O. You need to use flush.

Paul Hutch
Posts: 495
Joined: Fri Aug 25, 2017 2:58 pm
Location: Blackstone River Valley, MA, USA
Contact: Website

Re: Delay in writing to text file

Sat May 23, 2020 3:10 pm

Change the buffering parameter of the open function. IIRC, the default for normal text files on Raspbian is 4 KiB (4096 bytes), this reduces the frequency of writes which increases performance and reduces uSD card wear.

For line buffering use:

Code: Select all

days_file = open(path, 'a', 1)
To specify a specific number of bytes to buffer use that number instead of the "1".
Details here:
https://docs.python.org/3.5/library/functions.html#open

solo2500
Posts: 116
Joined: Sat Jul 09, 2016 12:38 am

Re: Delay in writing to text file

Sat May 23, 2020 4:44 pm

THANK ALL!
I'm a total novice, non-programer (...basically a hack.)

Return to “Python”