According to the documentation if I use open(“file”,”a”) and write to the file the new data would be appended, but in the example below the second command just overwrites the file. I don’t quite get why.
import subprocess
startupinfo = subprocess.STARTUPINFO()
subprocess.STARTF_USESHOWWINDOW = 1
startupinfo.dwFlags = subprocess.STARTF_USESHOWWINDOW
with open(r"c:\folder\test.txt","a") as log:
Process = subprocess.Popen(['dir'],
stdout = log, stderr = log,
startupinfo = startupinfo,
shell=True)
with open(r"c:\folder\test.txt","a") as log:
Process = subprocess.Popen(['dir'],
stdout = log, stderr = log,
startupinfo = startupinfo,
shell=True)
I already tried mode “a+b”, but I get the same end result.
Within the
subprocessthe file position is not increased.log.tell()returns0in the secondwithstatement. You can incease the position oflogto the end of the file. And it seems to be good towait()for the firstProcess. Following works for me: