Python 错误消息 io.UnsupportedOperation:不可读 [英] Python error message io.UnsupportedOperation: not readable

查看:31
本文介绍了Python 错误消息 io.UnsupportedOperation:不可读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I made a simple program but It shows the following error when I run it:

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("
")

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

它显示此错误消息:

文件C:/Users/Sachin Patil/fourth,py.py",第 18 行,
对于文件中的行:

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

UnsupportedOperation:不可读

UnsupportedOperation: not readable

推荐答案

您正在打开文件为 "w",代表可写.

You are opening the file as "w", which stands for writable.

使用 "w" 您将无法读取文件.请改用以下内容:

Using "w" you won't be able to read the file. Use the following instead:

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 错误消息 io.UnsupportedOperation:不可读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆