gpt4 book ai didi

python - IO错误: File not open for reading python

转载 作者:太空宇宙 更新时间:2023-11-03 18:45:16 24 4
gpt4 key购买 nike

这就是我到目前为止所拥有的,我尝试的所有内容都一直表明文件未打开?有什么基本方法可以解决这个问题吗?

liNames = []
while 1 == 1:
liNames += [raw_input("Tell me a name. Type DONE(all caps) if done entering:")]
if "DONE" in liNames:
break

del liNames[-1]

print liNames

name_file = open("names.txt","w")
line = name_file.writelines(liNames)
for line in name_file:
print line
name_file.close()

最佳答案

要具体说明评论的建议,请在这行之后:

line = name_file.writelines(liNames)

插入这些新行:

name_file.close()
name_file = open("names.txt", "r") # or plain open("names.txt")

有了更多的经验,您可以将其写为:

with open("names.txt","w") as name_file:
name_file.writelines(liNames)
with open("names.txt") as name_file:
for line in name_file:
print line

有了更多的经验;-),您将学习如何打开文件进行读取和写入。但这比较棘手,对于 Windows 上的文本文件来说尤其棘手。

关于python - IO错误: File not open for reading python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19671704/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com