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