Search code examples
pythonwindowspython-2.7unlink

windows: python os.unlink raise OSError, [Error 5] Access is denied


run(by task scheduler using a different user account) a python script on windows and try to delete a file by using os.unlink, but got [Error 5] Access is denied exception.

try:
    os.unlink(file_path)
except OSError as error:
    logger.error('failed, error: {0}'.format(error))

2014-09-18 03:53:44,023 - error: [Error 5] Access is denied: u'C:\path\test.tgz'


Solution

  • This script is run by task scheduler. There are two options to fix this: one is granting full control of the folder to the user account so that the script will be able to delete files; the other one is to check 'Run with highest privileges' in the task scheduler.