일반적으로 이것을 사용하여 파일을 읽고 / 씁니다.
with open(infile,'r') as fin:
pass
with open(outfile,'w') as fout:
pass
그리고 하나의 파일을 읽고 다른 파일로 출력하려면 하나만으로 할 수 with
있습니까?
나는 그렇게 해왔다.
with open(outfile,'w') as fout:
with open(infile,'r') as fin:
fout.write(fin.read())
다음과 같은 것이 있습니까 (다음 코드는 작동하지 않습니다).
with open(infile,'r'), open(outfile,'w') as fin, fout:
fout.write(fin.read())
with
여러 개가 아닌 하나를 사용하면 어떤 이점이 with
있습니까? 이에 대해 논의하는 PEP가 있습니까?
with open(infile,'r') as fin, open(outfile,'w') as fout:
fout.write(fin.read())
예전 에는 contextlib.nested (현재는 더 이상 사용되지 않음)를 사용해야 했지만 Python2.7 부터는 with
여러 컨텍스트 관리자를 지원합니다 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다