我想解決練習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