我正在试验一个允许您编辑文本文件的 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
。该错误表明它位于 add.write(text)
部分的第 7 行。
没关系,我将 with open(file1, "br")
更改为 with open(file1, "a")
并且现在工作正常。感谢您所有的帮助!!!
最佳答案
用途:
file1 = input().strip()
input()
在末尾返回新行,这使得文件名无效。
关于python - 我收到 "[errno 22] Invalid argument: ' test.txt\r',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13223163/