Python 类型错误:'_io.TextIOWrapper';对象不可调用

Python 类型错误:'_io.TextIOWrapper';对象不可调用,python,python-3.x,Python,Python 3.x,当我测试代码时: def read_classification_from_file(path, name): path = add_slash(path) + name myfile = open(path, "r") mydict = {} for line in myfile(): x = line.split(" ") x[1]=x[1].replace("\n

当我测试代码时:

def read_classification_from_file(path, name):
        path = add_slash(path) + name
        myfile = open(path, "r")
        mydict = {}
        for line in myfile():
                x = line.split(" ")
                x[1]=x[1].replace("\n","")
                mydict[x[0]]=x[1]
        return mydict
def add_slash(path):
        if path.endswith('/'): return path
        return path + '/'
我收到错误消息:

   Traceback (most recent call last):
    File "spamfilter/solution/test_quality_for_corpus.py", line 59, in test_allPredictionsHam
    q = self.compute_quality_for_corpus(CORPUS_DIR)
    File "/local/ulohy/env/data/4893_1/quality.py", line 9, in compute_quality_for_corpus
    truth_dic = utils.read_classification_from_file(corpus_dir, "!truth.txt")
    File "/local/ulohy/env/data/4893_1/utils.py", line 5, in read_classification_from_file
    for line in myfile():
    TypeError: '_io.TextIOWrapper' object is not callable
所以,我只知道错误在哪里


谢谢大家!

您只需要对myfile中的行执行
<代码>文件对象可以直接迭代(一次生成一行)。但是,文件对象不支持调用(例如,
myfile()
没有实现,因为
file.\uuu-call\uuu
没有实现)。

“错误在哪里”:它显示在回溯-
中,用于myfile()中的行:
这不是我的错误,而是UnitTest。所以,对不起。请删除主题您可以自己删除,但单击删除链接对不起,这是错误的。有答案,不能删除。