我一直在尝试定义一个获取 2 个输入(一个字符和一个文件路径)并返回字符出现在其中的次数的函数。我知道有一种更短的方法可以做到这一点,method.count
但我想知道为什么这个函数总是返回 0。
def lettercount(character , path):
with open("bear.txt") as myfile:
counter = [obj for obj in myfile if obj==character]
return len(counter)