Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of open without a context manager? #139

Open
allComputableThings opened this issue May 6, 2022 · 3 comments
Open

Use of open without a context manager? #139

allComputableThings opened this issue May 6, 2022 · 3 comments

Comments

@allComputableThings
Copy link

While I love context managers, the stack isn't always the the place to perform clean up.
open can be used without async with. How can I use aiofiles.open without it?

import asyncio

import aiofiles

async def test():
    f = aiofiles.open('filename.txt', mode='w')
    try:
        #await f.write('123') #  'AiofilesContextManager' object has no attribute 'write'
        await f.send('123') # TypeError: can't send non-None value to a just-started generator
    finally:
        f.close()
asyncio.run(test())
@DivineSentry
Copy link

seconded. im currently writing code which would benefit from opening aiofiles without a context manager.

@shan-guo
Copy link

shan-guo commented May 25, 2022 via email

@byehack
Copy link

byehack commented Jan 11, 2023

for temporary workaround you can call __aenter__ directly:

import asyncio

import aiofiles

async def test():
    f = await aiofiles.open('filename.txt', mode='w').__aenter__()
    try:
        await f.write('123') #  Now works
    finally:
        f.close()
asyncio.run(test())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants