Python Popen writing command prompt output to logfile


Firstly I am running this Python script on Windows.

I am trying to open a new command prompt window every time and want to execute a .exe with some arguments. After I execute, I want to copy the command prompt output to a log file. I created a log file at the location say "log.log". When I run the script, it doesn’t seem to write the contents to the log file at all.

The coreServerFullPath is like C:UsersnandukDesktopSourceCodeCoreCoreServerServerCoreServerbinDebug

And here in this location I created a blank text document and named it to log.log

def OpenCoreServer():
    #logging.basicConfig(filename="log.log", level=logging.INFO)'your text goes here') #I see this line in the log.
    result = subprocess.Popen('start cmd /k "CoreServer.exe -c -s" >> log.log 2>&1', shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
    with open('log.log', 'r') as logg:
Asked By: nikhil



You might be able to get away with something like

import subprocess
import time

with open("log.log", "ab") as log:
    proc = subprocess.Popen("CoreServer.exe", stdout=log, stderr=log)
    while proc.poll() is None:
        # Do something while the process is running
    print("CoreServer exited with code", proc.returncode)
Answered By: AKX
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.