Skip to content Skip to sidebar Skip to footer

Most Efficient Way To "nibble" The First Line Of Text From A Text Document Then Resave It In Python

I have a text document that I would like to repeatedly remove the first line of text from every 30 seconds or so. I have already written (or more accurately copied) the code for th

Solution 1:

I'd use the fileinput module with inplace=True:

import fileinput

defremoveLine():
    inputfile = fileinput.input(path, inplace=True, mode='rU')
    next(inputfile, None)  # skip a line *if present*for line in inputfile:
        print line,  # write out again, but without an extra newline
    inputfile.close()

inplace=True causes sys.stdout to be redirected to the open file, so we can simply 'print' the lines.

The next() call is used to skip the first line; giving it a default None suppresses the StopIteration exception for an empty file.

This makes rewriting a large file more efficient as you only need to keep the fileinput readlines buffer in memory.

I don't think a deque is needed at all, even for your solution; just use next() there too, then use list() to catch the remaining lines:

defremoveLine():
    withopen(path, 'rU') as file:
        next(file, None)  # skip a line *if present*
        lines = list(file)
    withopen(path, 'w') as file:
        file.writelines(lines)  

but this requires you to read all of the file in memory; don't do that with large files.

Post a Comment for "Most Efficient Way To "nibble" The First Line Of Text From A Text Document Then Resave It In Python"