Issues with in try except and excel spreadsheet

Rhodri James rhodri at kynesim.co.uk
Mon Mar 13 14:01:41 EDT 2017


On 13/03/17 17:40, padawanwebdev at gmail.com wrote:
> Hello, I'm having a problem with a try except inside a while loop. The problem I see occuring has to do with an excel file the script tries to write to while the excel file is open. The exception captures and gives an error:
>
> OError: [Errno 13] Permission denied:'C:\\Users\\Administrator\\Desktop\\Version5.0\\DeviceTrackerReport45.xlsx'
>
> This is expected and is not the problem. However, the issue occurs when I close the excel file. As an example, if I run the script and the excel file is open thru 3 iterations, meaning that the script can't write to the excel file until the excel file is closed, then after the excel file is closed the script prints to the excel file 3 times. I don't need it to write to the excel file 3 times. I only need it to write just once. If the excel file is closed before I run the script than it works like its suppose too.
> I hope I made my case clear. I look forward to some feedback. Any would be greatly appreciated!

When you say "...the excel file is open thru 3 iterations", what do you 
mean?  Three iterations of what?  You haven't shown us a loop, so it's 
not obvious.

How do you know it prints (writes?) to the excel file three times? 
Again, there's nothing in the code snippet that would tell you.

> here is part of the code:
>
>         connectionResults = None
>         returnResults = InternetQualityTest.connectionTest(connectionResults)
>         if returnResults == True:
>             try:
>                 execfile('assetMapping.py')
>                 time.sleep(4)
>                 sys.exit()
>             except Exception as e:
>                 print "error",e
>                 time.sleep(20)

This looks plausible, though honestly the execfile makes me feel icky 
for unrelated reasons.  I repeat, though; this isn't a loop, and my 
crystal ball isn't up to telling me how it gets invoked.

> FYI: The assetMapping.py runs another module from inside, and it's this module running from assetMapping that writes to the excel file.
>


-- 
Rhodri James *-* Kynesim Ltd



More information about the Python-list mailing list