Saving Stdout from Subprocess.Popen to File, Plus Writing More Stuff to the File

Saving stdout from subprocess.Popen to file, plus writing more stuff to the file

You could call .wait() on each Popen object in order to be sure that it's finished and then call log.flush(). Maybe something like this:

def run(cmd, logfile):
p = subprocess.Popen(cmd, shell=True, universal_newlines=True, stdout=logfile)
ret_code = p.wait()
logfile.flush()
return ret_code

If you need to interact with the Popen object in your outer function you could move the .wait() call to there instead.

Capture output from subprocess.Popen commands that execute further processes

Apologies, probably as expected, this was user error. There is an if/else that determines whether the output goes to a logfile or terminal that wasn't being triggered correctly. So the example code I originally posted wasn't actually being touched. I should have added a bunch of print statements to see this fact earlier... It is working as expected now.

Python write stdout to log file

You could try to handle the response you got in stdout with regex and get your average time. Or you could use a python module and do it by youself like it's described in this thread Ping a site in Python?

Edit:
The simplest way would be with regex. I have modified your code snipet a bit :

import subprocess, re

list_of_ips = ["facebook.com", "google.com"]
for ip in list_of_ips:
ping_process = subprocess.Popen(['ping', '-c', '5', ip], stdout=subprocess.PIPE)
stdout = ping_process.stdout.read()
match = re.search(r'\d*\.\d*\/(\d*\.\d*)\/\d*\.\d*\/\d*\.\d*', stdout)
avg = match.group(1)
print('{}: {}ms'.format(ip, avg))

Edit2:
OK now it should fit ;)

import subprocess, re, csv

list_of_ips = ["facebook.com", "google.com"]
avgTimeList = []
for ip in list_of_ips:
ping_process = subprocess.Popen(['ping', '-c', '5', ip], stdout=subprocess.PIPE)
stdout = ping_process.stdout.read()
match = re.search(r'\d*\.\d*\/(\d*\.\d*)\/\d*\.\d*\/\d*\.\d*', stdout)
avg = match.group(1)
avgTimeList.append([ip, avg])

with open('avgTimes.csv', 'w') as csvfile:
csvWriter = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
for data in avgTimeList:
csvWriter.writerow(data)

python subprocess is overwriting file used for stdout - I need it to append to the file (windows)

Use the 'a' append mode instead:

log_file = open(log_file_path, 'a+')

If you still see previous content overwritten, perhaps Windows needs you to explicitly seek to the end of the file; open as 'r+' or 'w' and seek to the end of the file:

import os

log_file = open(log_file_path, 'r+')
log_file.seek(0, os.SEEK_END)


Related Topics



Leave a reply



Submit