def clean(file_, imports):
modules_not_imported = compare_modules(file_, imports)<EOL>re_remove = re.compile("<STR_LIT:|>".join(modules_not_imported))<EOL>to_write = []<EOL>try:<EOL><INDENT>f = open_func(file_, "<STR_LIT>")<EOL><DEDENT>except OSError:<EOL><INDENT>logging.error("<STR_LIT>".format(file_))<EOL>raise<EOL><DEDENT>else:<EOL><INDENT>for i in f.readlines():<EOL><INDENT>if re_remove.match(i) is None:<EOL><INDENT>to_write.append(i)<EOL><DEDENT><DEDENT>f.seek(<NUM_LIT:0>)<EOL>f.truncate()<EOL>for i in to_write:<EOL><INDENT>f.write(i)<EOL><DEDENT><DEDENT>finally:<EOL><INDENT>f.close()<EOL><DEDENT>logging.info("<STR_LIT>" + file_)<EOL>
Remove modules that aren't imported in project from file.