I am making a temperature log Python program that saves the CPU temperature to a CSV file every 10 seconds. How would I remove the first, say, 10 lines of the CSV file every month without deleting the file?

  • Should be lots of hits if you google. I'd probably use a random access file. This question is not Pi specific. – joan Jan 15 '18 at 18:12
  • Alternatively you can use logrotate – SBF Jan 16 '18 at 12:22

You can load the file into list, remove some indexes, and write it again:

file_name = 'file.csv'

file = open(file_name)
lines = files.readlines()

lines[0:10] = []

file = open(file_name, 'w')

You can enhance it a little by saving lines to other file (eg. file_name + '.new') and then move it back under its proper name (os.rename(...)).

HOWEVER its not efficient to rewrite the whole file again. OS has the potential to truncate the end of any file, but cannot freely truncate the beginning.

Thus, as the example shows, you have to process the whole file manually (load, store and process in memory, write).

I'd suggest to write your chunks of data to different files and then delete the ones that you don't need, or move them to archive, or just ignore them in your program.

Not the answer you're looking for? Browse other questions tagged or ask your own question.