lines here in the file-based filter of example - and also guarantee immediate file closures if the processing function fails with an exceptiondef filter_files(namefunction)with open(name' 'as inputopen(name out'' 'as outputfor line in inputoutput write(function(line)write the modified line and againfile object line iterators could simplify the stream-based filter' code in this example as welldef filter_stream(function)for line in sys stdinprint(function(line)end=''read by lines automatically since the standard streams are preopened for usthey're often easier to use when run standaloneit simply parrots stdin to stdoutc:\pp \system\filetoolsfilters py hillbillies txt *granny +jethro *elly may +"uncle jedbut this module is also useful when imported as library (clients provide the lineprocessing function)from filters import filter_files filter_files('hillbillies txt'str upperprint(open('hillbillies txt out'read()*granny +jethro *elly may +"uncle jedwe'll see files in action often in the remainder of this bookespecially in the more complete and functional system examples of first thoughwe turn to tools for processing our fileshome directory tools one of the more common tasks in the shell utilities domain is applying an operation to set of files in directory-- "folderin windows-speak by running script on batch of fileswe can automate (that isscripttasks we might have to otherwise run repeatedly by hand for instancesuppose you need to search all of your python files in development directory for global variable name (perhaps you've forgotten where it is usedthere are many platform-specific ways to do this ( the find and grep commands in unix)but python scripts that accomplish such tasks will work on every platform where python works--windowsunixlinuxmacintoshand just about any other platform directory tools