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
The preferred way to work with resources in python is to use context managers:
with open(infile) as fp:
indata = fp.read()
The with
statement takes care of closing the resource and cleaning up.
You could write that on one line if you want:
with open(infile) as fp: indata = fp.read()
however, this is considered bad style in python.
You can also open multiple files in a with
block:
with open(input, 'r') as infile, open(output, 'w') as outfile:
# use infile, outfile
Funny enough, I asked exactly the same question back when I started learning python.