A motorist barley avoided being hit by a train after failing to check if the track was clear in both directions.