gpt4 book ai didi

python-3.x - Python 错误消息 io.UnsupportedOperation : not readable

转载 作者:行者123 更新时间:2023-12-03 09:15:17 29 4
gpt4 key购买 nike

我做了一个简单的程序,但是当我运行它时显示以下错误:

line1 = []
line1.append("xyz ")
line1.append("abc")
line1.append("mno")

file = open("File.txt","w")
for i in range(3):
file.write(line1[i])
file.write("\n")

for line in file:
print(line)
file.close()

它显示此错误消息:

File "C:/Users/Sachin Patil/fourth,py.py", line 18, in
for line in file:

UnsupportedOperation: not readable

最佳答案

您正在以 "w" 的身份打开文件,代表可写。
使用 "w"您将无法读取该文件。请改用以下内容:

file = open("File.txt","r")
此外,这里是其他选项:
"r"   Opens a file for reading only.
"r+" Opens a file for both reading and writing.
"rb" Opens a file for reading only in binary format.
"rb+" Opens a file for both reading and writing in binary format.
"w" Opens a file for writing only.
"a" Open for writing. The file is created if it does not exist.
"a+" Open for reading and writing. The file is created if it does not exist.

关于python-3.x - Python 错误消息 io.UnsupportedOperation : not readable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44901806/

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