It should reach the last file in the directory and then stop, but instead it looks for another file and causes an error.