Animation by scripting I am actually trying to move a car along a path. The path is stored in a file. 

Right now I am reading each and every position from the file, moving the car to that position followed by appropriate rotation, then I assign a key. 
following is the code I am using:

    ob = bpy.data.objects['car']
    bpy.context.scene.objects.active = ob 
    ob.select = True
    frame_num = 0
    pos = [list(a) for a in positions]
    m = [0.0,0.0,0.0]
    for j in range(len(pos)-1):
        for k in range(3):
            m[k] = pos[j+1][k] - pos[j][k]
        DirectionVector = mathutils.Vector(tuple(m))
        bpy.context.scene.frame_set(frame_num)
        ob.location = pos[j]
        ob.rotation_mode = 'QUATERNION'
        ob.rotation_quaternion = DirectionVector.to_track_quat('X','Y')
        bpy.ops.anim.keyframe_insert_menu(type="Location")
        bpy.ops.anim.keyframe_insert_menu(type="Rotation")
        frame_num += 30
        j += 1​


Even after all this I am not satisfied with my result. In real life when we see an obstacle in front of us we rotate to our left or right. But here the car rotates far before it reaches the position. I know it is because I have defined the keyframes like that.
I tried using two for loops, but even the end result in that is not that good.

My goal is to have the car avoid obstacles by making a tighter turn around them. How do I achieve this?I feel that there should be some easy way to do this. Can someone help me?

This is a part of my environment.
Consider inserting more keyframes so you have more options to change directions.  Perhaps you would need to calculate intermediate turning locations such as locations on a simplified half ellipse to avoid an obstacle.

frame_num += 30 would become

frame_num += 5 and other python changes
Consider a [Follow Path Constraint] whose offset and influence can be animated via keyframe.  Perhaps left and right turn constraints.