我正在执行Zed Shaw的精彩学习Python The Hard Way,但我遇到了一个额外的问题:9– 10行可以写成一行,怎么写?我尝试了一些不同的想法,但无济于事。我可以继续前进,但是那会带来什么乐趣呢?
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."
Zed还写道,他可以在一行中完成整个脚本。我不确定他的意思。
随时随地为我提供帮助:给出答案或仅作提示-甚至可能包含对该问题的模糊或隐藏答案。
indata = open(from_file).read()