''' использование двух менеджеров контекста одновременно ''' # обычный способ try: with open(source) as fin: with open(target, "w") as fout: for line in fin: fout.write(process(line)) except EnvironmentError as err: print(err) # оптимизированный способ source = "/tmp/source.txt" target = "/tmp/target.txt" try: with open(source) as fin, open(target, 'w') as fout: for line in fin: fout.write(line) except EnvironmentError as err: print(err)