things up by shifting processing from python code to built-in tools for instanceif we're concerned with speedwe can probably make our file scanner faster by using the file' line iterator to step through the file instead of the manual readline loop in example - (though you' have to time this with your python to be sure)def scanner(namefunction)for line in open(name' ')function(linescan line by line call function object and we can work more magic in example - with the iteration tools like the map builtin functionthe list comprehension expressionand the generator expression here are three minimalist' versionsthe for loop is replaced by map or comprehensionand we let python close the file for us when it is garbage collected or the script exits (these all build temporary list of results along the way to run through their iterationsbut this overhead is likely trivial for all but the largest of files)def scanner(namefunction)list(map(functionopen(name' '))def scanner(namefunction)[function(linefor line in open(name' ')def scanner(namefunction)list(function(linefor line in open(name' ')file filters the preceding works as plannedbut what if we also want to change file while scanning itexample - shows two approachesone uses explicit filesand the other uses the standard input/output streams to allow for redirection on the command line example - pp \system\filetools\filters py import sys def filter_files(namefunction)input open(name' 'output open(name out'' 'for line in inputoutput write(function(line)input close(output close(filter file through function create file objects explicit output file too def filter_stream(function)while trueline sys stdin readline(if not linebreak print(function(line)end=''no explicit files use standard streams orinput(if __name__ ='__main__'filter_stream(lambda lineline file and directory tools write the modified line output has outsuffix orsys stdout write(copy stdin to stdout if run