Python 异步操作文件 aiofiles
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Python 异步操作文件 aiofiles
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            # 異步文件操作
# pip install aiofiles# 基本用法
import asyncio
import aiofilesasync def wirte_demo():# 異步方式執(zhí)行with操作,修改為 async withasync with aiofiles.open("text.txt","w",encoding="utf-8") as fp:await fp.write("hello world ")print("數(shù)據(jù)寫入成功")async def read_demo():async with aiofiles.open("text.txt","r",encoding="utf-8") as fp:content = await fp.read()print(content)async def read2_demo():async with aiofiles.open("text.txt","r",encoding="utf-8") as fp:# 讀取每行async for line in fp:print(line)
if __name__ == "__main__":asyncio.run(wirte_demo())asyncio.run(read_demo())asyncio.run(read2_demo())
                            
                        
                        
                        總結(jié)
以上是生活随笔為你收集整理的Python 异步操作文件 aiofiles的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: webpack4 入门配置研究
 - 下一篇: 【模板】并查集