【发布时间】:2021-02-23 22:28:41
【问题描述】:
我正在尝试编写这个程序,如果用户打开现有文件,他们可以选择读取、重新开始或附加到它,但附加选项不起作用。这是为什么呢?
from sys import argv
file = input("Please open a file: ")
try:
file = open(file, "r+")
choice = input("""
What would you like to do with this file?
A) Read file
B) Delete file and start over
C) Append file
""").lower().rstrip()
if choice in "a":
print(file.read())
elif choice in "b":
print("What would you like to write?")
file.write(input())
elif choice in "c":
file = open(file, "a")
print("What would you like to write?\n")
file.write(input())
except:
print("This is a new file.\n")
file = open(file, "w")
print("What would you like to save in this file?")
file.write(input())```
【问题讨论】:
-
请提供预期的minimal, reproducible example。这篇文章包含很多不相关的代码。
-
我认为有必要显示所有这些,因为我试图让用户选择不附加。最后一个 elif 是 append 选项的位置
-
您的通用异常处理将吞噬任何引发的异常,从而无法进行调试。
标签: python python-3.x file append