python - 我收到 "[errno 22] Invalid argument: ' test.txt\r'

标签 python file text error-handling

我正在试验一个允许您编辑文本文件的 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/

相关文章:

python - 通过 pypi 镜像中的 pip 从索引链接安装 pinax

Java:为什么在读取文本文件的情况下\n 被认为是 2 个字符?

python - 在附加模式下,我的文件是否在 RAM 中打开?

unity3d - RectTransform坐标根据TextMesh Pro文字大小

angularjs - Protractor:获取包含多个 I 标签的 anchor 文本

python - 根据字符串列表修改数据框行

python - 使用字典替换文本中的单词

python - 如何通过递归获得数字列表的总和?

file - Grails 文件对本地运行的应用程序可见/部署时丢失?

python - pygame 中的文本格式为段落?