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.
source
share