Ad

I'm Getting "[errno 22] Invalid Argument: 'test.txt\r'

- 1 answer

I'm experimenting with a python program that will allow you to edit text files, I will use this feature later on in other programs, but I get the error: [errno 22] Invealid argument: 'test.txt\r'. Also I never added \r to test.txt. Here is my code:

 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()

Okay, new problem. I edited the code so that now it says file1 = input().strip() and it worked fine ALL up until I got another error. The error is: io:UnsupportedOperation: write. The error says it's in line 7 in the part that says add.write(text).

Nevermind, I changed the with open(file1, "br") to with open(file1, "a") and it works fine now. THANKS FOR ALL OF YOUR HELP GUYS!!!

Ad

Answer

Use:

file1 = input().strip()

input() returns the new line at the end and that makes the filename invalid.

Ad
source: stackoverflow.com
Ad