如何从tarfile流式传输文件以进行读取?

2024-03-28 16:59:12 发布

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

我正在尝试从位于桶中的tarfile读取wav文件。因为有很多文件,我不想先提取这些文件

相反,我希望从tar文件读取数据并将其流式传输到wavfile.read(从scipy.io

with tf.gfile.Open(chunk_fp, mode='rb') as f:
    with tarfile.open(fileobj=f, mode='r|*') as tar:
        for member in ds_text.index.values:
            bytes = BytesIO(tar.extractfile(member))  # Obviously not working
            rate, wav_data = wavfile.read(bytes)
            # Do stuff with data ..

然而,我无法得到一个供wavfile.read工作的蒸汽

尝试不同的事情会导致不同的错误:

 tar.extractfile(member).seek(0)

{AttributeError}'_Stream' object has no attribute 'seekable'

 tar.extractfile(member).raw.read()

{StreamError}seeking backwards is not allowed

等等

有什么办法可以做到这一点吗


Tags: 文件readdatabytesmodeaswithnot