TypeError:尝试打开文件后,预期的str,字节或os.PathLike对象,而不是_io.TextIOWrapper

时间:2020-06-11 18:56:10

标签: python-3.x file

我对Python抱有幻想,并且正在创建一个需要读取.cvs文件中存在的所有值并取其平均值的应用程序。

def medie(file):
    file = open(file, "r")
    riga = file.readline().split(";")
    somma = 0
    i = 1
    medie = list()
    sommaTot = 0
    votiTot = 1
    materie = 0

    while materie < (len(riga)) - 1:
        materie += 1
        voti = 0
        while riga[0] != "":
            riga = file.readline().split(";")
            try:
                try:
                    if riga[i] != "":
                        valore = (riga[i]).split(",")

                        if len(valore) > 1:
                            valore = str(valore[0] + "." + valore[1])
                        somma += float("".join(valore))
                        sommaTot += float("".join(valore))

                        voti += 1
                        votiTot += 1
                        # print("Voto {} in giorno {}".format(riga[i], riga[0]))
                except IndexError:
                    break
            except ValueError:
                pass
        file.close()

        file = open(file, "r")
        riga = file.readline().split(";")

        i += 1
        media = somma / voti
        medie.append(media)
        file.close()
        somma = 0

    mediaTotVoti = sommaTot / votiTot

    i = 0
    votiTot = 0
    tot = 0
    while i < (int(len(medie))) - 1:
        tot += medie[i]
        votiTot += 1
        i += 1
    mediaTotMaterie = tot / votiTot
    return(mediaTotMaterie, mediaTotVoti)

我创建此函数以求平均值,当我尝试直接这段代码起作用时,但是当我使用它创建函数时,它返回此错误:

TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper

我应该如何解决? 谢谢。

PS:我在Windows 10上使用Python 3。

0 个答案:

没有答案