[Tutor] Scanning a file for specific text and copying it to a new file

Emile van Sebille emile at fenx.com
Fri Dec 3 00:25:23 CET 2010

On 12/2/2010 10:27 AM Ben Ganzfried said...
> I'm trying to build a program that reads in a file and copies specific
> sections to a new file.  More specifically, every time the words
> "summary on" are in the original file, I want to copy the following
> text to the new file until I get to the words "summary off".
> My questions are the following:
> 1) Once I have read in the old file, how do I copy just the parts that
> I want to the new file?

I'd consider reading the entire file in, then use the resulting string's 
split method to break out the parts that will need to be written, and 
finally write those out.

If the file you're reading is very large (eg, several hundred MB), or 
competition is high for memory, you'll want to consider loops.


> What command do I give the computer to know
> that "summary on" (whether capitalized or not) means start writing to
> the new file-- and "summary off" means stop?  Also, I assume I'll put
> all of this in a while loop based on the condition that we should read
> until we are done reading the whole document?  (although, this is
> somewhat inefficient because the last "summary on/off" could be way
> before the end, but this is preferable to missing one that is at the
> end...)
> Thank you very much for any help you can provide.  Much appreciated!
> Ben

More information about the Tutor mailing list