Either that or it was a ruse to show Hillary was “innocent” and it backfired. He never intended to prosecute her from the beginning. It was all likely a kabuki dance to move one of the black clouds hovering of her head.
Comey probably figured that Hillary was sure to win at that point and if he suppressed news of the investigation until after the election he would look bad. Remember he did re-exonerate her a few days later. Early voting had already started and it isn’t known if Comey’s announcement changed any votes.