Chances were, more often than not, that the port was heavily defended, and most likely not worth the risk. Shallow waters, mines, subnets, and probably a handful of tin cans. That's asking for a u-boat never to return.
In my case in-game, I almost didn't make it out of Freetown alive. An ASW Trawler dumped a can right on top of my bow quarters and wrecked havoc and pushed my boat into the sea floor some 5m below me (I was at periscope depth). I somehow didn't sustain critical damage, and made note of the fact that I shouldn't dive very deep for the duration of the patrol. Little did I know that 30 hours later, that would be the very reason that sent me to the bottom. (My boat crushed at just over 80 meters when I was pushed down by a close barrage of depth charges.)
So to sum it up. Risk vs Reward...we'll pass on harbor raids...