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

微笑、不失礼 提交于 2019-11-29 06:45:51

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);

This'll also work, due to directing standard out to the file output.txt before executing "file.py":

import sys

orig = sys.stdout
with open("output.txt", "wb") as f:
    sys.stdout = f
    try:
        execfile("file.py", {})
    finally:
        sys.stdout = orig

Alternatively, execute the script in a subprocess:

import subprocess

with open("output.txt", "wb") as f:
    subprocess.check_call(["python", "file.py"], stdout=f)

If you want to write to a directory, assuming you wish to hardcode the directory path:

import sys
import os.path

orig = sys.stdout
with open(os.path.join("dir", "output.txt"), "wb") as f:
    sys.stdout = f
    try:
        execfile("file.py", {})
    finally:
        sys.stdout = orig

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.

The simplest way to run a script and get the output to a text file is by typing the below in the terminal:

PCname:~/Path/WorkFolderName$ python scriptname.py>output.txt

*Make sure you have created output.txt in the work folder before executing the command.

file_open = open("test1.txt", "r")
file_output = open("output.txt", "w")

for line in file_open:
    print ("%s"%(line), file=file_output)

file_open.close()
file_output.close()

using some hints from Remolten in the above posts and some other links I have written the following:

from os import listdir
from os.path import isfile, join
folderpath = "/Users/nupadhy/Downloads"
filenames = [A for A in listdir(folderpath) if isfile(join(folderpath,A))]
newlistfiles = ("\n".join(filenames))
OuttxtFile = open('listallfiles.txt', 'w')
OuttxtFile.write(newlistfiles)
OuttxtFile.close()

The code above is to list all files in my download folder. It saves the output to the output to listallfiles.txt. If the file is not there it will create and replace it with a new every time to run this code. Only thing you need to be mindful of is that it will create the output file in the folder where your py script is saved. See how you go, hope it helps.

Shilpa Amarendrababu Sujatha

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.

You could also do this by going to the path of the folder you have the python script saved at with cmd, then do the name.py > filename.txt It worked for me on windows 10

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!