【发布时间】:2016-12-09 02:47:10
【问题描述】:
我正在尝试构建一个程序,该程序将在 I/O 文件中记录学生的姓名和年级。问题是当用户在删除函数中从文件中删除名称时。谢谢你看:)
(x是用户给的名字)
def remove(x):
with open ("grades.txt", "rt") as f:
text = f.read()
if x in text:
new = del text[x]
with open ("grades.txt", "wt") as f:
text = f.write(new)
else:
print("name not in the file")
【问题讨论】:
-
你的问题到底是什么?
-
您如何存储以及要删除什么?具体一点。
-
@MohammadYusufGhazi 不要编辑帖子中的空白,尤其是关于 Python 的帖子。空格在 Python 中在语法上很重要,如果问题出在 WS 本身,您最终会屏蔽它。
-
@MohammadYusufGhazi 为什么要在编辑中从代码中删除错误? 不要!
-
@S.Grym 请edit您的问题并输入minimal reproducible example。还请附上任何错误或追溯的全文。