Multiple context managers

suggest change

You can open several content managers at the same time:

with open(input_path) as input_file, open(output_path, 'w') as output_file:

    # do something with both files. 
    
    # e.g. copy the contents of input_file into output_file
    for line in input_file:
        output_file.write(line + '\n')

It has the same effect as nesting context managers:

with open(input_path) as input_file:
    with open(output_path, 'w') as output_file:
        for line in input_file:
            output_file.write(line + '\n')

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents