在完成这个练习时,我遇到了一个问题。
from sys import argv
from os.path import exists
script, from_file, to_file = argv
print "Copying from %s to %s" % (from_file, to_file)
# we could do these two on one line too, how?
input = open(from_file)
indata = input.read()
print "The input file is %d bytes long" % len(indata)
print "Does the output file exist? %r" % exists(to_file)
print "Ready, hit RETURN to continue, CTRL-C to abort."
raw_input()
output = open(to_file, 'w')
output.write(indata)
print "Alright, all done."
output.close()
input.close()
这条线# we could do these two on one line too, how?
让我感到困惑。我能想到的唯一答案是:
indata = open(from_file).read()
这执行了我想要的方式,但它需要我删除:
input.close()
因为输入变量不再存在。那么,我该如何执行此关闭操作?
你会如何解决这个问题?