|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
def count_words(filename):
try:
with open(filename)as a:
contents=a.read()
except FileNotFoundError:
print("sorry,the file"+filename+"does not exist")
else:
words=contents.splt()
number=len(words)
print("the file"+filename+"has about"+ str(number) +"words")
filenames=['1.py','k.py']
for filename in filenames:
count_words(filename)
Traceback (most recent call last):
File "/Users/lsy/Documents/pythonlsy/k.py", line 13, in <module>
count_words(filename)
File "/Users/lsy/Documents/pythonlsy/k.py", line 8, in count_words
words=contents.splt()
AttributeError: 'str' object has no attribute 'splt'
函数名错了,是 split() 不是 splt() 哈
把 words=contents.splt() 改成 words=contents.split() 即可
|
|