user2957951 user2957951 - 2 months ago 16x
Python Question

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

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

execfile ('')

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 ('') > ('output.txt')

All I get is this:



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

Thanks for your help


what your doing is checking the output of execfile('') against the string 'output.txt'

you can do what you want to do with subprocess

#!/usr/bin/env python
import subprocess
with open("output.txt", "w+") as output:["python", "./"], stdout=output);