用python将数据写入fifo文件

2024-10-03 23:25:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经录制了一个音频文件,我正在将该文件转换为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文件,而不在其中写入任何内容。请给出任何建议


Tags: 文件代码importosas格式withopen
1条回答
网友
1楼 · 发布于 2024-10-03 23:25:33

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

import base64
import select
os.system("mkfifo audio1.fifo")
with open("audio1.fifo", "wb") 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())

相关问题 更多 >