# Buggy code
def __init__(self, **kwargs):
    super(AzCli, self).__init__(**kwargs)

    from azure.cli.core.commands.arm import add_id_parameters
    from azure.cli.core.cloud import get_active_cloud
    from azure.cli.core.extensions import register_extensions
    from azure.cli.core._session import ACCOUNT, CONFIG, SESSION

    import knack.events as events

    self.data["headers"] = {}
    self.data["command"] = "unknown"
    self.data["command_extension_name"] = None
    self.data["completer_active"] = ARGCOMPLETE_ENV_NAME in os.environ
    self.data["query_active"] = False

    azure_folder = self.config.config_dir
    if not os.path.exists(azure_folder):
        os.mkdir(azure_folder)
    ACCOUNT.load(os.path.join(azure_folder, "azureProfile.json"))
    CONFIG.load(os.path.join(azure_folder, "az.json"))
    SESSION.load(os.path.join(azure_folder, "az.sess"), max_age=3600)
    self.cloud = get_active_cloud(self)
    logger.debug("Current cloud config:\n%s", str(self.cloud.name))

    register_extensions(self)
    self.register_event(events.EVENT_INVOKER_POST_CMD_TBL_CREATE, add_id_parameters)
    # TODO: Doesn't work because args get copied
    # self.register_event(events.EVENT_INVOKER_PRE_CMD_TBL_CREATE, _pre_command_table_create)

    self.progress_controller = None


# Traceback (IOError)
$ az
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/usr/local/lib/python2.7/dist-packages/azure/cli/__main__.py", line 35, in <module>
help_cls=AzCliHelp)
File "/usr/local/lib/python2.7/dist-packages/azure/cli/core/__init__.py", line 47, in __init__
ACCOUNT.load(os.path.join(azure_folder, 'azureProfile.json'))
File "/usr/local/lib/python2.7/dist-packages/azure/cli/core/_session.py", line 42, in load
self.save()
File "/usr/local/lib/python2.7/dist-packages/azure/cli/core/_session.py", line 46, in save
with codecs_open(self.filename, 'w', encoding=self._encoding) as f:
File "/usr/lib/python2.7/codecs.py", line 896, in open
file = __builtin__.open(filename, mode, buffering)
IOError: [Errno 2] No such file or directory: '/root/.azure/azureProfile.json'
$

# Fixed code
def __init__(self, **kwargs):
    super(AzCli, self).__init__(**kwargs)

    from azure.cli.core.commands.arm import add_id_parameters
    from azure.cli.core.cloud import get_active_cloud
    from azure.cli.core.extensions import register_extensions
    from azure.cli.core._session import ACCOUNT, CONFIG, SESSION

    import knack.events as events
    from knack.util import ensure_dir

    self.data["headers"] = {}
    self.data["command"] = "unknown"
    self.data["command_extension_name"] = None
    self.data["completer_active"] = ARGCOMPLETE_ENV_NAME in os.environ
    self.data["query_active"] = False

    azure_folder = self.config.config_dir
    ensure_dir(azure_folder)
    ACCOUNT.load(os.path.join(azure_folder, "azureProfile.json"))
    CONFIG.load(os.path.join(azure_folder, "az.json"))
    SESSION.load(os.path.join(azure_folder, "az.sess"), max_age=3600)
    self.cloud = get_active_cloud(self)
    logger.debug("Current cloud config:\n%s", str(self.cloud.name))

    register_extensions(self)
    self.register_event(events.EVENT_INVOKER_POST_CMD_TBL_CREATE, add_id_parameters)
    # TODO: Doesn't work because args get copied
    # self.register_event(events.EVENT_INVOKER_PRE_CMD_TBL_CREATE, _pre_command_table_create)

    self.progress_controller = None