如何在Win32环境下使用Python获取wav文件属性?

时间:2013-12-30 22:06:23

标签: python python-3.x

第一次我试过这个:

import wave

#read wave file I'm trying to open
def getWaveInfo():
    try:
        #get the list of wave file attributes im interested in (channels, famerate, frames number...)
        w = wave.open('wave.wav','rb')
        print("Number of channels is: ",    w.getnchannels())
        print("Sample width in bytes is: ", w.getsampwidth())
        print("Framerate is: ",             w.getframerate())
        print("Number of frames is: ",      w.getnframes())
    except:
        print(wave.error)

仍然一无所获。然后我换成了这个:

import wave

#read wave file I'm trying to open
def getWaveInfo():
    #get the list of wave file attributes im interested in (channels, famerate, frames number...)
    w = wave.open('wave.wav','rb')
    print("Number of channels is: ",    w.getnchannels())
    print("Sample width in bytes is: ", w.getsampwidth())
    print("Framerate is: ",             w.getframerate())
    print("Number of frames is: ",      w.getnframes())

if __main__ == '__main__':
    getWaveInfo()

仍然没有任何反应!我甚至使用了C:\ python wave.py> test.txt和文件是空白的!

上面的所有代码都已经过评论。任何有关为什么这不起作用的见解? 我正在使用Windows 7和Wing IDE 101 V5。 Python 3.3.2

我不知道还有什么可以让它工作......已经在Wings IDE中进行了调试,并且在wave导入之后没有任何其他工作。

现在我的源代码是这样的:

import wave

def getWaveInfo():
    try:
        w = wave.open('audio.wav','rb')
        print("Number of channels is: ",    w.getnchannels())
        print("Sample width in bytes is: ", w.getsampwidth())
        print("Framerate is: ",             w.getframerate())
        print("Number of frames is: ",      w.getnframes())
    except:
        print(w.error)

我终于提出了一个错误(!):

Traceback (most recent call last):
  File "c:\audio.py", line 1, in <module>
builtins.ImportError: bad magic number in 'wave': b'\x03\xf3\r\n'

我的python文件名为"wave.py",但现在我将其更改为"audio.py"。 还将音频波形文件从"wave.wav"更改为"audio.wav"

更改了我的代码,现在就像这样:

from wave import *

def getWaveInfo():
     w = wave.open('audio.wav','rb')
     print("Number of channels is: ",    w.getnchannels())
     print("Sample width in bytes is: ", w.getsampwidth())
     print("Framerate is: ",             w.getframerate())
     print("Number of frames is: ",      w.getnframes())

if __name__ == "__main__":
     getWaveInfo()

现在我有错误:

`Traceback (most recent call last):
  File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 11, in <module>
  File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 4, in getWaveInfo
    #print 'Exiting sandbox process'
builtins.NameError: global name 'wave' is not defined`

1 个答案:

答案 0 :(得分:1)

如果带有python脚本的文件名为wave.py(正如您在评论中所说),就像模块一样,您实际上是在导入自己的文件而不是模块。更改你的python的文件名,看看它是否有效。实施例

>>> import wave
>>> wave.__file__
'/usr/lib/python2.7/wave.py' #correct filename, wave from python library
>>> quit()
pawel@pawel-VPCEH390X:~/stack$ touch wave.py # create wave file
pawel@pawel-VPCEH390X:~/stack$ python
>>> import wave
>>> wave.__file__
'wave.py' #this is wave file that I already created, no python wave module here