In Python, how can I open a file and read it on one line, and still be able to close the file afterwards?

前端 未结 7 2130
面向向阳花
面向向阳花 2021-02-10 07:26

While working through this exercise I ran into a problem.

from sys import argv
from os.path import exists

script, from_file, to_file = argv
print \"Copying from         


        
7条回答
  •  日久生厌
    2021-02-10 07:58

    The following Python code will accomplish your goal.

    from contextlib import nested
    
    with nested(open('input.txt', 'r'), open('output.txt', 'w')) as inp, out:
        indata = inp.read()
        ...
        out.write(out_data)
    

提交回复
热议问题