Step further with filebasedMessages
Dave Angel
davea at davea.name
Tue May 5 13:10:39 EDT 2015
On 05/05/2015 11:25 AM, Cecil Westerhof wrote:
>
> I have a file with quotes and a file with tips. I want to place random
> messages from those two (without them being repeated to soon) on my
> Twitter page. This I do with ‘get_random_message’. I also want to put
> the first message of another file and remove it from the file. For
> this I use ‘dequeue_message’.
>
Removing lines from the start of a file is an n-squared operation.
Sometiomes it pays to reverse the file once, and just remove from the
end. Truncating a file doesn't require the whole thing to be rewritten,
nor risk losing the file if the make-new-file-rename-delete-old isn't
quite done right.
Alternatively, you could overwrite the line, or more especially the
linefeed before it. Then you always do two readline() calls, using the
second one's result.
Various other games might include storing an offset at the begin of
file, so you start by reading that, doing a seek to the place you want,
and then reading the new line from there.
Not recommending any of these, just bringing up alternatives.
--
DaveA
More information about the Python-list
mailing list