有人能解释一下Python为什么不读取这个文件吗?

2024-04-24 14:51:34 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有下面的代码。基本上,它应该读入testcase.txt文件然后把它们打印出来,但是当我运行程序的时候它是空白的。有什么解决办法吗?你知道吗

testFile = 'testcase.txt'
file = open(testFile, 'r')
index = 0
for line in file:
    test_list.insert(index,line)
    index += 1
file.close()

Tags: 文件代码intest程序txtforindex
1条回答
网友
1楼 · 发布于 2024-04-24 14:51:34

您可能想更多地阅读有关pythonfile objects方法的文档,但这里:

filecontents = open('testcase.txt').readlines() 
#readlines automatically splits the file up by linebreaks

for index,line in enumerate(filecontents):
    print(index,line)

相关问题 更多 >