import os

class RunDir(object):
    def __init__(self, dir, create=False):
        self._run_dir = dir
        self._create = create

    def __enter__(self):
        self._starting_dir = os.path.abspath(os.getcwd())
        if self._create and not os.path.isdir(self._run_dir):
            os.makedirs(self._run_dir)
        os.chdir(self._run_dir)

    def __exit__(self, type, value, traceback):
        os.chdir(self._starting_dir)


def open_run_dir(dir, **kwds):
    return RunDir(dir, **kwds)

@@ -4,16 +4,17 @@
 ort os%0A%0A
+%0A
 class Ru
@@ -53,16 +53,22 @@
 elf, dir
+ectory
 , create
@@ -103,16 +103,22 @@
 ir = dir
+ectory
 %0A       
@@ -381,16 +381,26 @@
 _(self, 
+exception_
 type, va
@@ -471,24 +471,30 @@
 _run_dir(dir
+ectory
 , **kwds):%0A 
@@ -513,16 +513,22 @@
 nDir(dir
+ectory
 , **kwds

Rename dir variable to directory.