以艰难的方式学习 Python 练习 17 额外问题 [英] Learn Python the Hard Way Exercise 17 Extra Question(S)
问题描述
我正在学习 Zed Shaw 的精彩Learn Python The Hard Way,但一个额外的问题让我难住了:第 9 行--10 可以写成一行,怎么写?我尝试了一些不同的想法,但都无济于事.我可以继续前进,但这样做有什么乐趣?
I'm doing Zed Shaw's fantastic Learn Python The Hard Way, but an extra question has me stumped: Line 9--10 could be written in one line, how? I've tried some different thoughts, but to no avail. I could move on, but what would the fun in that be?
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 还写道,他可以在一行中完成整个脚本.我不太清楚他的意思.
Zed also writes that he could do the whole script in one line. I'm not exactly sure what he means by that.
请随意帮助我:通过给出答案或只是暗示——并且可能包括对问题的折叠或隐藏答案.
Feel free to help me however you want: by giving the answer or merely hinting---and perhaps including a collapsed or hidden answer to the question.
推荐答案
indata = open(from_file).read()
这篇关于以艰难的方式学习 Python 练习 17 额外问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!