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?
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() file.close() lines[0:10] =  file = open(file_name, 'w') file.writelines(lines) file.close()
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 (
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.