Error called AttributeError:__enter__

I am practicing with a code, and below is the snippet:

def readfile(data_pt): return open(data_pt, "r")

    for file in files:       
        with readfile(file) as openfile:
            molecules.append(process_file_fn(openfile))

However, I get this error with readfile(file) as openfile: AttributeError: __enter__

I tried to define the mode which is read in both the function and the loop but I am met with the same error. Any insights on how to fix this error? Thank you in advance

Have you overridden readfile or open somewhere else? I cannot reproduce this issue:

>>> with open('a', 'r') as f:
...   pass
...
>>> d = open('a', 'r')
>>> with d as f:
...   pass
...
>>> def readfile(s):
...   return open(s, 'r')
...
>>> with readfile('a') as f:
...   pass
...
>>> with readfile('a') as f:
...   print("hello")
...
hello

oh sorry I posted the wrong first part of the code that was causing the error. This one is the correct part that is probably causing the error:

        tardata = tarfile.open(data, "r")
        files = tardata.getmembers()

        def readfile(data_pt): return tardata.extractfile(data_pt)

basically, I am trying to process a .tar file