你好我是編程初學者。我正在閱讀Zed Shaw的書「學習Python困難之路」,並且遇到了一個我發現很奇怪的錯誤。我想知道爲什麼它不斷給我的錯誤:TypeError:在Windows Powershell中運行我的代碼後,'file'類型的對象沒有len()。我跑的代碼:TypeError:'file'類型的對象沒有len()
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)
in_file = open(from_file, "r")
print "The input file is %d bytes long" % len(in_file)
它說錯誤在最後一行。我不明白爲什麼會出現這個錯誤,因爲如果我是對的,我在讀模式下打開我的文件並將其保存在變量in_file中。那爲什麼它不能讀取in_file的len?在肖捷思銳的書,他在我寫
in_file = open(from_file, "r")
與評論的地方寫
in_file = open(from_file)
indata = in_file.read()
:# we could do these two on one line, how?
所以我認爲他想讓我寫我寫的代碼。
如果有人能幫助我,我將不勝感激。在此先感謝
「所以我想他要我寫我寫的代碼。」 - 不。你似乎誤解了「r」的意思。 – user2357112
在讀取模式下打開文件與讀取file_完全不同。這是打開一本書與意圖閱讀它(而不是強調或強調段落)並實際閱讀它的區別。 –
供參考:http://sopython.com/wiki/LPTHW_Complaints – jonrsharpe