Learn Python the Hard Way-- ex17
这个练习卡了好久了,今天在网上查了查终于跑通了。
能跑通的代码:
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?
in_file = open(from_file)
indata= in_file.read()
print ("The input file is %d bytes ong" % len(indata))
print ("Does the output file exist? %r" %exists (to_file))
print ("Ready, hit RETURN to continue, CTRL-C to abort.")
input()
output= open(to_file, 'w')
output.write(indata)
print("Alright, all done.")
output.close()
in_file.close()
之前不能跑通的代码(根据书上来的):
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 ong" % len(indata))
print ("Does the output file exist? %r" %exists (to_file))
print ("Ready, hit RETURN to continue, CTRL-C to abort.")
input()
output= open(to_file, 'w')
output.write(indata)
print("Alright, all done.")
output.close()
input.close()
报错如下:
自己感觉应该是input作为一个变量(??)和函数冲突了?
刚开始学python,后面再慢慢理解吧~