Search code examples
pythonpython-re

Regular Expressions expected string or bytes like object


Hello I am trying to take emails from a txt file but I cant I dont know why the error is

TypeError: expected string or bytes-like object

import re

pattern_email = re.compile(r"[a-zA-Z0-9]+@[a-zA-Z0-9]+\.com|edu|net")

with open("Yeni Metin Belgesi (4).txt","r",encoding="utf-8") as f:
     content=f.read
     matches = pattern_email.finditer(content)
     for match in matches:
        print(match)

this is the text file


Solution

  • f.read is a method, so you need to do f.read()

    do note that .read() will read the entire file, you may want to iterate it line by line instead