Read CSV - The process cannot access the file

Hello Dynamo Friends :slight_smile:

When reading CSV-files i ran into the problem of getting the Windows Error:

The process cannot access the file because another process has locked a portion of the file

This was because opening and closing the CSV was sensitive to error, this was improved by using the “With” method. But for about 2-5% of runs the problem remains.

So let´s solve that problem by looping the reading process until it works, there are just some things i don´t know how to handle.

First draft:

timeout = time.time() + 120 #seconds

bool = True
while bool == True:
time.sleep(0.5) # sleep for 500 milliseconds
	if time.time() > timeout:
		syncresult="timeout"
		break
	with io.open(SlogFilePath,"r", encoding = "utf-16(LE)") as File:	
		FILE = File.read().splitlines()
		if READING_DID_WORK:
			bool = False
		else:
			bool = True

OUT = FILE
  • How can i check if READING_DID_WORK? Just checking if FILE = List, or is there a better way. FILE could also be an empty list, so that case has to be covered.

  • Is there a way to induce that failing access problem? Because i have to test my code somehow!

Happy about any advice :slight_smile:
kind regards!

Got a reply on Stack, will take me a few days to understand it, then I´ll be back with explanation.

Lets go, all these strange “bool = whatever” phrases i used are not neccessary because python already has a function for this:

While True:

So i can start my While loop for reading a CSV like:

while True:
	try:
		with io.open(SlogFilePath,"r", encoding="utf-16(LE)") as SlogFile:
			SLOG = SlogFile.read().splitlines()

This will now run forever because it´s not really a condition and so always true.
To handle a failing attempt we need to use an except statement. Because i know i want to handle a Windows Error i can define the error like this:

while True:
	try:
		with io.open(SlogFilePath,"r", encoding="utf-16(LE)") as SlogFile:
			SLOG = SlogFile.read().splitlines()
    except OSError:
        time.sleep(0.5)

Now this loop will run again and again even if there is a file access error.

To get an output if the reading works we need to add an else statement.

while True:
	try:
		with io.open(SlogFilePath,"r", encoding="utf-16(LE)") as SlogFile:
			SLOG = SlogFile.read().splitlines()
    except OSError:
        time.sleep(0.5)
    else:
        return SLOG

So the exception handles what happens if we encounter an error, the else statement handles what happens if there is no error.
We add a maximum amount of retries to be save. After the max amount is reached, we raise the error, even if there is none.

def Read_SLOG():
    retry = 0
    max_retries=5
    while True:
        try:
            with io.open(SlogFilePath,"r", encoding="utf-16(LE)") as SlogFile:
                SLOG = SlogFile.read().splitlines()
        except OSError:
            time.sleep(0.5)
            retry += 1
            if retry > max_retries:
                raise
        else:
            return SLOG

Similar procedure for writing:

def Write_QLOG_START_Entry():
	retry = 0
	max_retries=5
	while True:
		try:
			with io.open(QlogFilePath,"w", encoding = "UTF8", newline='') as Qlogfile:	
				for Qlog in QLOG_START_New:
					writer = csv.writer(Qlogfile,delimiter="'")
					writer.writerow([Qlog])
		except OSError:
			time.sleep(0.5)
			retry += 1
			if retry > max_retries:
				raise
		else:
			return QLOG_START_New_Entry

More Info:

What Are Try/Except Statements in Python? | by Jonathan Hsu | Better Programming
8. Errors and Exceptions — Python 3.11.0 documentation

2 Likes