如何忽略损坏的文件?

How to ignore corrupted files?

提问人:Boris Reif 提问时间:12/29/2022 更新时间:12/29/2022 访问量:333

问:

如何在 Python 中遍历目录并打开好的 wave 文件,同时忽略坏的(损坏的)文件?

我想从目录中打开各种波形文件。但是,其中一些文件可能已损坏,有些文件可能不符合规范。特别是,该目录中将有一些文件,当尝试打开它们时会引发错误:

浪。错误:文件不以 RIFF ID 开头

我想忽略这些文件。我想捕获错误并继续循环。这怎么能做到呢?

我的代码:

for file_path in files:
            sig=0
            file = str(file_path)
            sig, wave_params = DataGenerator.open_wave(file)
            if sig == 0:
                print(
                    'WARNING: Could not open wave file during data creation: ' + file)
                continue
            if wave_params[0] != 1:
                print("WARNING: Wrong NUMBER OF CHANNELS in " + file)
                txt.write(
                    "WARNING: Wrong NUMBER OF CHANNELS in " + file + "\n")
                continue
            if wave_params[1] != 2:
                print("WARNING: Wrong SAMPLE WIDTH in " + file)
                txt.write("WARNING: Wrong SAMPLE WIDTH in " + file + "\n")
                continue
            if wave_params[2] != RATE:
                print("WARNING: Wrong FRAME RATE in " + file)
                txt.write("WARNING: Wrong FRAME RATE in " + file + "\n")
                continue
            if wave_params[3] != SAMPLES:
                print("WARNING: Wrong NUMBER OF SAMPLES in " + file)
                txt.write(
                    "WARNING: Wrong NUMBER OF SAMPLES in " + file + "\n")
                continue
            if wave_params[4] != 'NONE':
                print("WARNING: Wrong comptype: " + file)
                txt.write("WARNING: Wrong comptype: " + file + "\n")
                continue
            if wave_params[5] != 'not compressed':
                print("WARNING: File appears to be compressed " + file)
                txt.write(
                    "WARNING: File appears to be compressed " + file + "\n")
                continue
            if bit_depth != (wave_params[2] * (2**4) * wave_params[1]):
                print("WARNING: Wring bit depth in " + file)
                txt.write("WARNING: Wring bit depth in " + file + "\n")
                continue
            if isinstance(sig, int):
                print("WARNING: No signal in " + file)
                txt.write("WARNING: No signal in " + file + "\n")
                continue

我打开波文件的代码:

    def open_wave(sound_file):
    """
    Open wave file
    Links:
         https://stackoverflow.com/questions/16778878/python-write-a-wav-file-into-numpy-float-array
         https://stackoverflow.com/questions/2060628/reading-wav-files-in-python
    """
    if Path(sound_file).is_file():
        sig = 0
        with wave.open(sound_file, 'rb') as f:
            n_channels = f.getnchannels()
            samp_width = f.getsampwidth()
            frame_rate = f.getframerate()
            num_frames = f.getnframes()
            wav_params = f.getparams()
            snd = f.readframes(num_frames)
        audio_as_np_int16 = np.frombuffer(snd, dtype=np.int16)
        sig = audio_as_np_int16.astype(np.float32)
        return sig, wav_params
    else:
        print('ERROR: File ' + sound_file + ' does not exist. BAD.')
        print("Problem with openng wave file")
        exit(1)

正确缩放波形文件输出的缺失行是故意完成的。

我对如何捕获上述错误感兴趣。有关如何防御性地打开 WAVE 文件的提示也会很好。也就是说,我怎样才能简单地忽略抛出错误的波形文件?

Python 错误处理 防御性编程 即兴演奏

评论

2赞 Charles Duffy 12/29/2022
顺便说一句,拥有一个预期值列表并遍历该列表会使您的代码变得简单得多,而不是为每个值设置单独的条件。ifexpected_params = [("NUMBER OF CHANNELS", 1), ("SAMPLE WIDTH", 2), ("FRAME RATE", RATE), ...]

答:

2赞 ti7 12/29/2022 #1

只需将函数包装在一个块中即可try:except

for file_path in files:
    sig=0
    file = str(file_path)
    try:  # attempt to use `open_wave`
        sig, wave_params = DataGenerator.open_wave(file)
    except wave.Error as ex:
        print(f"caught Exception reading '{file}': {repr(ex)}")
        continue  # next file_path
    # opportunity to catch other or more generic Exceptions
    ...  # rest of loop
0赞 Grimmace_23 12/29/2022 #2

您可以使用 try-catch 块。您“尝试”访问文件并捕获潜在异常的地方。在这里,你可以做一个“通行证”