如何在Win32环境下使用Python3获取wave文件属性?
首先我试过这个:如何在Win32环境下使用Python3获取wave文件属性?,python,python-3.x,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
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,文件是空的
上面的所有代码都已注释。有人知道为什么这不起作用吗?
我使用的是Windows7和WingIDE101V5。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)
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`
`回溯(最近一次呼叫最后一次):
文件“C:\Program Files(x86)\Wing IDE 101 5.0\src\debug\tserver\\u sandbox.py”,第11行,在
getWaveInfo中的文件“C:\Program Files(x86)\Wing IDE 101 5.0\src\debug\tserver\\u sandbox.py”,第4行
#打印“正在退出沙盒进程”
builtins.NameError:未定义全局名称“wave”`
如果包含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
什么不起作用?你有什么错误?您使用的是哪个
wave
库?我从我的C:\drive运行此代码,并且wave文件与此代码处于同一层次结构中。这个py文件的。这就是wave.py。什么也没发生!翅膀甚至不眨眼!我在Wing IDE python shell中得到的唯一想法是:“[evaluate wave.py]”尽了最大努力,从文档中获得了所有代码和示例:尝试用print(wave.error)
替换wave.error
,以便您实际看到错误。或者现在就完全删除试一下块。我不明白。。。删除了“try:…except:”块。。没有什么。将printf()函数语句放在try-except的异常中(当然,在删除它之前),也不会发生任何事情。让我试着在提示符下运行它。它是,如果是,而不是。\uuuu-name\uuuuuu-main\uuuuuuu=='\uuuuu-main\uuuuuuuu'
。现在我把代码改为导入wave-def-getWaveInfo():尝试:w=wave.open('audio.wav','rb')')打印(“通道数是:”,w.getnchannels())打印(“以字节为单位的样本宽度是:”,w.getsampwidth())打印(“帧率为:”,w.getframerate())打印(“帧数为:”,w.getnframes())除了:打印(w.error)
我得到以下错误:[evaluate audio.py]回溯(最近一次调用):文件“c:\audio.py”,第1行,在builtins.importorror中:“wave”中的错误幻数:b'\x03\xf3\r\n'我想是这样。只要试着重命名你的python文件,也许它会工作。我正在使用python v3.3.2:d047928ae3f6,2013年5月16日,00:03:43 BTW。感觉像个失败者。。。试图使suych成为一个简单的东西,但它不起作用…好吧,现在你解决了一个问题,你重命名了你的文件,没关系。所以你需要解决这个新错误,也许你可以更新你原来的帖子,或者问新问题,然后发布整个堆栈跟踪?不要气馁,这只是编程,每个人都必须处理bug和错误。
>>> 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