The python subprocess is the rewriting file used for stdout - I need it to be added to the file (windows)

I want to add STDOUTfrom subprocess.call()to an existing file. My code below overwrites the file -

log_file = open(log_file_path, 'r+')
cmd = r'echo "some info for the log file"'
subprocess.call(cmd, shell=True, stdout=log_file, stderr=STDOUT)
log_file.close()

I am looking for an equivalent >>in subprocess.call()or subprocess.Popen(). It drives me crazy trying to find it.

UPDATE:

After the answers so far I have updated my code to

import subprocess

log_file = open('test_log_file.log', 'a+')
cmd = r'echo "some info for the log file\n"'
subprocess.call(cmd, shell=True, stdout=log_file, stderr=subprocess.STDOUT)
log_file.close()

I run this code from the command line in windows -

C:\users\aidan>test_subprocess.py

This adds text to the log file. When I run the script again, nothing new is added. This seems to be overwriting the file.

+3
source share
2 answers

Use "app" instead:

log_file = open(log_file_path, 'a+')

, , Windows , ; "r +" "w" :

import os

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

log_file_path. "r +". 'a' append 'r +':

log_file = open(log_file_path, 'a+')
+4

All Articles