<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def ensure_directory(path):
 """ Ensure directory exists for a given file path. """

    dirname = os.path.dirname(path)
    if not os.path.exists(dirname):
        os.makedirs(dirname)