Error PermissionError: [Errno 13] Permission denied in python. Where I made a mistake?

Mysql problem was solved, but there was another: an error with the files
# Libraries
import requests
from colorama import init, Fore, Back
import json
import sys
import os
import re
from pygame import mixer
import datetime
import time
from gtts gTTS import
import MySQLdb
mp3_alert = "audio/start.mp3"
mixer.init()
conn = MySQLdb.connect(charset='utf8', host="m9083077.beget.tech", user="m9083077_alert", 
 password="mnLYz*hL", db="m9083077_alert")
cursor = conn.cursor()
init()


class YC():
 key = 'de3669c8-04f4-41ed-bf90-86640c6b28a4'
 text = ""
 dictor = ""
 def create(self, file):
 url='https://tts.voicetech.yandex.net/generate?key=' + self.key + '&text=' + self.text + '&format=mp3&lang=EN-us&speaker=' + self.dictor
 r = requests.get(url)
 return r


def title(text):
 print ("======================= "+text+" =======================")

def start():
 title("AS: Information about programme")
 print ("1) Connection to the database is imperative! Failure to comply with them, you may lose data")
 print ("2) operator command will be in version 0.5 Gamma")
 print ("3) Current version: Beta 0.4")
 print ("4) a speech Synthesizer provided by the company 'Yandex'")
 print (" ")
def command(consol):
 if (consol == "Stop" or consol == "stop" or consol == "exit" or consol == "exit" or consol == "Exit"):
exit()
 elif (consol == "Add" or consol == "add" or consol == "+"):
 title("Add notification")
 adding = input("Your message >>> ")
 if(adding != ""):
 cursor.execute("""INSERT INTO alerts(mass) VALUES (%s)""" % adding)
 elif (consol == "Info" or consol == "info" or consol == "inf" or consol == "info" or consol == "info"):
 title("program Information")
 print("the author of the program"+": "+"Daniel Perangin")
 print("program Version"+": "+"Beta v0.3")
 print("the Year of release"+": "+"2017")
 elif (consol == "time" or consol == "Time" or consol == "t:m"):
 now = datetime.datetime.now()
 now_time = datetime.datetime.now()
 print ("the Current date and time:")
 print ("Current year: %d" % now.year)
 print ("Current month: %d"% now.month)
 print ("Current day: %d" % now.day)
 print ("Current hour: %d" % now.hour)
 print ("Current minute: %d" % now.minute)
 print ("Current second: %d" % now.second)
else:
 print("AlertSystem: there is no Such command!")





start()

def main(mp3_alert, conn, cursor):
 def alert():
mixer.music.load(mp3_alert)
mixer.music.play()
 while the mixer.music.get_busy():
time.sleep(0.01)
 # Connect to the database
 cursor.execute("SELECT * FROM alerts WHERE reading = 'no'")
 text = cursor.fetchall()
 now = datetime.datetime.now()
 now_time = datetime.datetime.now()
g=0
 for f in text:
 g = g+1
 #Date for future print(str(now.day)+"-"+str(now.month)+"-"+str(now.year))
 if(f[1]!="" and f[3] <= str(now.hour) and f[4] <= str(now.minute)):
tts=YC()
 tts.dictor = f[6]
 tts.text = f[2]
 b = "audio/text/"+now_time.strftime("%d%m%Y%I%M%S")+".mp3"
 r = tts.create(b)
 musicreading = open(b, "wb")
 with musicreading as code:
code.write(r.content)
musicreading.close()
alert();
 #Administrator information
title("AlertSystem")
 if(now.hour < 10):
 hour = "0" + str(now.hour)
else:
 hour = str(now.hour)
 if(now.minute < 10):
 minute = "0" + str(now.minute)
else:
 minute = str(now.minute)
 print ("Time: "+hour+":"+minute)
 print ("number in the database: "+str(f[0]))
 print ("alert Text: "+f[2])
#Voice
mixer.music.load(b)
mixer.music.play()
 while the mixer.music.get_busy():
time.sleep(0.1)
 query = "UPDATE `alerts` SET reading = 'yes' WHERE `alerts`.id = '"+str(f[0])+"'"
cursor.execute(query)
conn.commit()
# Is done to ensure that when new row to the database, it is immediately displayed
while 0==0:
 main(mp3_alert, conn, cursor)

conn.close()

But after some changes began to occur error:
Traceback (most recent call last):
 File "C:\Users\Даниил\Desktop\45.NEW\CallSystem\CallSystemc — server.py", line 122, in <module>
 main(mp3_alert, conn, cursor)
 File "C:\Users\Даниил\Desktop\45.NEW\CallSystem\CallSystemc — server.py", line 94, in main
 musicreading = open(b, "wb")
PermissionError: [Errno 13] Permission denied: 'audio/text/02102017013122.mp3'</module>
June 14th 19 at 21:05
2 answers
June 14th 19 at 21:07
Solution
Here is the correct code:

# Libraries
import requests
from colorama import init, Fore, Back
import json
import sys
import os
import re
from pygame import mixer
import datetime
import time
from gtts gTTS import
import MySQLdb
mp3_alert = "audio/start.mp3"
mixer.init()
conn = MySQLdb.connect(charset='utf8', host="m9083077.beget.tech", user="m9083077_alert", 
 password="mnLYz*hL", db="m9083077_alert")
cursor = conn.cursor()
init()

class YC():
 key = 'de3669c8-04f4-41ed-bf90-86640c6b28a4'
 text = ""
 dictor = ""
 def create(self, file):
 url='https://tts.voicetech.yandex.net/generate?key=' + self.key + '&text=' + self.text + '&format=mp3&lang=EN-us&speaker=' + self.dictor
 r = requests.get(url)
 return r


def title(text):
 print ("======================= "+text+" =======================")

def start():
 title("AS: Information about programme")
 print ("1) Connection to the database is imperative! Failure to comply with them, you may lose data")
 print ("2) operator command will be in version 0.5 Gamma")
 print ("3) Current version: Beta 0.4")
 print ("4) a speech Synthesizer provided by the company 'Yandex'")
 print (" ")
def command(consol):
 if (consol == "Stop" or consol == "stop" or consol == "exit" or consol == "exit" or consol == "Exit"):
exit()
 elif (consol == "Add" or consol == "add" or consol == "+"):
 title("Add notification")
 adding = input("Your message >>> ")
 if(adding != ""):
 cursor.execute("""INSERT INTO alerts(mass) VALUES (%s)""" % adding)
 elif (consol == "Info" or consol == "info" or consol == "inf" or consol == "info" or consol == "info"):
 title("program Information")
 print("the author of the program"+": "+"Daniel Perangin")
 print("program Version"+": "+"Beta v0.3")
 print("the Year of release"+": "+"2017")
 elif (consol == "time" or consol == "Time" or consol == "t:m"):
 now = datetime.datetime.now()
 now_time = datetime.datetime.now()
 print ("the Current date and time:")
 print ("Current year: %d" % now.year)
 print ("Current month: %d"% now.month)
 print ("Current day: %d" % now.day)
 print ("Current hour: %d" % now.hour)
 print ("Current minute: %d" % now.minute)
 print ("Current second: %d" % now.second)
else:
 print("AlertSystem: there is no Such command!")





start()

def main(mp3_alert, conn, cursor):
 def alert():
mixer.music.load(mp3_alert)
mixer.music.play()
 while the mixer.music.get_busy():
time.sleep(0.01)
 # Connect to the database
 cursor.execute("SELECT * FROM alerts WHERE reading = 'no'")
 text = cursor.fetchall()
 now = datetime.datetime.now()
 now_time = datetime.datetime.now()
g=0
 for f in text:
 g = g+1
 #Date for future print(str(now.day)+"-"+str(now.month)+"-"+str(now.year))
 if(f[1]!="" and f[3] <= str(now.hour) and f[4] <= str(now.minute)):
tts=YC()
 tts.dictor = f[6]
 tts.text = f[2]
 b = "audio/text/"+now_time.strftime("%d%m%Y%I%M%S")+str(f[0])+".mp3"
 r = tts.create(b)
 musicreading = open(b, "wb")
 with musicreading as code:
code.write(r.content)
alert();
 #Administrator information
title("AlertSystem")
 if(now.hour < 10):
 hour = "0" + str(now.hour)
else:
 hour = str(now.hour)
 if(now.minute < 10):
 minute = "0" + str(now.minute)
else:
 minute = str(now.minute)
 print ("Current time: "+hour+":"+minute)
 print ("number in the database: "+str(f[0]))
 print ("alert Text: "+f[2])
#Voice
mixer.music.load(b)
 j = 0
 while j <= f[7]-1:
 j = j + 0.5
mixer.music.play()
 while the mixer.music.get_busy():
time.sleep(1)
 query = "UPDATE `alerts` SET reading = 'yes' WHERE `alerts`.id = '"+str(f[0])+"' LIMIT 1"
cursor.execute(query)
conn.commit()
musicreading.close()
# Is done to ensure that when new row to the database, it is immediately displayed
while 0==0:
 main(mp3_alert, conn, cursor)

conn.close()
June 14th 19 at 21:09
The application has permissions to write to the folder where the created mp3 file?
And the folder actually have? - Roosevelt_Borer commented on June 14th 19 at 21:12
Of course, resolution is also there - Terrell_Graham commented on June 14th 19 at 21:15

Find more questions by tags Python