Forum Discussion
FICO
5 years agoCommunity Manager
Issue with scenario attachments; attachment not found
Hello - looking for advice/help related to an issue with scenario attachments. we are using python to create several excel workbook files, which contain model input data. we have a set of strings (i.e. setA), and each element of setA should have a corresponding excel file. then, we iterate over setA and use insightputscenattach to upload each attachment.
we are finding that insightputscenattach fails (consistently) for one of the files. in mosel, we use getfsize to check to see if the file exist (e.g. has a non-0 size).
from python, we can read the file and we see that the file exists, has data, and therefore the size is not 0.
Bottom line: python is indicating the the files/attachments are fine (exist, and have a non-0 size). mosel is indicating that one of the files/attachments does not exist. We are unable to diagnose the issue, and thus unable to resolve the issue with the attachment.
Gary
18 Replies
- FICOCommunity Manager
Here are few questions to guide further investigations (your description seems to hint at a situation where the file has been locked by the program that has been used to access it):
- Can you confirm whether you are running the software on a Windows platform?
- How has the spreadsheet file been opened?
- Are you certain that the file has been closed + released by the software that has been used to open it at the point where it is accessed by Mosel?
- FICOCommunity Manager
Hi Susanne, some answers:
originally, we create/write-to the excel file from python. at that point, we did not attempt to re-open the file. now, we wanted to confirm that a file was created properly; so we do the following: [1] create and write-to file using python, [2] read content of file in python to verify that file contains the expected data.
I will check to see that the file is closed (by python) before we try to access the file in Mosel. but, I can say the there are ~10 similar files being created and uploaded as a scenario attachment, and only 1 of the 10 exhibiting this problem. and all 10 are created in the same python code. In mosel, am I able to see/list the content of files in the working directory? If so, I would do this to check to see what files are available.
Gary
- FICOCommunity Manager
You can use the routine 'findfiles' to list all files or files of certain types in a specified directory (current working directory if none is specified; optionally recursive configuration=search all subdirectories), for example:
declarations lsf: list of text end-declarations ! List all files with extensions xls/xslx in current working directory findfiles(lsf, "*.xls|*.xlsx") writeln(lsf)The Mosel routines 'getfsize' or 'getfstat' will try to access (open) a file and will therefore fail to return any useful information if Mosel cannot access it because the file is locked by some other program.
I think that what could have happened is that Python still has some kind of handle on the file, perhaps it has not yet flushed the contents that is to be written?
- FICOCommunity Manager
Thanks, this is helpful. So, I used findfiles before and after running the python script: findfiles(lsf,"*.xlsx")
After running python, I see the following output:
[110A Model Input.xlsx,120A Model Input.xlsx,2000 Model Input.xlsx,2011 Model Input.xlsx,211A Model Input.xlsx,2210 Model Input.xlsx,230A Model Input.xlsx,241A Model Input.xlsx,260A Model Input.xlsx,310A Model Input.xlsx,321A Model Input.xlsx,330A Model Input.xlsx]
Note - There is a total of 12 xlsx files, as expected. The set is shown below (with 12 elements)
SET: {`110A',`2210',`2011',`241A',`230A',`260A',`310A',`211A',`2000',`120A',`330A',`321A'}
Note - when I try to add attachments, I get the following output. Code is shown below too.
issue: 110A file is shown to have file size=0. Previously, we did not check for file size but tried to upload each attachment. That would lead to a run-time error, and the execution would fail. The error was: "Error downloading attachment for 110A".
Debug: adding model input attachment for market: 110A
Error adding attachment 110A Model Input.xlsx
Filesize = 0 for 110A Model Input.xlsx
Debug: adding model input attachment for market: 2210
Attachment 2210 Model Input.xlsx added to the scenario.
Debug: adding model input attachment for market: 2011
Attachment 2011 Model Input.xlsx added to the scenario.
Debug: adding model input attachment for market: 241A
Attachment 241A Model Input.xlsx added to the scenario.
Debug: adding model input attachment for market: 230A
Attachment 230A Model Input.xlsx added to the scenario.
Debug: adding model input attachment for market: 260A
Attachment 260A Model Input.xlsx added to the scenario.
Debug: adding model input attachment for market: 310A
Attachment 310A Model Input.xlsx added to the scenario.
Debug: adding model input attachment for market: 211A
Attachment 211A Model Input.xlsx added to the scenario.
Debug: adding model input attachment for market: 2000
Attachment 2000 Model Input.xlsx added to the scenario.
Debug: adding model input attachment for market: 120A
Attachment 120A Model Input.xlsx added to the scenario.
Debug: adding model input attachment for market: 330A
Attachment 330A Model Input.xlsx added to the scenario.
Debug: adding model input attachment for market: 321A
Attachment 321A Model Input.xlsx added to the scenario.
procedure set_attachment(filename: string, tags: list of string) if getfsize(filename) > 0 then insightputscenattach(filename) if insightattachstatus=INSIGHT_ATTACH_OK then insightsetscenattachtags(filename,tags) writeln("Attachment "+filename+" added to the scenario.") else writeln("Error adding attachment "+filename) exit(1) end-if else writeln("Error adding attachment "+filename) writeln(" Filesize = 0 for "+filename) end-if end-procedure - FICOCommunity ManagerSusanne – thanks again for your support on this issue. I did reply in FICO community, but also wanted to email you directly. My current situation/understanding: 1. The insight application will fail during the step of uploading the scenario attachments. It consistently fails for “110A” 2. It appears that the file exists (based on filefiles output) 3. getfsize is reporting that file size is 0; I don’t know if that means that file is found and size is 0 (file is empty), or if file was not found (???). 4. This issue started to occur ~2 weeks back; prior to that the same code was running fine (consistently), and we did not encounter this error. 5. We are stuck! My temporary workaround is to check file size and only upload scenario attachment when size is greater than 0. But, this does not solve our issue. cc-ing Kellington, as he helped with some of the mosel development (and specifically, with the code to handle the scenario attachments). Gary
- FICOCommunity ManagerAll – according to: https://www.fico.com/fico-xpress-optimization/docs/latest/mosel/mosel_lang/dhtml/getfsize.html The function returns -1 if the file cannot be found or accessed and MAX_INT if the size exceeds the integer capacity (∼2Gb). So, apparently the file exists; but the file size is truly 0. But, we still believe that the file contains data and should not have a size=0. Gary
- FICOCommunity Manager
Given that the file exists but its size is reported as 0, I think that this needs to be addressed within the Python code - you could try the following, assuming that you have a got a Python file object f:
First do a
f.flush()and then (after an “import os”)
os.fsync(f.fileno()) - FICOCommunity Manager
Hi Gary,
since your application is running on Windows, it's possible that a virus scanner (e.g. Windows Defender) temporarily has a file lock on you xlsx file. Another problem could be, that the Python component that creates the xlsx file doesn't properly release the file handle. Which component are you using to create the xlsx files?
To see whether Windows Defender causes the problem, you could try to add a small loop: If the file size returned by Mosel is zero, wait for a few seconds, and then try again to get the file size.
By the way: Do you strictly need the combination of Mosel and Python? Or would it be an option to use pure Python, and switch to the new 'xpressinsight' Python package and write an Insight app in pure Python?
Regards,
Johannes
- FICOCommunity Manager
PS: Could you also try to explicitly determine the file size from within Python with the help of the two following commands.
It would be interesting to see if the correct file size can be determined from within Python.
os.path.getsize(file_name) os.stat(file_name).st_size - FICOCommunity ManagerThanks for the suggestions regarding the file size. Here is the output that was produced. The last file (for 110A) is the one that has been a problem and we see that: the file exists, the file size is not 0. Gary os.path.getsize(file_name) for 330A Model Input.xlsx = 22278 os.stat(file_name).st_size for 330A Model Input.xlsx = 22278 os.path.getsize(file_name) for 321A Model Input.xlsx = 7831 os.stat(file_name).st_size for 321A Model Input.xlsx = 7831 os.path.getsize(file_name) for 310A Model Input.xlsx = 8999 os.stat(file_name).st_size for 310A Model Input.xlsx = 8999 os.path.getsize(file_name) for 260A Model Input.xlsx = 8633 os.stat(file_name).st_size for 260A Model Input.xlsx = 8633 os.path.getsize(file_name) for 241A Model Input.xlsx = 12372 os.stat(file_name).st_size for 241A Model Input.xlsx = 12372 os.path.getsize(file_name) for 230A Model Input.xlsx = 15178 os.stat(file_name).st_size for 230A Model Input.xlsx = 15178 os.path.getsize(file_name) for 2210 Model Input.xlsx = 8464 os.stat(file_name).st_size for 2210 Model Input.xlsx = 8464 os.path.getsize(file_name) for 211A Model Input.xlsx = 23397 os.stat(file_name).st_size for 211A Model Input.xlsx = 23397 os.path.getsize(file_name) for 2011 Model Input.xlsx = 8161 os.stat(file_name).st_size for 2011 Model Input.xlsx = 8161 os.path.getsize(file_name) for 2000 Model Input.xlsx = 8687 os.stat(file_name).st_size for 2000 Model Input.xlsx = 8687 os.path.getsize(file_name) for 120A Model Input.xlsx = 9564 os.stat(file_name).st_size for 120A Model Input.xlsx = 9564 os.path.getsize(file_name) for 110A Model Input.xlsx = 9466 os.stat(file_name).st_size for 110A Model Input.xlsx = 9466
Related Content
Related Content
- 11Views0likes2Comments
