This question has already been asked here and here, but none of the solutions worked for me.
How do I remove the first line from a large file efficiently in Python 3?
I am writing a program which requires logging, and the log file has a configurable maximum size, which could be infinite. Therefore, I do not want to use readlines()
or similar methods as these would be memory intensive. Speed is not a huge concern, but if it can be done without rewriting the entire file, and without temporary files, that would be great.
Solutions need to be cross-platform.
Example log file:
[09:14:56 07/04/17] [INFO] foo
[23:45:01 07/04/17] [WARN] bar
[13:45:28 08/04/17] [INFO] foobar
... many thousands more lines
Output:
[23:45:01 07/04/17] [WARN] bar
[13:45:28 08/04/17] [INFO] foobar
... many thousands more lines
This code will be run in a loop:
while os.path.getsize(LOGFILE) > MAXLOGSIZE:
# remove first line of file
None of the following solutions work and are memory efficient:
Solution #1 - works but inefficient
with open('file.txt', 'r') as fin:
data = fin.read().splitlines(True)
with open('file.txt', 'w') as fout:
fout.writelines(data[1:])
Solution #2 - doesn't work, leaves file empty
import shutil
source_file = open('file.txt', 'r')
source_file.readline()
target_file = open('file.txt', 'w')
shutil.copyfileobj(source_file, target_file)
Solution #3 - works, efficient, but uses additional file:
with open("file.txt",'r') as f:
with open("new_file.txt",'w') as f1:
f.next() # skip header line
for line in f:
f1.write(line)