将数据写入python的fifo文件中

问题描述 投票:0回答:1

我已经录制了一个音频文件,并将该文件转换为base64格式。现在,我想将此音频文件写入fifo文件。代码如下:

import os 
import base64
import select
os.system("mkfifo audio1.fifo")
with open("audio1.fifo") as fifo:
     select.select([fifo],[],[fifo])
     with open("out1.wav","rb") as audioFile:
         str = base64.b64encode(audioFile.read())
         fifo.write(str)

但是以上代码仅创建fifo文件,但未在其中写入任何内容。请给我任何建议。

python raspberry-pi raspbian gpio
1个回答
0
投票

使用+模型可以同时支持读写

import base64
import select
os.system("mkfifo audio1.fifo")
with open("audio1.fifo") as fifo:
     select.select([fifo],[],[fifo])
     with open("out1.wav","rb+") as audioFile:
         str = base64.b64encode(audioFile.read())
         fifo.write(str)

同时读取和写入两个不同的文件:

# maker sure the be read file's has some data.
with open("a.file", "w") as fp:
    fp.write("some data")

# now a.file has some data, we create a b.file for write the a.file's data.
with open("b.file", "w") as write_fp, open("a.file", "r") as read_fp:
    write_fp.write(read_fp.read())

# for bytes data
with open("b.file", "wb") as write_fp, open("a.file", "rb") as read_fp:
    write_fp.write(read_fp.read())
© www.soinside.com 2019 - 2024. All rights reserved.