Skip to content Skip to sidebar Skip to footer

How To Read Only Wav Files In A Directory Using Python?

from scipy.io.wavfile import read files = [f for f in os.listdir('.') if os.path.isfile(f)] print files for i in range(0,1): w = read(files[i]) print w I need to read only .wav fi

Solution 1:

Use the glob module and pass to it a pattern (like *.wav or whatever the files are named). It will return a list of files that match the criteria or the pattern.

import glob
from scipy.io.wavfile import read

wavs = []
for filename in glob.glob('*.wav'):
    print(filename)
    wavs.append(read(filename))

Solution 2:

If you don't want to use glob, then something like this will also work:

files = [f for f inos.listdir('.') ifos.path.isfile(f) and f.endswith(".wav")]

Post a Comment for "How To Read Only Wav Files In A Directory Using Python?"