Python -How to solve OSError: [Errno 22] Invalid argument

Your issue is with backslashing characters like \T :

Try:

f = open(r'C:\\Users\Tanishq\Desktop\python tutorials\test.txt', 'r')

Python uses \ to denote special characters. Therefore, the string you provided does not actually truly represent the correct filepath, since Python will interpret \Tanishq\ differently than the raw string itself. This is we we place the r in front of it. This lets Python know that we do indeed want to use the raw string and to treat \ as a normal character.

Leave a Comment