1

I have been trying to overwrite a file as i do not need it in my folder as it makes a duplicate. I have used different Syntax such as os.rename but it does not work with my code. So I then used os.replace but now i get a message saying 'PermissionError: [WinError 5] Access is denied'. If anyone can help, please help as soon as possible.

This is the code that I've been trying to fix:

if row[0]==user:
           with open('temp','a')as newone:
           theaverage = (int(counter) + int(row[1]) + int(row[2]))/3
           high = max(int(counter), int(row[1]),int(row[2]))
           newone.write("%s,%s,%s,%s,%s,%s\n"%(user,counter,row[1],row[2],theaverage,high))
           newone.close()
  else:
      with open('temp','a')as newone:
      newone.write("%s,%s,%s,%s,%s,%s\n"%(row[0],row[1],row[2],row[3],row[4],row[5]))
      newone.close()

  os.replace('temp','Class A.csv')

Here is the Error message I'm getting:

1 (1).py", line 113, in part2
    os.replace('temp','Class A.csv')
PermissionError: [WinError 5] Access is denied: 'temp' -> 'Class A.csv'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • just because you can write to a file doesn't mean you have the permissions to delete that file (which is essentially what your .replace() call is doing). – Marc B Mar 30 '15 at 20:50
  • But shouldn't it replace the file? – Mohammad Jaji Mar 30 '15 at 20:52
  • the code is a maths problem, which should save the recent 3 scores and the only way I found out was to create new file which but the new score and delete the old one. so for eg, if there was user who run the quiz more then 3, the code should save the recent 3 scores and delete the oldest one. – Mohammad Jaji Mar 30 '15 at 20:55
  • [This](http://stackoverflow.com/questions/26091530/permissionerror-winerror-5-access-is-denied-python-using-moviepy-to-write-gif) might help. – David Mar 30 '15 at 20:55
  • David, I used the run administrator and it still gives me that error and also i got he latest version of python so that not the problem. thank – Mohammad Jaji Mar 30 '15 at 20:58

0 Answers0