Unable to redirect the subprocess CMD.exe to txt file. Please help !!!


V

Venkatesh

Hello comp.lang.python Group,

I am trying to invoke a subprocess in Python as below

import sys
import time
import os
import subprocess
DETACHED_PROCESS = 0x00000008

path = r'C:\Windows\System32\cmd.exe /k ping www.google.com -n 4 >> temp.txt'
p = subprocess.Popen("%s"%(path), stdout = subprocess.PIPE, stderr = subprocess.STDOUT, creationflags=DETACHED_PROCESS)

With this code, unable to invoke the subprocess and hence not able to store the Ping statistics in the file.

Any help on this OR any better way of achieving this??
 
Ad

Advertisements

R

random832

Hello comp.lang.python Group,

I am trying to invoke a subprocess in Python as below

import sys
import time
import os
import subprocess
DETACHED_PROCESS = 0x00000008

path = r'C:\Windows\System32\cmd.exe /k ping www.google.com -n 4 >>
temp.txt'
p = subprocess.Popen("%s"%(path), stdout = subprocess.PIPE, stderr =
subprocess.STDOUT, creationflags=DETACHED_PROCESS)

With this code, unable to invoke the subprocess and hence not able to
store the Ping statistics in the file.

Any help on this OR any better way of achieving this??

You should use /c, instead of /k. /k creates an interactive prompt.

I think you're being a bit overly complicated though by invoking cmd at
all. What about this?

subprocess.Popen("ping www.google.com -n 4",
stdout=open('temp.txt','a'), ...)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top