I agree with you, Fleming had almost given up after years of research in vane, but then, just by not cleaning a pile of petri dishes he ended up making one of the most important discoveries in the history of medicine. He found that the mold, that grew because of the dirt that those dishes had, produced something that killed bacteria by targeting the cell walls and inhibiting the bacteria's reproduction.
This means that his discovery was by "accident".