Use a try and except
So you try to print everything, except if you get an error it is probably the index error meaning you are done.
for by5 in range(0,len(temp_dir),5): try: print (temp_dir[by5]) print (temp_dir[by5+1]) print (temp_dir[by5+2]) print (temp_dir[by5+3]) print (temp_dir[by5+4]) except: print('That\'s all')
So you try to print everything, except if you get an error it is probably the index error meaning you are done.