Python Glossary Русский

all Python 3until 3.13Removed from the standard libraryterm

sndhdr

removed modules

Detected the format of a sound file from its header. Removed in Python 3.13 — read WAV parameters with wave, otherwise check the byte signature.

Syntax

import sndhdr  # до 3.13

sndhdr.what(file) returned SndHeaders: the format ('wav', 'aiff', 'au' and others), frame rate, channels, frame count and sample width. PEP 594 dropped it along with imghdr: the format list was outdated and nobody maintained it. How to port: for WAV open the file with wave.open() and take getparams(), which has the same rate, channels and width; the format is recognised from the first bytes: b'RIFF' plus b'WAVE' at byte 8 for WAV, b'FORM' for AIFF, b'.snd' for AU. filetype, puremagic and python-magic know hundreds of formats; the standard-sndhdr package on PyPI restores the module.

Examples

# Python 3.13+
try:
    import sndhdr
except ModuleNotFoundError as e:
    print(type(e).__name__)  # → ModuleNotFoundError
# Python 3.13+
import wave
with wave.open('a.wav', 'wb') as w:
    w.setparams((1, 2, 8000, 0, 'NONE', 'not compressed'))
    w.writeframes(bytes(200))
head = open('a.wav', 'rb').read(12)
print(head[:4] == b'RIFF' and head[8:12] == b'WAVE')  # → True
with wave.open('a.wav', 'rb') as r:
    print(r.getframerate(), r.getnchannels(), r.getnframes())  # → 8000 1 100
import sndhdr, wave
with wave.open('a.wav', 'wb') as w:
    w.setparams((1, 2, 8000, 0, 'NONE', 'not compressed'))
    w.writeframes(bytes(200))
info = sndhdr.what('a.wav')
print(info.filetype, info.framerate, info.nchannels)  # → wav 8000 1

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy