Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #53566 > unrolled thread

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

Started byVenkatesh <venkatesh.totad@gmail.com>
First post2013-09-03 06:54 -0700
Last post2013-09-03 12:21 -0400
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Unable to redirect the subprocess CMD.exe to txt file. Please help !!! Venkatesh <venkatesh.totad@gmail.com> - 2013-09-03 06:54 -0700
    Re: Unable to redirect the subprocess CMD.exe to txt file. Please help !!! William Ray Wing <wrw@mac.com> - 2013-09-03 10:22 -0400
    Re: Unable to redirect the subprocess CMD.exe to txt file. Please help !!! random832@fastmail.us - 2013-09-03 12:21 -0400

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

FromVenkatesh <venkatesh.totad@gmail.com>
Date2013-09-03 06:54 -0700
SubjectUnable to redirect the subprocess CMD.exe to txt file. Please help !!!
Message-ID<3c50a8aa-261f-447d-b07c-cdad3a5465e0@googlegroups.com>
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??

[toc] | [next] | [standalone]


#53570

FromWilliam Ray Wing <wrw@mac.com>
Date2013-09-03 10:22 -0400
Message-ID<mailman.6.1378221752.5461.python-list@python.org>
In reply to#53566
On Sep 3, 2013, at 9:54 AM, Venkatesh <venkatesh.totad@gmail.com> wrote:

> 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??
> 
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list


I use the following in OS-X, should be very similar in Windows.

       import subprocess
       ping_result = subprocess.Popen(['ping', '-c1', '-t1', self.target_IP], stderr=subprocess.PIPE, stdout = subprocess.PIPE).communicate()[0]

self.target_IP is a string containing the IP address, which is defined elsewhere.
ping_result contains the complete returned string from the ping command.  Note the -c switch (count = 1 ping packet) and the -t switch (force exit after one second).
you will have to parse the returned string to extract the stats.

-Bill

[toc] | [prev] | [next] | [standalone]


#53573

Fromrandom832@fastmail.us
Date2013-09-03 12:21 -0400
Message-ID<mailman.8.1378225305.5461.python-list@python.org>
In reply to#53566

On Tue, Sep 3, 2013, at 9:54, Venkatesh wrote:
> 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'), ...)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web