The backslash indicates a special escape character. Therefore, directory = path_to_directory.replace("\", "\\")
would cause Python to think that the first argument to replace didn't end until the starting quotation of the second argument since it understood the ending quotation as an escape character.
directory=path_to_directory.replace("\\","\\\\")