Forum Discussion

M_Schofer_1111's avatar
M_Schofer_1111
Occasional Contributor
2 years ago

Writing To an Excel File -Python

I have a script I call Write to File

It is called from a loop.  I write to the file two to three times per loop.

Every once in a while, I get an error message that the file is being processed and I get an error. 

I have run it again and it blows through the error.

 

The file is not Open.

How do assure not getting the error.   

 

def WriteToLog(Message):

curTime = aqDateTime.Now()
cpu = Sys.CPUUsage + 100
curUser = Sys.UserName
excelFile = Excel.Open(Project.Variables.LogFile)
excelSheet = excelFile.SheetByTitle["2023"]
# Write the obtained data into a new row of the file
RowIndex = excelSheet.RowCount
RowIndex = RowIndex+1
excelSheet.Cell["A",RowIndex].Value = (RowIndex)
excelSheet.Cell["B", RowIndex].Value = "Passed"
excelSheet.Cell["C", RowIndex].Value = curUser
excelSheet.Cell["D", RowIndex].Value = curTime
excelSheet.Cell["E", RowIndex].Value = DDT.CurrentDriver.Value[3]
excelSheet.Cell["F", RowIndex].Value = DDT.CurrentDriver.Value[8]
......(rows omitted for clarity

excelSheet.Cell["W",RowIndex].Value = (str(DDT.CurrentDriver.Value[40]) + ' Status')
excelSheet.Cell["X",RowIndex].Value = (str(aqString.trim(DDT.CurrentDriver.Value[41])) + ' Status Change Dt')
excelSheet.Cell["Y",RowIndex].Value = (Message + ' Message')

excelFile.Save()

  • The error message you're receiving may be due to a concurrency issue, which occurs when multiple processes try to access the same file at the same time. It's possible that the file is being accessed by another process or application, causing your script to fail.

    One way to avoid this error is to use file locking to ensure that only one process can access the file at a time. Python provides a built-in module called flock that you can use to achieve this.

    Here's an example of how you can use flock to lock the file before writing to it:

     

    import fcntl
    
    def WriteToLog(Message):
        curTime = aqDateTime.Now()
        cpu = Sys.CPUUsage + 100
        curUser = Sys.UserName
        excelFile = Excel.Open(Project.Variables.LogFile)
        excelSheet = excelFile.SheetByTitle["2023"]
        RowIndex = excelSheet.RowCount
        RowIndex = RowIndex+1
        
        with open(Project.Variables.LogFile, "a") as file:
            fcntl.flock(file, fcntl.LOCK_EX) # Lock the file
            file.write(f"{RowIndex}\tPassed\t{curUser}\t{curTime}\t{DDT.CurrentDriver.Value[3]}\t{DDT.CurrentDriver.Value[8]}\t...\t{str(DDT.CurrentDriver.Value[40])} Status\t{str(aqString.trim(DDT.CurrentDriver.Value[41]))} Status Change Dt\t{Message} Message\n")
            fcntl.flock(file, fcntl.LOCK_UN) # Unlock the file
        excelFile.Save()

     

     

    In this example, we're using a context manager (with open(...) as file:) to open the file and automatically close it when we're done. We're also using the fcntl.flock() function to lock the file before writing to it (fcntl.LOCK_EX), and unlock it afterwards (fcntl.LOCK_UN).

    Note that we're writing to the file using a single file.write() call, which reduces the number of times we need to access the file and minimizes the chances of a concurrency issue. Also, we're using tabs (\t) as a delimiter between the fields, but you can use any other character that suits your needs.

    I hope this helps you avoid the error message you're receiving. Let me know if you have any questions! milestoneapply.com

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    In your loop, are you calling WriteToLog()? If yes, then you are opening the file, writing data and then saving it, but you are not closing the file. Either check if the file opened, so you don't open it again or close the file after saving.