【发布时间】:2014-01-15 19:52:02
【问题描述】:
通常,我们会使用它来读/写文件:
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 讨论过这个问题?
【问题讨论】:
标签: python file-io with-statement