itertools - using python's 'with' while reading two files simultaneously with izip -


i regularly use python read 2 or more files simultaneously in following way:

for line1, line2 in izip(open(file1),open(file2)):     line1 , line2 

(using izip itertools package because files i'm reading huge , don't want load entire file memory).

i have been converted using with while reading files, apparently better since close open files if program crashes (at least that's understand discussion on here , other places):

with open(filename) fh:     line in fh:         line 

however, can't seem figure out how combine these 2 methods. when trying use izip in context, says 'itertools.izip' object has no attribute '__exit__' intuit part of reason why using with powerful.

so, there anyway use izip with?

you'll kick when see how obvious :

with open(fname1) f1, open(fname2) f2:     line1, line2 in izip(f1, f2):          ...  

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -