Basically, I'm trying to program my ai assistant M.A.R.T.I.N. to open and close files. I've been opening files with os.startfile but I can't seem to find a way to close them. Can someone help me? Here's my code for reference.
import pyttsx3
import datetime
import speech_recognition as sr
import webbrowser
import os
engine = pyttsx3.init()
def speak(audio):
engine.say(audio)
engine.runAndWait()
def time():
Time = datetime.datetime.now().strftime("%I: %M: %S")
speak(Time)
def date():
year = int(datetime.datetime.now().year)
month = int(datetime.datetime.now().month)
day = int(datetime.datetime.now().day)
speak(day)
speak(month)
speak(year)
def wishme():
speak("Welcome back sir!")
speak("How may I help you?")
def takeCommand(ask):
if ask:
print(ask)
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
voice_data = ' '
try:
print("Recognizing...")
voice_data = r.recognize_google(audio)
print(voice_data)
except Exception as e:
print(e)
speak("Say that again please sir...")
return "none"
return voice_data
def respond(voice_data):
if 'what is your name' in voice_data:
speak("My name is MARTIN")
if 'search' in voice_data:
search = takeCommand(ask=speak('what do you want to search for?'))
url = 'https://google.com/search?q=' + search
webbrowser.get().open(url)
if 'find location' in voice_data:
location = takeCommand(ask=speak('what place do you want to search for?'))
url = 'https://google.nl/maps/place/' + location
webbrowser.get().open(url)
if 'thank you' in voice_data:
speak("You're welcome sir")
if 'Martin not now' in voice_data:
speak(" ")
if 'exit' in voice_data:
exit()
if 'good morning Martin' in voice_data:
speak('good morning sir')
if 'good afternoon Martin' in voice_data:
speak('good afternoon sir')
if 'good evening Martin' in voice_data:
speak('good evening sir')
if 'good night Martin' in voice_data:
speak('good night sir')
if 'open' in voice_data:
requestedapp = takeCommand(ask=speak('what do you want to open?'))
os.startfile(requestedapp)
if 'close' in voice_data:
requestedapp = takeCommand(ask= speak('what do you want to close?'))
wishme()
while True:
voice_data = takeCommand(ask=True)
respond(voice_data)