Ex-CIA chief David Petraeus took the blame for the extramarital affair that torpedoed his career, but his forgiving wife, Holly, is sticking by him, according to a newly revealed private letter.

“I screwed up royally,” Petraeus told his longtime Army pal, retired Brig. Gen. James Shelton in the hand-written missive.

“I paid the price (appropriately) and I sought to do the right thing at the end of the day,” he wrote in the Nov. 20 message.

Petraeus stepped down on Nov. 9 as news of his affair with biographer Paula Broadwell went public.

“He’s a superb human being. What happened to him happened to a number of people,” lamented Shelton during a phone interview with The Post from his Englewood, Fla., home.

Since stepping down, Petraeus has not publicly discussed the affair that led to his resignation.

In his letter, however, Petraeus says that neither he nor his wife is ready to end their 37-year marriage.

“Team Petraeus will survive though [I] have obviously created enormous difficulty for us,” he wrote. “Holly is, however, once again demonstrating how incredibly fortunate I was to marry her.”

He signed the note “ATW” for “All The Way” — the military call for Army parachutists.

“It means, ‘I’m going to give it everything I got.’ That’s David Petraeus. All the way. I can’t say that enough,” Shelton said.

Shelton has known Petraeus for three decades, since they first bonded over a bottle of Ballantine’s scotch in a tent with other soldiers in Erurum, eastern Turkey, during the early 1970s.

Petraeus, 60, was never one to chase after women and was devoted to Holly, whom he met at West Point, where her father was commandant, Shelton said.

Sources had earlier stated the obvious — that Holly, the mother of their two adult children, was “furious” at Petraeus for the hanky-panky.

Shelton put the blame on fellow West Point graduate Broadwell, 40, for the affair and speculated that Petraeus, whom he described as naive, fell for her charms while the two jetted to military bases and war zones for her Petraeus biography.

“He’s a man, and all men have an Achilles’ heel,” Shelton said.