I'm sharing an experience that might save some time to other users some day in the future.
It happened while reading a list of .xls files in python/pandas to append them to dataframe. The code is below. If a file in the list is missing the code will notice with a try/except. One of these files is named 'CON.xls' and the file was missing.
When the loop was executed the try/except apparently does not work. The program is on hold and nothing happen. This is only when the file is named 'CON.xls'. The code was OK with all the other file names in the list.
I then tried to create a 'CON.xls' file saving it directly from excel and even excel refused to accept the name. 'CON.xls' is a reserved file name.
Try/except was apparently not detecting this kind of issue or was not the right way in this case:
def db_to_df(list_of_file_names):
return_df=pd.DataFrame([])
for file_name in list_of_file_names:
try:
df=pd.DataFrame([])
df=pd.read_excel(file_name+'.xls')
return_df = pd.concat([return_df, df])
print('\tFile added: ', file_name)
except:
print('\nERROR: ', file_name,'\n')
return(return_df)
The above with WIN7 and a very old 2003 xls, not sure with other versions.