Alexy Grabov
Alexy Grabov

Reputation: 151

write output to file from python subprocess call with 'start'

I'm trying to redirect an output from a shell window opened from a python script - to a file.

So, after some googling, I came up with this code:

file_output = open(MS_Log + 'LOGS.txt', 'w+')
subprocess.call('start ' + URL_logs, shell=True, stdout=file_output)

This does indeed opens a new shell window and runs the command I want in it. However, the output is printed on the screen (inside that new shell window) and not to the file.

I want it to be both - printed in the shell window and saved to file.

Now, the command I run there is continuous, it runs until stopped with Ctrl+C, so I think this might be the problem...? Maybe if it will be closed gracefully it will dump it's contents to the file?

Upvotes: 2

Views: 7310

Answers (2)

jDo
jDo

Reputation: 4010

OP, I adapted a good solution from this question.

It should show the output of your command in terminal and save it to file while running. That means it'll work for infinite loops as well. It doesn't use the shell=True option that leaves you open to shell injection (always set tight permissions on a script that uses shell=True if you're not just playing on your LAN at home). The solution has one weakness: it only works if there are newlines in your output; if not, nothing will be printed/written.

import subprocess

""" continuously print command output and append it to file while running """

def run(command):    
    popen = subprocess.Popen(command, stdout=subprocess.PIPE)
    return iter(popen.stdout.readline, b"")

filename = "subprocess_output"
f = open(filename, "a")

for line in run(["/bin/bash", "infinite_loop.sh"]):
    print(line), # the comma keeps python from adding an empty line
    f.write(line)

f.close()

Upvotes: 0

Miguel Jiménez
Miguel Jiménez

Reputation: 514

You can try to save the output in a variable and then write this in a file:

process = subprocess.Popen(command, stdout = subprocess.PIPE, shell = True)
(process_output,  error) = process.communicate()
file = open(path_of_file_output, "w")
file.write(process_output)
file.close()

Upvotes: 1

Related Questions