为什么“开放”功能的输出不允许我分配索引属性?

时间:2018-10-23 15:49:32

标签: python-3.x indexing

我开始学习python3中的编程,并且我正在做一个项目,该项目读取文本文件的内容并告诉您文件中有多少个单词。作为我,我总是想挑战自己,并尝试在输出消息中添加文件名,以便将来我将为其创建GUI等等。

我得到的错误是:AttributeError:'_io.TextIOWrapper'对象没有属性'index'

这是我的代码:

# Open text file

document = open("text2.txt", "r+")

# Reads the text file and splits it into arrays
text_split = document.read().split()

# Count the words
words = len(text_split)

# Display the counted words
document_name = document[document.index("name=")]
output = "In the file {} there are {} words.".format(document_name, words)

print (output)

1 个答案:

答案 0 :(得分:0)

决定接受@Jean-FrançoisFabre的建议,放弃了也输出文件名的想法(立即)。