当前位置 : 主页 > 编程语言 > python >

python-3.x – Python错误消息io.UnsupportedOperation:不可读

来源:互联网 收集:自由互联 发布时间:2021-06-25
我做了一个简单的程序,但是当我运行它时会显示以下错误: 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
我做了一个简单的程序,但是当我运行它时会显示以下错误:

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.
网友评论