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?
os.path.exists(...)to check if a file exists.