python introduced with extension that eventually appeared in python as well in these and later pythonsthe with statement may also specify multiple (sometimes referred to as "nested"context managers with new comma syntax in the followingfor exampleboth filesexit actions are automatically run when the statement block exitsregardless of exception outcomeswith open('data'as finopen('res'' 'as foutfor line in finif 'some keyin linefout write(lineany number of context manager items may be listedand multiple items work the same as nested with statements in pythons that support thisthe following codewith (as ab(as bstatements is equivalent to the followingwhich also works in and with (as awith (as bstatements python ' release notes have additional detailsbut here' quick look at the extension in action--to implement parallel lines scan of two filesthe following uses with to open two files at once and zip together their lineswithout having to manually close when finished (assuming manual closes are required)with open('script py'as open('script py'as for pair in zip( )print(pair(' first python script\ ''import sys\ '('import sys load library module\ ''print(sys path)\ '('print(sys platform)\ '' \ '('print( * raise to power\ ''print( * )\ 'you might use this coding structure to do line-by-line comparison of two text filesfor example--replace the print with an if for simple file comparison operationand use enumerate for line numberswith open('script py'as open('script py'as for (linenum(line line )in enumerate(zip( ))if line !line print('% \ % \ % (linenumline line )stillthe preceding technique isn' all that useful in cpythonbecause input file objects don' require buffer flushand file objects are closed automatically when reclaimed if still open in cpythonthe files would be reclaimed immediately if the parallel scan were coded the following simpler wayfor pair in zip(open('script py')open('script py'))print(pair exception coding details same effectauto close