你好我是编程初学者。我正在阅读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