def remove_dir(directory):
try:<EOL><INDENT>rmtree(directory)<EOL><DEDENT>except OSError as e:  <EOL><INDENT>from errno import EACCES<EOL>if e.errno == EACCES:<EOL><INDENT>from stat import S_IWRITE<EOL>for dirpath, dirnames, filenames in os.walk(directory):<EOL><INDENT>for filename in filenames:<EOL><INDENT>os.chmod(os.path.join(dirpath, filename), S_IWRITE)<EOL><DEDENT><DEDENT>rmtree(directory)<EOL><DEDENT><DEDENT>
Delete directory and all contained files and subdirectories.

    Note: this is the same as the shutil's rmtree function but overcomes
    a reported issue on Windows*.

    *Due to permissions (being able to create but not remove files).