Ho do I write a script to remove loose geometry from all objects?
Set the active object:

bpy.context.scene.objects.active = o


and you should check wether the object is a mesh object:

if o.type == 'MESH': 
    ...