pythontypeerrortext-parsingfile-reademail-parsing

Python Email Parser not reading from text file


I am trying to run the following code for reading the contents of the mail from a text file by using the parse() function of email.parser.Parser but continuously getting error as "TypeError: parse() missing 1 required positional argument: 'fp'"

from email.parser import Parser

with open('an_email.txt') as fp:
    mail_msg = Parser.parse(fp)

print(mail_msg)

I am unable to find a solution to this. Can please someone help on this one ?


Solution

  • parser is an instance method in the Parser class. That means that you must first create a Parser instance:

    ...
    parser = Parser()
    with open('an_email.txt') as fp:
        mail_msg = parser.parse(fp)