2010-05-17 5 views

답변

16

또한 수행 할 수 있습니다

파이썬 2.7에서
from contextlib import nested 

with nested(open(spam), open(eggs)) as (f_spam, f_eggs): 
    # do something 

3.1 with은 다음 구문을 지원하기 때문에 당신은 nested 기능이 필요하지 않습니다 : 유 할 것 방법

with open(spam) as f_spam, open(eggs) as f_eggs: 
    # do something 
+0

을 파이썬 3에서? – Tshepang

+3

'(스팸)을 f_spam으로, (계란)을 f_eggs :'.................. http : // docs에서 네 번째 글 머리표로 봅니다. python.org/release/3.1/whatsnew/3.1.html#other-language-changes – blokeley

3
with open(spam,'r') as f_spam: 
    with open(eggs,'r') as f_bar: 
    #do stuff with each 
관련 문제