-1

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)
Ruli
  • 2,592
  • 12
  • 30
  • 40
  • My apologies for the random things at the end. I was forced to put those there. – idontknowwhatimdoing16 Dec 08 '20 at 02:50
  • Please reduce your code to the minimum that still reproduces your problem. A lot of the code above is irrelevant. –  Dec 08 '20 at 03:04
  • Without something like `psutil` or something similar to track active processes it would be quite difficult, however you can attempt to kill the task in the following way `os.system("TASKKILL /F /IM app_name.exe")` It goes without saying that this is not a very safe approach, and I don't recommend it! – TheLazyScripter Dec 08 '20 at 03:11
  • 2
    Does this answer your question? [How do I close a file opened using os.startfile(), Python 3.6](https://stackoverflow.com/questions/57909525/how-do-i-close-a-file-opened-using-os-startfile-python-3-6) – Ruli Dec 08 '20 at 07:31

1 Answers1

-2

Please enter the file path on "os.startfile()" function. It takes the file path, not the name.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • This does not provide an answer to the question above and is even incorrect, if file name is provided working directory is used to look for it. However, this was most likely meant to be comment, you will be able to post comments once you gain sufficient reputation (50), until then, please avoid posting answers which do not attempt to solve the original problem. – Ruli Dec 08 '20 at 07:29