简体   繁体   English

Python os和gob:在单个文件与多个文件上执行脚本时,结果不同

[英]Python os and gob: Different results when executing a script on a single file vs. multiple files

I am writing a python shell script that has the option to execute a function (that writes a set of means from a 2D numpy array to a data file) on either a single .wav file or all .wav files in a specified directory. 我正在编写一个Python Shell脚本,该脚本可以在单个.wav文件或指定目录中的所有.wav文件上执行函数(将一组方法从2D numpy数组写入数据文件)。

For example, 例如,

> myscript.py --file=audio.wav --data="data.tab"

should write a floating point value to data.tab. 应该将一个浮点值写入data.tab。

> myscript.py --path="path/with/audio_files" --data="data.tab"

should write a set of floating point values to data.tab. 应该将一组浮点值写入data.tab。

I have a folder with a bunch of WAV files. 我有一个包含一堆WAV文件的文件夹。 When I execute the path on a single file (with the --file option), the result is different than the result when file gets processed with the --path option. 当我在单个文件(使用--file选项)上执行路径时,结果与使用--path选项处理文件时的结果不同。

I have implemented the path file 3 ways: 我已经通过3种方式实现了路径文件:

1) Using os.listdir() 1)使用os.listdir()

for audioFile in os.listdir(options.path):
    if audioFile.endswith('.wav'):
        foo(audioFile)

2) Using os.walk() 2)使用os.walk()

for r, d, f in os.walk(options.path):
for audioFile in f:
    if audioFile.endswith('.wav'):
            foo(audioFile)  

3) Using glob.glob() 3)使用glob.glob()

for audioFile in glob.glob("*.wav"):
    foo(audioFile)

Methods 1 and 2 return the same result. 方法1和2返回相同的结果。 Method 3 returns a different result. 方法3返回一个不同的结果。 All 3 methods return a different result than processing a single file. 这三种方法返回的结果都不同于处理单个文件的结果。

What is going differently when I use the os or gob modules? 使用os或gob模块时会有什么不同?

EDIT: Here is where I process all .wav files in a directory: 编辑:这是我在目录中处理所有.wav文件的地方:

for r, d, f in os.walk(options.path): 
    for audioFile in f:
        if audioFile.endswith('.wav'):  
            # Add MFCC 1-12 to data.
            mfcc12(audioFile, sampleRate, data)

This is in mfcc12(): 这在mfcc12()中:

# mfccs is a 2D numpy array. 
# Each column corresponds to one feature of the audiofile
for i in range(mfccs[0].size):
    mfccMean = mfccs[:, i].mean()
    mfccStdDev = mfccs[:, i].std()  
    data.write(str(mfccMean) + '\t' + str(mfccStdDev) + '\t')

I am using YAAFE to extract the features from the audio files. 我正在使用YAAFE从音频文件中提取功能。

The glob functions do the Unix-style thing of omitting files whose name starts with . glob函数执行Unix样式的操作,忽略名称以开头的文件. :

>>> os.listdir('.')
['conn.c', 'Makefile', 'conn.o', '.depend', 'conn.c.orig', 'conn']
>>> glob.glob('*')
['conn.c', 'Makefile', 'conn.o', 'conn.c.orig', 'conn']

(note, fnmatch.fnmatch does not do this; the code to skip dot-files is in glob.glob1 ). (注意, fnmatch.fnmatch 这样做;跳过点文件的代码是在glob.glob1 )。 Presumably you have some dot-files. 大概您有一些点文件。

I think I solved the problem. 我想我解决了问题。 The problem was not with my mfcc extraction settings, not the Python code. 问题不是我的mfcc提取设置,不是Python代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM