Toykan Ozdeger
Toykan Ozdeger

Reputation: 169

How to write to command line using python

I need to run a series of commands in command prompt and I want to automate this process. I can run a command in command prompt using Python code:

import os
os.system("start /B start cmd.exe @cmd /k {command}")

OR

import os
import subprocess
p = subprocess.Popen(["start", "cmd", "/k", "command"], shell = True)

However, after executing a command, I cannot write another command to the same command prompt. Is there a way to do this?

The thread Calling an external command in Python is similar but I don't think it explains how to write a new command to the same command prompt after the one before finishes executing

Also, as I understand running multiple bash commands with subprocess explains how to run commands in parallel not one after each other.

Upvotes: 6

Views: 15879

Answers (1)

Dr.Xp0rK
Dr.Xp0rK

Reputation: 1

you can insert the '&' symbol (or other symbols, such as '&&' for example:

p = subprocess.Popen(["start", "cmd", "/k", "cd Desktop && cd Programs"], shell = True)

Upvotes: -1

Related Questions