Python 如何在读取文件后再次写入文件(或在读写过程中进行循环)?
我正在写一个程序,允许你写一个文件,然后检查它。我的程序只有在我先添加文本然后再复习的情况下才有效。另一方面,当我读取文件,然后再次尝试写入时 我将在这一行Python 如何在读取文件后再次写入文件(或在读写过程中进行循环)?,python,python-3.x,Python,Python 3.x,我正在写一个程序,允许你写一个文件,然后检查它。我的程序只有在我先添加文本然后再复习的情况下才有效。另一方面,当我读取文件,然后再次尝试写入时 我将在这一行文件中出现此错误。write(addtext+“\n”): io.UnsupportedOperation:不可写 这是我的密码: file = open("notebook.txt","r") file = open("notebook.txt","w") while True: print("(1) Read the notebo
文件中出现此错误。write(addtext+“\n”)
:
io.UnsupportedOperation:不可写
这是我的密码:
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”)
,但它们都不起作用。尝试替换
file = open("notebook.txt", "r")
与
您可能必须先关闭该文件,然后再重新打开它。我认为存在notebook.txt。否则,请在使用此代码之前创建(或修改此代码以管理此文件不存在的情况) 不能同时写入和读取文件。首先,在读和写之后,使用paradigme在使用后自动关闭文件,并避免操作系统错误导致打开的文件过多(例如,如果在
IOError
之后忘记关闭文件)
做:
不要使用文件
作为变量名,因为它是一个内置函数,在Python2中,请参见(关键字也是如此)
我不太清楚你的要求是什么,但我看了看你的代码,我尽了最大努力想看看我能做些什么 因此,首先我导入了os模块,以检查启动时是否存在txt文件。如果有,我就打开看。如果没有,那么打开它进行书写 对于while循环,第一个if语句打开txt文件进行读取,然后文件中的行通过readlines()函数存储在内容中。然后我使用for循环打印内容中的每一行 第二个if语句打开文件进行追加,我使用writelines()函数写入用户的输入 以下是添加到您的代码中的我的代码:
import os
if os.path.isfile("notebook.txt"):
f = open("notebook.txt", "r+")
f.close()
else:
f = open("notebook.txt", "w")
f.close()
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:
f = open("notebook.txt", "r+")
content = f.readlines()
for line in content:
print(line)
f.close()
elif selection == 2:
addtext = input("Write a new note:")
f = open("notebook.txt", "a")
f.writelines(addtext + "\n")
f.close()
elif selection == 3:
f = open("notebook.txt", "w")
with f as filename:
filename.write("")
print("Notebook emptied.")
elif selection == 4:
break
让我知道这是否适用于您以及您是否有任何问题。编辑您的问题并格式化您的代码,使其看起来更干净。我已重写了您的代码。这很好用#/bin/python3 file=open(“notebook.txt”,“w”),而True:print(“1)读取笔记本\n(2)添加注释\n(3)清空笔记本\n(4)退出”)selection=int(输入(“请选择”),如果selection==1:file=open(“notebook.txt”,“r”)content=file.read()打印(content)elif selection==2:addtext=input(“写新便笺:”)file.Write(addtext)file.close()异常值错误:必须正好有一个create/read/Write/append模式谢谢。添加f=open(“notebook.txt”,“a”)可以使我的程序正常工作。也谢谢你告诉我如何删除文本。谢谢。你的解决方案适合我。但是我仍然可以在没有代码的情况下使用
_file = "notebook.txt"
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:
with open(_file, "r") as filename:
content = filename.read()
print(content)
elif selection == 2:
with open(_file, "a") as filename:
note = input("Your note:")
filename.write(note)
elif selection == 3:
with open(_file, "w") as filename:
filename.write('')
elif selection == 4:
break
import os
if os.path.isfile("notebook.txt"):
f = open("notebook.txt", "r+")
f.close()
else:
f = open("notebook.txt", "w")
f.close()
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:
f = open("notebook.txt", "r+")
content = f.readlines()
for line in content:
print(line)
f.close()
elif selection == 2:
addtext = input("Write a new note:")
f = open("notebook.txt", "a")
f.writelines(addtext + "\n")
f.close()
elif selection == 3:
f = open("notebook.txt", "w")
with f as filename:
filename.write("")
print("Notebook emptied.")
elif selection == 4:
break