Simple Problem statement. I want to read files asynchronously . My problem is when I try to read a file using aiofiles.open it just errors out with the cryptic messsage
AttributeError: __enter_
The crux of the problem can be demonstrated with the following sample
with open("/tmp/test/abc_20211105.txt","w") as f:
f.write("this is a sample!")
with aiofiles.open('/tmp/test/abc_20211105.txt','r') as f: # This is where the error occurs
f.read()
the file gets created but I can't read the same file using aiofiles.
I have tried specifying encoding etc, but nothing helps.
What does this error mean?
The
aiofiles.opencontext manager is meant to be used asynchronously (async with), in a coroutine. Standard synchronous context managers rely on__enter__and__exit__methods, whileasynccontext managers use methods named__aenter__and__aexit__, thus,async withis necessary to call the__aenter__and__aexit__methods ofaiofiles.open, instead of__enter__and__exit__(which are not defined foraiofiles.open):