我做了一个简单的程序,但是当我运行它时会显示以下错误: 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()
它显示以下错误消息:
您打开文件为w,代表可写.File “C:/Users/Sachin Patil/fourth,py.py”, line 18, in
for line in file:UnsupportedOperation: not readable
使用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.