我正在试验一个允许您编辑文本文件的 python 程序,稍后我将在其他程序中使用此功能,但我收到错误:[errno 22] Invealid argument: 'test.txt\r'。我也从未将 \r 添加到 test.txt 中。这是我的代码:
def menu():
print("Type in the full name of the text file you would like to add to.")
file1 = input()
with open(file1, "br") as add:
print("What do you want to write?")
text = input()
add.write(text)
menu()
好吧,新问题。我编辑了代码,现在它说file1 = input().strip()
并且它工作正常,直到我得到另一个错误。错误是:io:UnsupportedOperation: write
。错误说它在第 7 行中说add.write(text)
.
没关系,我将 with 更改为open(file1, "br")
with open(file1, "a")
,现在可以正常工作了。感谢您的所有帮助!