Search code examples
pythonpermission-denieddataloadernetwork-drive

PermissionError Access denied


while loading some data from a network drive a permission error occurs from time to time and the script terminates with a permission error.

the error occurs in this line :

try:
   data =  self.data_file_loader(path)
except PermissionError:
   print('here !!')

i assume that the error occurs because of some issues in the network drive. please note that this error happens just from time to time or/and in case of iteration over a large dataset.

a workaround i used is to catch the exception, wait 10 ms and then try again, it worked, but the code isn t really stable besides taking more time to load the data.

try:
   data =  self.data_file_loader(path)
except PermissionError:
   print('here !!')
   time.sleep(10)
   data = self.data_file_loader(path)

is there a better way to do this ?


Solution

  • You could implement some sort of loop with a safety net if the code takes too long. It's pretty much based off what you suggested, but with more 'stability.'

    MAX_TRIES = 5
    SLEEP_TIME = 10
    for _ in range(MAX_TRIES):
        try: 
            data = self.data_file_loader(path)
            break
        except PermissionError:
            time.sleep(SLEEP_TIME)
    else:
        # do something to exit the program here, this block is only called when data could not be read