Charlie Kimzey
Charlie Kimzey

Reputation: 125

I'm getting "[errno 22] Invalid argument: 'test.txt\r'

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!!!

Upvotes: 2

Views: 1740

Answers (2)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250951

use input().strip() to remove the trailing new-lines from your input, it might have came due to running your program in terminal or cmd.

Upvotes: 1

kichik
kichik

Reputation: 34704

Use:

file1 = input().strip()

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

Upvotes: 4

Related Questions