I have a python code with some print statements.Now, I want to read input from one file and output it to another file.How do i do it ? Should i include this ?
code :
fo = open("foo.txt", "r") foo = open("out.txt","w") Naive way:
fo = open("foo.txt", "r") foo = open("out.txt","w") foo.write(fo.read()) fo.close() foo.close() Better way, using with:
with open("foo.txt", "r") as fo: with open("out.txt", "w") as foo: foo.write(fo.read()) Nice way (using a module that does it for you - shutil.copy):
from shutil import copy copy("foo.txt", "out.txt")
with open("foo.txt") as fo, open("out.txt", "w") as foo: