How do I save output into a text file in python?

自古美人都是妖i 提交于 2019-12-13 07:26:23

问题


So what I want to do is save the output of this program into a text file.

import itertools
res = itertools.product('qwertyuiopasdfghjklzxcvbnm', repeat=3)
for i in res: 
print ''.join(i)

Im running python 2.7


回答1:


You can use open and then write method of the resulting file handler.

import itertools
res = itertools.product('qwertyuiopasdfghjklzxcvbnm', repeat=3)

with open('output.txt', 'w') as f:
    for group in res:
        word = ''.join(group)
        f.write(word+'\n')
        print(word)


来源:https://stackoverflow.com/questions/40558729/how-do-i-save-output-into-a-text-file-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!