1

This is the error i get when i run my code

File "D:\talha\Documents\School\Computer\Programming\Caravan Park TKINTER\Tkinter Caravan.py", line 27, in Add_Customer
    with open(userFile, 'w')as details:
PermissionError: [Errno 13] Permission denied: 'adfaadfadf.txt'

This is the code that causes the error

def Add_Customer():

    nameEntry = firstName.get()
    lastNameEntry = lastName.get()
    EmailEntry = Email.get()
    PhoneEntry = Phone.get()

    userFile = nameEntry+lastNameEntry+'.txt'

    with open(userFile, 'w')as details:

        details.write('First Name: '+nameEntry)
        details.write('Last Name: '+lastNameEntry)
        details.write('Email Address: '+EmailEntry)
        details.write('Phone Number: '+PhoneEntry)

    details.close()

this is the line in which the error occurs

with open(userFile, 'w')as details:
Talha Tutorials
  • 93
  • 1
  • 1
  • 12
  • im quite new so if you dont think you have enough information to help me you can just ask me to edit my question :) – Talha Tutorials Oct 25 '20 at 14:01
  • It seems the file is open in some other application already. Maybe it is open in a text editor yet. – Michael Butscher Oct 25 '20 at 14:02
  • @Michael Butscher i just checked and its not open in another application nor is it open in a text editor. – Talha Tutorials Oct 25 '20 at 14:05
  • Specify the full path to the file you are trying to write to. Your code is writing the file to the current working directory. 1. You can't be sure where that is. 2. It might be a folder you don't have write permissions on. – BoarGules Oct 25 '20 at 15:04
  • ok im going to try that now – Talha Tutorials Oct 25 '20 at 15:13
  • Try to print out the current working directory using `print(os.getcwd())` before opening the file. See whether it is the path you expect. – acw1668 Oct 25 '20 at 16:23
  • You don't have to call `close` when you use `with` for files. That isn't going to solve your problem, it's just true. – OneMadGypsy Oct 25 '20 at 18:28
  • so i made sure i dont write to a different folder somewhere random on my computer, but now im trying to write to the folder my python file is in and it gives me an error for some reason(permission denied) – Talha Tutorials Oct 28 '20 at 18:04

1 Answers1

1

Permission denied is an operating system error and isn't as much related to python.

Check the path you are trying to write to and check if you have permissions there.

You could try saving you code in a different place like the desktop, I ran this code with few modifications to get around the StringVar's and it worked fine. This could be because I have the administrator on my computer. Maybe you could run the code as administrator?

Let me know if this helps.

  • yeah so the problem is that im trying to open a file in the current working directory which is not the folder i wanna save it to, the folder i want to save it to is the one my python file is in and i havent found a way of doing that – Talha Tutorials Oct 28 '20 at 17:55
  • Lets goo!! the problem was that my text editor didnt have administrative privilages so it couldnt make a file in that folder. – Talha Tutorials Oct 28 '20 at 18:07