我正在做 Zed Shaw 的精彩Learn 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 还写道,他可以在一行中完成整个脚本。我不完全确定他的意思。
随心所欲地帮助我:给出答案或仅仅暗示——也许包括对问题的折叠或隐藏的答案。