Blender script keyframe in loop doesn't affect first element
It looks like you have more than one problem:
One try using obj.keyframe_insert(data_path='location', frame=frame_num)

instead of 

scene.frame_set(frame_num)
    ob.keyframe_insert(data_path='location', index = -1).

Also, you set frame_num = 20 inside your while loop... which should cause an infinite loop.

How to keyframe_insert without moving the object