13
import itertools  

variations = itertools.product('abc', repeat=3)  
for variations in variations:  
    variation_string = ""  
    for letter in variations:  
        variation_string += letter  
    print (variation_string)  

How can I redirect output into a txt file (on windows platform)?

joaquin
  • 82,968
  • 29
  • 138
  • 152
user593908
  • 143
  • 1
  • 1
  • 6

6 Answers6

18

From the console you would write:

python script.py > out.txt

If you want to do it in Python then you would write:

with open('out.txt', 'w') as f:
    f.write(something)

Obviously this is just a trivial example. You'd clearly do more inside the with block.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
7

You may also redirect stdout to your file directly in your script as print writes by default to sys.stdout file handler. Python provides a simple way to to it:

import sys  # Need to have acces to sys.stdout
fd = open('foo.txt','w') # open the result file in write mode
old_stdout = sys.stdout   # store the default system handler to be able to restore it
sys.stdout = fd # Now your file is used by print as destination 
print 'bar' # 'bar' is added to your file
sys.stdout=old_stdout # here we restore the default behavior
print 'foorbar' # this is printed on the console
fd.close() # to not forget to close your file
AndyG
  • 39,700
  • 8
  • 109
  • 143
user473489
  • 116
  • 2
  • If you're on Windows watch out for Windows bug - [Cannot redirect output when I run Python script on Windows using just script's name](http://stackoverflow.com/questions/3018848/). Also, the default system handler for standard output is stored in `sys.__stdout__` so unless you want to revert to the previously set one there's no need to save `sys.stdout` before assigning to it. – Piotr Dobrogost Oct 04 '12 at 11:07
2

In window command prompt, this command will store output of program.py into file output.txt

python program.py > output.txt
kefeizhou
  • 6,234
  • 10
  • 42
  • 55
  • the out put to the above program is like a clustered state ::aaaaabaacabaabbabcacaacbaccbaababbacbbabbbbbcbcabcbbcccaacabcaccbacbbcbcccaccbccc//////Sir can you modify the program so that the output would be in a line after line that is the first line of the output would be aaa and the next line would be aab and the next would be aac and so on ....Thank you – user593908 May 16 '11 at 12:43
1

If it were me, I would use David Heffernan's method above to write your variable to the text file (because other methods require the user to use a command prompt).

import itertools  

file = open('out.txt', 'w')
variations = itertools.product('abc', repeat=3)  
for variations in variations:  
    variation_string = ""  
    for letter in variations:  
        variation_string += letter  
    file.write(variation_string)
file.close()
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Adam
  • 442
  • 1
  • 6
  • 18
1

you may use >>

log = open("test.log","w")

print >> log, variation_string

log.close()
YOU
  • 120,166
  • 34
  • 186
  • 219
  • import itertools file = open('out.txt', 'w') variations = itertools.product('abc', repeat=3) for variations in variations: variation_string = "" for letter in variations: variation_string += letter file.write(variation_string) file.close() the out put to the above program is like a clustered state ::aaaaabaacabaabbabcacaacbaccbaababbacbbabbbbbcbcabcbb//////Sir can you modify the program so that the output would be in a line after line that is the first line of the output would be aaa and the next line would be aab and the next would be aac and so on ..Thank you – user593908 May 16 '11 at 12:52
0

Extension to David's answer

If you are using PyCharm,

Go to Run --> Edit Configurations --> Logs --> Check mark Save console output to file --> Enter complete path --> Apply

Screenshot

Morse
  • 8,258
  • 7
  • 39
  • 64