0

I was creating a note taking program, using the user voice command. When the file already exists, I want to show an error. It runs smoothly when the file name is different though. When the file already exists, I have coded to open in the writing mode. It would erase the earlier file, and write new instead of adding in the append mode.

elif 'note' in query:
            try:
                speak(" Okay master. what's the file name")
                b= takecommand()
                f = open(f"{b}.txt","w")
                speak("Okay master. tell me what to note down")
                a= takecommand()
                f.write(f"{a}\n")
                f.close()
            except Exception as e:
                speak("file name already exist")

Can you help me with troubleshooting the script? Like how could I first make it thrown an error when the filename is same?

4
  • When you say "file name is the same", do you mean "if the file already exists"? You can use os.path.exists(...) to check if a file exists. Commented Nov 5, 2022 at 3:47
  • The "file name" means the name of the new note file I am going to take. Commented Nov 5, 2022 at 3:48
  • 1
    I know what the file name is. I was just trying to guide you to ask your question properly. What you want to is prevent the user from overwriting a file that already exists, right? That's what I advised you. Commented Nov 5, 2022 at 3:52
  • Yes, @TimRoberts Commented Nov 5, 2022 at 3:54

2 Answers 2

2

You'd want to check if the file exists after they input it, which can be done using "os.path.exists" as also suggested by Tim Roberts. Here's code that should work for you:

elif 'note' in query:
        try:
            Looper = True # Added a loop so it can repeatedly ask the user until they give a valid answer.
            speak(" Okay master. what's the file name") # Put here instead of in the loop so it will repeat once since the else condition already asks for a new file name aswell.
            while Looper == True:
                b = takecommand()
                if os.path.exists(f"{b}.txt"):
                    f = open(f"{b}.txt","w")
                    speak("Okay master. tell me what to note down")
                    a= takecommand()
                    f.write(f"{a}\n")
                    Looper = False # So the loop won't repeat again (regardless of the below line).
                    f.close()
                else:
                    speak("That file already exists. Give me a new file name.")
        except Exception as e:
            speak("Hm, an error occured. Try again later.")

I also added in a while loop for you, so that way if the user gives a file name that already exists, it will keep asking until they give a valid file name.

Ensure you have OS imported for the code to work by adding the following to the top of your code:

import os
Sign up to request clarification or add additional context in comments.

Comments

1

Use mode 'x'

x' create a new file and open it for writing. The 'x' mode implies 'w' and raises an FileExistsError if the file already exists.

try:
    # ...
    f = open(f"{b}.txt","x")
    # ...
except FileExistsError:
    speak("file name already exist")

Or mode 'a' to append the strings to existing file.

'a' open for writing, appending to the end of the file if it exists

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.