from sys import argv
from os.path import exists
script, from_file, to_file = argv
print "We will copy %s to %s." % (from_file, to_file)
indata = open(from_file).read()
print "The input file is %d bytes long." % len(indata)
print "Does the output file exist? %r" % exists(to_file)
print "Ready, hit enter to continue, CTRL-C to abort."
raw_input()
txt = open(to_file, 'w').write(indata)
print txt.read()
一直显示
AttributeError:'Nonetype'object has no attribute 'read'.
txt = open(to_file, 'w').write(indata)
这段代码执行完后,txt并不是file对象,而是write(indata)函数所返回的对象,根据python文档
此函数返回None,所以会产生上述错误。
你应该open再次打开文件来read读取。