Issues with in try except and excel spreadsheet

2017-03-13 Thread padawanwebdev
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!  

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)

FYI: The assetMapping.py runs another module from inside, and it's this module 
running from assetMapping that writes to the excel file.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Issues with in try except and excel spreadsheet

2017-03-13 Thread padawanwebdev
On Monday, March 13, 2017 at 11:10:36 AM UTC-7, Rhodri James wrote:
> On 13/03/17 17:40, [email protected] 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

I apologize if I short-changed you with the code. It's a bigger piece of code 
and I thought maybe with my explanation and the piece I showed it would be 
enough. I was wrong. Here is the rest. I apologize in advance if there are any 
more confusions. 

I do appreciate your help! Thanks in advance :-)


THIS IS: Main.py

import InternetQualityTest
import assetMapping
import time
import sys



if __name__ == '__main__':

while True:

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 IS: constants.py

import urllib
import socket
from openpyxl.reader.excel import load_workbook




hostname = socket.gethostname()
getip = urllib.urlopen('https://api.ipify.org').read()

errorReportWB = 
load_workbook(r'C:\Users\Administrator\Desktop\Version5.0\DeviceTrackerReport45.xlsx')
masterListWB = 
load_workbook(r'C:\Users\Administrator\Desktop\Version5.0\masterListWB.xlsx')
yardWB = load_workbook(r'C:\Users\Administrator\Desktop\Version5.0\yardWB.xlsx')

errorReportSheet = errorReportWB.get_sheet_names()
masterListSheet = masterListWB.get_sheet_names()
yardSheet = yardWB.get_sheet_names()


import constants
import DataRedundancyCheck
import assetMappingFunctions


ipCoordinate = []
deviceCoordinate = [] 

#xx#
 
##


THIS IS: assetMapping.py


## Initital Test: checks if IP is in master list
initialCheck = assetMappingFunctions.functions()
  #Creat object Initial_Test of Asset_Mapping_Module 
Class from the Asset_Mapping.py file
masterInventorySheet = 
initialCheck.yard(constants.masterListSheet,0,constants.masterListWB)
#Get Master list sheet name 
ipMasterInventoryCount = initialCheck.inventoryCount(masterInventorySheet,"B")  
#Get number count of IP addresses in the Master list 
ipMasterInventory = 
initialCheck.ipDeviceList(ipMasterInventoryCount,"B",masterInventorySheet)  
#Get the list of IPs in the Masterlist 
ipMasterValidation = 
initialCheck.ipValidationCheck(ipMasterInventory,masterInventorySheet) 
#Test if external IP address is in the Master List

if ipMasterValidation==False

Re: Issues with in try except and excel spreadsheet

2017-03-14 Thread padawanwebdev
On Tuesday, March 14, 2017 at 4:42:39 AM UTC-7, Rhodri James wrote:
> On 13/03/17 20:37, [email protected] wrote:
> > On Monday, March 13, 2017 at 11:10:36 AM UTC-7, Rhodri James wrote:
> >> On 13/03/17 17:40, [email protected] 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
> >
> > I apologize if I short-changed you with the code. It's a bigger piece of 
> > code and I thought maybe with my explanation and the piece I showed it 
> > would be enough. I was wrong. Here is the rest. I apologize in advance if 
> > there are any more confusions.
> >
> > I do appreciate your help! Thanks in advance :-)
> >
> >
> > THIS IS: Main.py
> >
> > import InternetQualityTest
> > import assetMapping
> > import time
> > import sys
> >
> >
> >
> > if __name__ == '__main__':
> >
> > while True:
> >
> > 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)
> 
> [Large amount of code with no suspicious-looking loops snipped]
> 
> Thanks for the extra code.  It's hard to get the balance right between 
> giving people enough information to help and giving us so much 
> information we can't see the wood for the trees.
> 
> Anyway, I'll go back to my questions.  What output do you have that 
> convinces you that assetMapping.py is invoked three (or however many) 
> times while the excel file is open?  What output do you have that 
> convinces you that it then writes three (or however many) times?  As far 
> as I can tell, the loop above can only repeat the call to execfile() if 
> it catches an exception: what were those exceptions?
> 
> -- 
> Rhodri James *-* Kynesim Ltd

As an example, If I have the excel file open and there is data that needs to be 
written to the excel file than the exception will catch this error: 

IOError: [Errno 13] Permission denied: 
'C:\\Users\\Administrator\\Desktop\\Version5.0\\DeviceTrackerReport45.xlsx'

essentially, because the excel file is open during the process of writing to 
the excel file the "try" will keep attempting to write to the excel until its 
closed. So, if there were 5 attempts to write the data to the excel file, then 
after the excel file is closed that data will be written to the excel file n+1  
times (6 times). When this happens it shows 6 rows of the newly written data, 
however, it's the same data. To illustrate this here is the output from the 
shell line while the excel file is open and when it's closed. Note