微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 python 中在运行时添加音频文件后无法读取音频文件

如何解决在 python 中在运行时添加音频文件后无法读取音频文件

import tkinter as tk
from tkinter import ttk
from tkinter import filedialog as fd
from tkinter.messageBox import showinfo
import tkinter as tk
import speech_recognition as sr
import nltk
# create the root window
root = tk.Tk()
root.title('Tkinter File Dialog')
root.resizable(False,False)
root.geometry('300x150')


def select_files():
   filetypes = (
    ('audio files','*.wav'),)

  file = fd.askopenfilenames(
    title='Open file',initialdir='/',filetypes=filetypes)

   showinfo(
    title='Selected File',message=file
   )
 r = sr.Recognizer()
 if file is not None:
    tk.messageBox.showinfo("result","file added successfully")
  with file as source:
    audiof = r.record(source,duration=20)
    resultf = r.recognize_google(audiof)
  contentf = file.read()
  with open("runtimefile.txt","w") as text_file:
    text_file.write("%s" % resultf)
  with open("runtimefile.txt","r") as file:
    file_content = file.read()
  runtokens = nltk.word_tokenize(file_content)
  print("tokens for runtime file",runtokens)
  text_file.close()


   # open button
  open_button = ttk.Button(
    root,text='Open Files',command=select_files
     )

  open_button.pack(expand=True)

  root.mainloop()
   Exception in Tkinter callback
  Traceback (most recent call last):
File "C:\Users\Tani\Anaconda3\envs\pythonProject\lib\tkinter\__init__.py",line 1892,in __call__
return self.func(*args)
File "C:/Users/Tani/PycharmProjects/pythonProject/microphone.py",line 33,in 
 select_files
 with file as source:
 AttributeError: __enter__

我试图在 python 中在运行时将音频文件作为输入,然后通过将其转换为文本文件标记化作为输入的文件内容来读取该文件,但系统给了我类似上面提到的错误

>

请建议我解决这个问题

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。