简体   繁体   中英

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

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM