First open the file and use ato add if you want to keep a record of all output / errors or use wto overwrite every time:
with open("stdout.txt","a+") as stdout:
subprocess.Popen('my command', cwd='my path', shell=True, stdout=stdout, stderr=stdout)
Using withwill automatically close your file.
source
share