Python error "TypeError: 'encoding' is an invalid keyword argument for this function"

4.9K    Asked by AnushaAcharya in Python , Asked on Apr 5, 2021

 I have a tine code snippet and it gives me the following error:

TypeError: 'encoding' is an invalid keyword argument for this function

code:

import numpy as np
trump = open('speeches.txt', encoding='utf8').read()
# display the data
print(trump)

Answered by Anusha Acharya

You are getting typeerror: 'encoding' is an invalid keyword argument for this function error because you are using an argument named encoding in the open method and the open method does not have a parameter named encoding.


To resolve this type of error you have to do is use the io module. like this:

import io

    trump = io.open('speeches.txt', encoding='utf8').read()


Your Answer

Interviews

Parent Categories