23

I want to save result of script to file. Example of script:

#!/usr/bin/env python i=0 while i<10: a=raw_input('Write a number') print 'Result%s'%str(2*a) i+=1 

And I want to save to file print value. I know that I can do that in script f=open()..., but I want to do that using output in terminal. I read that I can use module subprocess but I don't know it is correct.

3
  • 8
    python file.py > out.txt ?? Commented Apr 29, 2014 at 12:04
  • what u want to save? results or all conversion? Commented Apr 29, 2014 at 12:09
  • Thankfully, this existed. I wanted to make a Python IDE, and than you very much for the courage to be able to ask this. Commented Aug 31, 2020 at 2:43

3 Answers 3

26

You can redirect the output to a file using > in terminal:

python your_script.py > output.txt 
Sign up to request clarification or add additional context in comments.

2 Comments

I don't like that you relay on shell =).
IMO OP knows writing to file can be done with open(), but he wants to use print and not write. But I have to admit, it is quite unclear. Maybe he does not want to change his code at all, then best shot is to use terminal, or subprocess.
24
python file.py &> out.txt 

This will direct all the output including the errors to the out.txt.

5 Comments

This is perfect. Many answers missed this
What is the difference between using &> and just > ?
&> also includes the errors but > doesn't.
Do you know how I can write to the file instead of append to it? If i run this again the output of the previous file is not written over but append to
How about deleting the out.txt before calling this? rm out.txt; python file.py &> out.txt
22

IMO this is the correct pythonic way, with-out relying on the system shell:

import sys f = open("test.out", 'w') sys.stdout = f print "test" f.close() 

In python you can change what is the default stdout object. You just need to assign whatever you want to sys.stdout. I think the object just need to have a write method defined (not absolutely sure, though).

This would do:

import sys f = open("test.out", 'w') sys.stdout = f i=0 while i<10: a=raw_input('Write a number') print 'Result%s'%str(2*a) i+=1 f.close() 

It's essentially the same what 0605002 suggests for systems that support syntax he uses, but realized in pure python and should be more portable.

Even more pythonic way, as per comment suggestion:

import sys with open("test.out", 'w') as f: sys.stdout = f i=0 while i<10: a=raw_input('Write a number') print 'Result%s'%str(2*a) i+=1 

Of course you can refactor your "client code" and make it a function or something.

5 Comments

with statement not found!! :-)
@Aशwiniचhaudhary Yea I am not very good at python yet =(. But I did f.close()! =) I edited the question and added even more pythonic way. I hope everyone's content with the answer now.
OK, it is works. Thanks. I have got one more question: Is it possibility to save the result of terminal according to below sample of code import sys f = open("test.out", 'w') sys.stdout = f "on terminal execute some operation (due to run another script) and I save result from terminal to the file" f.close()
@qwerty12345 IMO, it's best to use subprocess for it. You can specify what will be used for stdin, stdout and stderr. You can look for "How can I run another script from python." there are several ways, and someone most probably already asked it. It is also possible to use import for this, I think.
is it possible to do both, print on terminal and store in file? on the terminal its possible, but I'd like to solve it purely with python

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.