Fixed list index out of range error when looping over files