它会产生错误信息。它是做什么的?

时间:2016-02-21 03:32:09

标签: python-2.7

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)

in_file = open(from_file)
indata = in_file.read()

print "The input file is %d bytes long" % len(indata)

print "Does the output file exists? %r" % exists(to_file)

print "Ready, hit RETURN to continue, CTRL-C to abort."
raw_input()

out_file = open(to_file,'w')
out_file.write(indata)

print"Alright, all done."

to_file.close()
from_file.close() 

执行后会发出错误信息

to_file.close() ArrtibuteError:' str'对象没有属性'关闭'

1 个答案:

答案 0 :(得分:0)

您应该关闭文件描述符而不是文件名。

替换:

to_file.close()
from_file.close() 

使用:

in_file.close()
out_file.close()