I write function operates with some data. I start from reading data from file, put it into structure and then do some calculations. My problem - incoming file can be txt, csv or xls (xlsx). For different types of file I need to use different methods.
So, my question is - how to check the type of incoming file.
Now I use the next approach:
def main(filename):
if '.txt' in filename:
# use read_txt
elif '.xls' in filename:
print('there is no txt method')
else:
print('etc.')
But it is not perfect: for example if I create file My.txt.xls
it fails.
I think about try
and use all the reading functions one by one, but look like there is some more obvious way...