string.index should be string.rindex to get the last "." before file extension