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

后端 未结 8 611
离开以前
离开以前 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:24

    Use this instead:

    text_file = open('output.txt', 'w')
    text_file.write('my string i want to put in file')
    text_file.close()
    

    Put it into your main file and go ahead and run it. Replace the string in the 2nd line with your string or a variable containing the string you want to output. If you have further questions post below.

提交回复
热议问题