操作系统.系统在python中多次打开同一个应用程序



这个程序基本上是一个硬编码的个人助理,我用它来做基本的事情。我告诉程序"打开火狐",它会完美地打开它,当我再给一个命令,比如"打开Sublime",它仍然会打开火狐。为什么呢?我不希望它在我没有要求的情况下打开其他应用程序。发生这种情况是因为我正在运行应用程序,但我没有通过脚本关闭它,就像我按"X"并关闭窗口一样。真的被这个卡住了!

这是我的代码…

import speech_recognition as sr
import pyttsx3
import json
import random
import os
import subprocess
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
def text2speech(audio):
engine.setProperty('rate', 180) # Sets speed percent can be more than 100
engine.say(audio)
engine.runAndWait()
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
print('Listening...')
r.pause_threshold = 1
audio = r.listen(source)
try:
print('Recognizing...')
query = r.recognize_google(audio, language = 'en-in')
print('You: ', query)
except Exception as e:
print(e)
print('Unable to recognize your voice.')
return 'None'
return query
def rubysays(key): # key is the tag or key element in the data.json which contains the responses
length = len(data[key])
# print(length)
say_this = data[key][random.randint(0, length - 1)]
return say_this
def startruby():
query = takeCommand().lower()
if 'open firefox' or 'pen firefox' or 'firefox' or 'fire fox' in query: # opens Visual Studio Code
say_this = 'Opening Firefox Browser'
print(say_this)
text2speech(say_this)
# os.startfile(r'C:Program FilesMozilla Firefoxfirefox.exe')
os.system(r'C:Program FilesMozilla Firefoxfirefox.exe')

elif 'open code' or 'pen code' in query: # opens Visual Studio Code
say_this = 'Opening Visual Studio Code'
print(say_this)
text2speech(say_this)
# os.startfile(r'C:Program FilesMicrosoft VS CodeCode.exe')
os.system(r'C:Program FilesMicrosoft VS CodeCode.exe')

elif 'open sublime' or 'pen sublime' or 'open sub lime' or 'pen sub lime'in query: # opens Sublime Text
say_this = 'Opening Sublime Text'
print(say_this)
text2speech(say_this)
# os.startfile(r'C:Program FilesSublime Text 3sublime_text.exe')
os.system(r'C:Program FilesSublime Text 3sublime_text.exe')
elif 'open cmd' or 'c m d' in query: # opens command prompt
say_this = 'Opening Windows Terminal'
print(say_this)
text2speech(say_this)
# os.startfile(r'C:UsersJasonPCAppDataLocalMicrosoftWindowsAppswt.exe')
os.system(r'C:UsersJasonPCAppDataLocalMicrosoftWindowsAppswt.exe')
elif 'open discord' or 'discord' in query: # opens Discord
say_this = 'Opening Discord'
print(say_this)
text2speech(say_this)
# os.startfile(r'C:UsersJasonPCAppDataLocalDiscordUpdate.exe')
os.system(r'C:UsersJasonPCAppDataLocalDiscordUpdate.exe')

elif 'open steam' or 'steam' in query: # opens Steam
say_this = 'Opening Steam'
print(say_this)
text2speech(say_this)
# os.startfile(r'C:Program Files (x86)Steamsteam.exe')
os.system(r'C:Program Files (x86)Steamsteam.exe')

elif 'open calculator' or 'calculator' in query: # opens Calculator
say_this = 'Opening Calculator'
print(say_this)
text2speech(say_this)
# os.startfile(r'C:WindowsSystem32calc.exe')
os.system(r'C:WindowsSystem32calc.exe')

elif 'open discord' or 'discord' in query: # opens Camera
say_this = 'Opening Camera'
print(say_this)
text2speech(say_this)
os.system('start microsoft.windows.camera:')

elif 'whatsapp' or 'whats app' or 'whats up' or "what's app" or "what's up" in query: # opens WhatsApp
say_this = 'Opening WhatsApp'
print(say_this)
text2speech(say_this)
# os.startfile(r'C:UsersJasonPCAppDataLocalWhatsAppWhatsApp.exe')
os.system(r'C:UsersJasonPCAppDataLocalWhatsAppWhatsApp.exe')

if __name__ == '__main__':
# use the json
f = open('data.json')  
data = json.load(f)
text2speech('Hey this is Ruby!')
while True:
query = takeCommand().lower()
if 'ruby' in query or 'rubi' in query:
say_this = rubysays('greeting')
print('You:', say_this)
text2speech(say_this)
startruby()

if 'bye' in query or 'goodbye' in query:
say_this = rubysays('bye')
print(say_this)
text2speech(say_this)
exit()

参见如何针对单个值测试多个变量?

:if 'open firefox' or 'pen firefox' or 'firefox' or 'fire fox' in query:当你在测试一个字符串时,总是为真,而不是如果字符串在查询中。

例如:

In [1]: query = "xxx"
In [2]: bool('open firefox' or 'pen firefox' or 'firefox' or 'fire fox' in query)
Out[2]: True
In [3]: bool('fire fox' in query)
Out[3]: False
In [4]: bool('open firefox')
Out[4]: True

相关内容

最新更新