TypeError: '_io.TextIOWrapper' object is not subscriptable

前端 未结 1 679
耶瑟儿~
耶瑟儿~ 2021-01-11 21:50

Getting the error as the title says. Here is the traceback. I know lst[x] is causing this problem but not too sure how to solve this one. I\'ve searched google + stackoverfl

相关标签:
1条回答
  • 2021-01-11 22:25

    You can't index (__getitem__) a _io.TextIOWrapper object. What you can do is work with a list of lines. Try this in your code:

    lst = open(input("Input file name: "), "r").readlines()
    

    Also, you aren't closing the file object, this would be better:

    with open(input("Input file name: ", "r") as lst:
        print(medianStrat(lst.readlines()))
    

    with ensures that file get closed, see docs

    0 讨论(0)
提交回复
热议问题