When I test my code:
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 + '/'
I receive error :
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
So, I just font understand, where the error is.
Thank you!
You just want
for line in myfile:.fileobjects can be iterated over directly (yielding 1 line at a time). However, file objects don’t support calling (e.g.myfile()isn’t implemented becausefile.__call__isn’t implemented).