Python simplifies many things and contains libraries and wrappers for many common operations, but the goal is not to hide fundamental truths.
The basic truth that you encounter is that you usually cannot add data to an existing flat structure without rewriting the entire structure. This is true regardless of language.
There are ways to save the file descriptor or make your code less readable, many of which are presented in other answers, but none of them change the fundamental operation: you must read in the existing file and then write down the data that you want to add, followed by existing data that you are reading.
Be sure to save the file descriptor file, but do not try to package this operation as few lines of code as possible. In fact, never look for the smallest lines of code - this is obfuscation, not programming.
Nicholas Knight Dec 15 '10 at 20:24 2010-12-15 20:24
source share