Search code examples
pythonwindowspermissionsoperating-system

Permission errors with os.replace


Permission errors when using the os library to replace a file with a temp file.

PermissionError: [WinError 5] Access is denied: 'C:\\Users\\User\\Documents\\newewenwekwe_proxies.txt.tmp' -> 'C:\\Users\\User\\Documents\\newewenwekwe_proxies.txt'

Code:

    os.replace(tempfile, filename)

Tried restarting my laptop, closing the file, running pycharm as an admin + running cmd as an admin and running the script. P.S I have administrator perms on my laptop.


Solution

  • Try os.rename() instead.

    The reason why can be found here.