【发布时间】:2017-06-29 03:51:11
【问题描述】:
我正在编写一个程序,允许您写入文件然后查看它。我的程序只有在我先添加文本然后查看时才有效。另一方面,当我读取文件然后尝试再次写入时
我会在这一行出现这个错误file.write(addtext + "\n"):
io.UnsupportedOperation: not writable
这是我的代码:
file = open("notebook.txt","r")
file = open("notebook.txt","w")
while True:
print("(1) Read the notebook \n(2) Add note \n(3) Empty the notebook \n(4) Quit")
selection = int(input("Please select one:"))
if selection == 1:
file = open("notebook.txt","r")
content = file.read()
print(content)
elif selection == 2:
addtext = input("Write a new note:")
file.write(addtext + "\n")
我曾尝试在读取过程后添加 file.close 或替换这行代码file = open("notebook.txt","r"),但它们都不起作用。
【问题讨论】:
-
编辑您的问题并格式化您的代码,使其看起来更清晰
标签: python python-3.x