如何使用Python读取目录中的wav文件? [英] How to read only wav files in a directory using Python?
本文介绍了如何使用Python读取目录中的wav文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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
我只需要从python工作目录中读取.wav文件.并将每个.wav文件存储为一个numpy数组.这是我的代码.但是在这段代码中,所有文件都被读取.我只读取目录中的wav文件吗?怎么可能?
I need to read only .wav files from python working directory. And store the each .wav files as a numpy array. This is my code. But in this code all files are read. I only read the wav files in the directory? How it possible?
推荐答案
使用glob
模块并向其传递一个模式(例如*.wav
或任何命名的文件).它将返回符合条件或模式的文件列表.
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))
这篇关于如何使用Python读取目录中的wav文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文