我不断收到TypeError:预期的str,字节或os.PathLike对象,而不是_io.TextIOWrapper

时间:2020-04-16 02:48:08

标签: python

我正在做一个项目,在这个项目中,我必须让用户创建文件,附加一些数字,然后找到这些数字的平均值。但是,当我这样做时,我总是收到以下错误。

TypeError:预期的str,字节或os.PathLike对象,而不是_io.TextIOWrapper

这是我的代码。

def main():
    file_name = input('Create file name: ')

    file_name = open(file_name, "w")
    file_name.close()
    cont = 'y'
    counter = 0 

    while cont == 'Y' or 'y':
        num = float(input('Enter a number to write to the file: '))
        file_name = open(file_name, "a")
        file_name.write(str(num) + '\n')
        file.name.close()
        cont = input('Would you like to write more numbers to the file?(Enter y to continue anything else to stop): ')


main()

0 个答案:

没有答案