7
이 경우 with 문을 어떻게 사용합니까?처리 할 여러 파일이있는 Python의 "With"문
f_spam = open(spam,'r')
f_bar = open(eggs,'r')
...
do something with these files
...
f_spam.close()
f_bar.close()
파일 번호는 2보다 클 수 있습니다.
이 경우 with 문을 어떻게 사용합니까?처리 할 여러 파일이있는 Python의 "With"문
f_spam = open(spam,'r')
f_bar = open(eggs,'r')
...
do something with these files
...
f_spam.close()
f_bar.close()
파일 번호는 2보다 클 수 있습니다.
또한 수행 할 수 있습니다
파이썬 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
with open(spam,'r') as f_spam:
with open(eggs,'r') as f_bar:
#do stuff with each
을 파이썬 3에서? – Tshepang
'(스팸)을 f_spam으로, (계란)을 f_eggs :'.................. http : // docs에서 네 번째 글 머리표로 봅니다. python.org/release/3.1/whatsnew/3.1.html#other-language-changes – blokeley