import io with io.open('dateiname.txt', 'w', encoding='utf-8') as outfile: for i in range(10): line = "Zeile " + str(i) + "\n" outfile.write(line)