【问题标题】:How to stream files from tarfile for reading?如何从 tarfile 流式传输文件以供阅读?
【发布时间】:2019-12-03 11:17:30
【问题描述】:

我正在尝试从存储桶中的 tar 文件中读取 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

等等。

有什么想法可以实现这一点吗?

【问题讨论】:

    标签: python tensorflow tarfile


    【解决方案1】:

    原来我只是以错误的模式打开文件。使用 r:* 而不是 r|* 有效:

    with tarfile.open(fileobj=f, mode='r:*') as tar:
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-09
      • 2020-06-17
      • 2012-05-03
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多