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

aiofile writing binary file .wav on windows issue #22

Open
wood-j opened this issue Aug 16, 2019 · 6 comments
Open

aiofile writing binary file .wav on windows issue #22

wood-j opened this issue Aug 16, 2019 · 6 comments

Comments

@wood-j
Copy link

wood-j commented Aug 16, 2019

Hello, I am using it to writing an wav file from iternet async with aiohttp. It works fine on linux, but downloaded audio sounds weird on windows.

    async def download_audio(self, task_line: TaskLine):
        url = f'{self.base_url}{task_line.raw_data_file_path}'
        logger.debug(url)
        async with aiohttp.ClientSession(cookie_jar=self._cookie_jar) as session:
            async with session.get(url=url) as resp:
                assert resp.status == 200, f'request error status code: {resp.status}'
                path = task_line.audio_file_path()
                dir, _ = os.path.split(path)
                if not os.path.exists(dir):
                    os.mkdir(dir)
                async with AIOFile(path, 'wb') as f:
                    await f.write(await resp.read())

When I change write to file code with internal open method, the audio file download turn out be fine.

    async def download_audio(self, task_line: TaskLine):
        url = f'{self.base_url}{task_line.raw_data_file_path}'
        logger.debug(url)
        async with aiohttp.ClientSession(cookie_jar=self._cookie_jar) as session:
            async with session.get(url=url) as resp:
                assert resp.status == 200, f'request error status code: {resp.status}'
                path = task_line.audio_file_path()
                dir, _ = os.path.split(path)
                if not os.path.exists(dir):
                    os.mkdir(dir)
                # async with AIOFile(path, 'wb') as f:
                #     await f.write(await resp.read())
                with open(path, 'wb') as f:
                    f.write(await resp.read())

Downloaded audio file info:
AIOFile:
AIOFile
open:
open

By the way, I am using manjaro18 linux system and windows10 ltsb2016 and both python3 latest version 3.7.3

aiofile        1.5.2
aiohttp        3.5.4
@wood-j wood-j changed the title writing binary file on windows aiofile writing binary file .wav on windows issue Aug 16, 2019
@wood-j
Copy link
Author

wood-j commented Aug 16, 2019

Here is a short test code:

import os
import aiohttp
import asyncio
from aiofile import AIOFile


async def download1(url: str, path: str):
    async with aiohttp.ClientSession() as session:
        async with session.get(url=url) as resp:
            assert resp.status == 200, f'request error status code: {resp.status}'
            async with AIOFile(path, 'wb') as f:
                await f.write(await resp.read())


async def download2(url: str, path: str):
    async with aiohttp.ClientSession() as session:
        async with session.get(url=url) as resp:
            assert resp.status == 200, f'request error status code: {resp.status}'
            with open(path, 'wb') as f:
                f.write(await resp.read())


if __name__ == '__main__':
    url = 'http://light.ucme.top/files/35eaecb0-60ba-11e9-8d8f-8cec4b573ab2.tts.wav'
    asyncio.run(download1(url, '1.wav'))
    asyncio.run(download2(url, '2.wav'))

@lemurchik
Copy link

There is a general problem with binary files write on Windows

@lemurchik
Copy link

#25

@dimaqq
Copy link

dimaqq commented Sep 1, 2022

Now that #19 is done via #25, perhaps this issue too is resolved?
@wood-j would you care to test, please 🙏🏼

@wood-j
Copy link
Author

wood-j commented Sep 27, 2022

Now that #19 is done via #25, perhaps this issue too is resolved? @wood-j would you care to test, please 🙏🏼

Tested ok on my windows env, thanks for the fix @dimaqq

@dimaqq
Copy link

dimaqq commented Sep 27, 2022

It was not my fix, but I'm happy to help even by pointing things out :)

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

3 participants