我一直在程序的倒数第二行遇到此错误,我不知道为什么,我所做的只是从文本文件中读取一行。
if (items[0]) == 86947367 :
with open("read_it.txt") as text_file:
try:
price = int(text_file.readlines()[2])
except ValueError:
print("error")
else:
new_price = int(price * (items2[0]))
print("£",new_price)
price_list.append(new_price)
product = (text_file.readline()[1])
print(product)
当你使用readlines()
,文件中的“光标”到达末尾。如果你第二次调用它,它就没有什么可读的了。
为了避免这种行为,您可以存储readlines()
在一个变量中用于多种用途,或使用text_file.seek(0)
将光标放回到文件的开头。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)