Open more than 1 file using with - python

Viewed 953

Normally, we would use this to read/write a file:

with open(infile,'r') as fin:
  pass
with open(outfile,'w') as fout:
  pass

And to do read one file and output to another, can i do it with just one with?

I've been doing it as such:

with open(outfile,'w') as fout:
  with open(infile,'r') as fin:
    fout.write(fin.read())

Is there something like the following, (the follow code dont work though):

with open(infile,'r'), open(outfile,'w') as fin, fout:
  fout.write(fin.read())

is there any benefit to using one with and not multiple with? is there some PEP where it discusses this?

2 Answers
Related