Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a python script and write output to txt file?

Tags:

python

output

I'm executing a .py file, which spits out a give string. This command works fine

execfile ('file.py')

But I want the output (in addition to it being shown in the shell) written into a text file.

I tried this, but it's not working :(

execfile ('file.py') > ('output.txt')

All I get is this:

tugsjs6555

False

I guess "False" is referring to the output file not being successfully written :(

Thanks for your help

like image 773
user2957951 Avatar asked Feb 23 '14 02:02

user2957951


1 Answers

If you are running the file on Windows command prompt:

python filename.py >> textfile.txt

The output would be redirected to the textfile.txt in the same folder where the filename.py file is stored.

The above is only if you have the results showing on cmd and you want to see the entire result without it being truncated.

like image 198
Shilpa Amarendrababu Sujatha Avatar answered Oct 12 '22 20:10

Shilpa Amarendrababu Sujatha