By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,203 Members | 1,546 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,203 IT Pros & Developers. It's quick & easy.

Re: Newbie question about sending and receiving data to the commandprompt.

P: n/a
En Tue, 19 Aug 2008 16:48:26 -0300, aditya shukla
<ad**************@gmail.comescribi�:
I am using windows vista and i am trying to send data to the command
prompt
,this is what is done.

import subprocess
proc =subprocess.Popen('cmd.exe',stdin=subprocess.PIPE)
proc.communicate('abc')
when i run this the command prompt just blinks and disappears , can
anyone
explain what's going on?
I don't have a Vista machine to test right now, but I'd expect the above
sequence to hang indefinitely. If you want to execute something, don't
forget the final \n
On XP this works fine:

import subprocess
proc
=subprocess.Popen('cmd.exe',stdin=subprocess.PIPE, stdout=subprocess.PIPE)
output = proc.communicate('dir\n')[0]
print repr(output)

--
Gabriel Genellina

Aug 20 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Is it possible to communicate in loop fashion?

====================================
import subprocess as s
proc = s.Popen('cmd.exe', stdin=s.PIPE, stdout=s.PIPE)
while 1:
cmd = raw_input('cmd:')
res = proc.communicate(cmd + '\n')[0]
print res
====================================
cmd:dir c:
< res >

cmd:dir d:

Traceback (most recent call last):
File "D:\Python25\my_subprocess.pipe.py", line 11, in <module>
res = proc.communicate(cmd + '\n')[0]
File "D:\Python25\lib\subprocess.py", line 651, in communicate
return self._communicate(input)
File "D:\Python25\lib\subprocess.py", line 862, in _communicate
self.stdin.write(input)
ValueError: I/O operation on closed file

Aug 22 '08 #2

P: n/a
En Fri, 22 Aug 2008 18:25:49 -0300, n00m <n0**@narod.ruescribió:
Is it possible to communicate in loop fashion?

====================================
import subprocess as s
proc = s.Popen('cmd.exe', stdin=s.PIPE, stdout=s.PIPE)
while 1:
cmd = raw_input('cmd:')
res = proc.communicate(cmd + '\n')[0]
print res
====================================
Don't use communicate, it waits for the process to finish.
But you can write and read from the pipes. The hard part is to recognize *where* output from the previous command ends (so you don't read further). The code below uses #\n as the prompt:

import subprocess as s

def read_output(stdout):
while 1:
line = stdout.readline()
if not line: break
line = line.rstrip('\r\n')
if line=='#': break
yield line

proc = s.Popen('cmd.exe /k prompt #$_', stdin=s.PIPE, stdout=s.PIPE)
for line in read_output(proc.stdout): pass # skip over initial prompt
while 1:
cmd = raw_input('cmd:')
proc.stdin.write(cmd+'\n')
for line in read_output(proc.stdout):
print line

--
Gabriel Genellina

Aug 24 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.