Python错误消息io.UnsupportedOperation:不可读 [英] Python error message io.UnsupportedOperation: not readable
本文介绍了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("\n")
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:
不支持的操作:不可读
推荐答案
您将以"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屋!
查看全文