简体   繁体   中英

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM