wave.error:#个通道未指定

j.d*_*doe 5 python wave

我正在尝试使用wave模块编辑wav文件的长度。但是似乎无法到达任何地方,因为我一直收到相同的错误,即未指定通道数。尽管如此,当我写一些东西来查看通道数时,仍然出现该错误,或​​者当我尝试设置通道数时,如下所示:

def editLength(wavFile):
   file = wave.open(wavFile, 'w')
   file.setnchannels(file.getnchannels())
   x = file.getnchannels()
   print (x)
Run Code Online (Sandbox Code Playgroud)

小智 0

来自https://docs.python.org/3.7/library/wave.html#wave.open

wave.open(file, mode=None)

If file is a string, open the file by that name, otherwise treat it as a file-like
object.

mode can be:
'rb'    Read only mode.
'wb'    Write only mode.

Note that it does not allow read/write WAV files.
Run Code Online (Sandbox Code Playgroud)

您尝试从 WAV 文件中读取和写入,该文件对象在第一次时file.getnchannels()未指定通道数。

def editLength(wavFile):
    with open(wavFile, "rb") as file:
        x = file.getnchannels()
        print(x)
Run Code Online (Sandbox Code Playgroud)

如果要编辑文件,您应该首先从原始文件中读取并写入临时文件。然后将临时文件复制到原始文件上。