我想解决练习15的额外信用问题Zed Shaw的学习Python困难的方式,但我遇到了一个问题。代码如下:学习Python困难的方法,练习15
from sys import argv
script, filename = argv
txt = open(filename)
print "Here's your file %r:" % filename
print txt.read()
print "I'll also ask you to type it again:"
file_again = raw_input("> ")
txt_again = open(file_again)
print txt_again.read()
print txt_again.read()
据我所知,已经使用的所有代码,但额外的信用问题7:
Startup
python
again and useopen
from the prompt. Notice how you can open files and runread
on them right there?
我试着输入了所有我能在终端想到的(在Mac上)首次启动python与'python'命令后,但我不能让代码运行。我该怎么做才能让这段代码从提示符运行?
什么是“我能想到的一切”?你得到什么错误? –
我刚开始用'open(xyz.txt)'或'xyz.txt.read()',我会得到_Traceback(最近调用最后一次): 文件“”,第1行,在 NameError:name 'ex15_sample'没有被定义。或者我会从'从sys导入argv'开始导入argv模块,就像我在脚本中那样。由于我是初学者,我仍然不太了解如何从提示符运行代码与从脚本运行代码不同。 –
我现在正在做这个例子,但是当它读取我的文本文件时,我只能得到最后一行。有什么建议么?谢谢 – Drewdin