import os def rewrite_file(filepath): with open(filepath, 'r') as f: contents = f.read() for filepath in os.listdir(): if file.endswith(".txt"): rewrite_file(filepath)