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

后端 未结 8 591
离开以前
离开以前 2020-12-10 14:31

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

8条回答
  •  失恋的感觉
    2020-12-10 15:16

    what your doing is checking the output of execfile('file.py') 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:
        subprocess.call(["python", "./script.py"], stdout=output);
    

提交回复
热议问题