Prog1020
Prog1020

Reputation: 4781

Example to call subprocess with cmd.exe

I want/try to run cmd.exe with 2 commands: "ver", then "exit",

from subprocess import Popen, PIPE
def do_process(command, text):
    pro = Popen(command, stdout=PIPE, stdin=PIPE, stderr=PIPE)
    out, err = pro.communicate(text.encode())
    return out

do_process(['cmd.exe'], 'ver\nexit\n')

it cannot return the output, seems cmd.exe is not ended. What is ok way.

Upvotes: 0

Views: 186

Answers (2)

Prog1020
Prog1020

Reputation: 4781

I made err inside my function, I need to decode result (I use cp866 on my OS):

def do_process(command, text):
    enc = 'cp866'
    p = Popen(command, stdout=PIPE, stdin=PIPE, stderr=PIPE)
    out, err = p.communicate(text.encode())
    return out.decode(enc)

Upvotes: 0

Andrew Dunai
Andrew Dunai

Reputation: 3129

It's not a very reliable method calling cmd.exe and I'm almost sure you won't be able to communicate with it properly.

In case you want to retrieve system version, you might use this:

Called on my machine

>>> import platform
>>> platform.platform()
'Linux-3.17.4-1-ARCH-x86_64-with-glibc2.2.5'

Called on some Windows machine

>>> import platform
>>> platform.platform()
'Windows-7-6.1.7601-SP1'

Upvotes: 1

Related Questions