Costi
Costi

Reputation: 1

Send commands to cmd prompt using Python subprocess module

using python 3.7

I have the following issue: I wrote a python script in which I open a cmd prompt, do some actions then I want to send some commands to that opened cmd prompt

To simplify, it looks something like:

import subprocess
process = subprocess.Popen(['start','cmd','/k','dir'], shell = True, stdin= subprocess.PIPE, 
stdout = subprocess.PIPE, text = True)

"DO some actions"

input = 'date'
process.stdin.write(input)
process.communicate(input, timeout = 10)

All the time the script exits with exception TimeoutExpired , and in the cmd prompt i do not see command written (the input)

I looked in the documentation, but i am new with python and did not understood very well how to use the subprocess module

Thank you for the support!

Upvotes: 0

Views: 2620

Answers (1)

If you want to write something like date in another cmd tab, do like this:

import subprocess
input = 'date'
subprocess.Popen(['start','cmd','/k','echo',input], shell = True, stdin = subprocess.PIPE, stdout = subprocess.PIPE, text = True)

Result:
enter image description here

Upvotes: 1

Related Questions