Search and replace image urls in posts

I coudn’t solve it with a plugin or plain SQL, see this other question. Instead I exported all the posts with Tools > Data export > WordPress and deleted the posts. Then I run this python script over the exported XML file and imported the update xml file again. Tadaa…

import re

p = re.compile(r'http://([^/]*)/getfile/files.posterous.com/([^/]*)/([^/]*)/([^/]*)')

f1 = open('input.xml', 'r')
f2 = open('output.xml', 'w')

for line in f1:
    f2.write(p.sub(r'http://my.domain.com/wordpress/wp-content/uploads/legacy/\4', line))

f1.close()
f2.close()