gpt4 book ai didi

python - 艰难地学习 Python 练习 17 个额外问题)

转载 作者:IT老高 更新时间:2023-10-28 20:38:25 25 4
gpt4 key购买 nike

我正在做 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 还写道,他可以在一行中完成整个脚本。我不太清楚他的意思。

随意帮助我:给出答案或只是暗示——也许包括对问题的折叠或隐藏答案。

最佳答案

indata = open(from_file).read()

关于python - 艰难地学习 Python 练习 17 个额外问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3561279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com