0

I wanted to ask if there is a way to run the commands of a command line program in python, but the codes must be executed repeatedly, namely, the solution I want is not this

os.system(f"xxx.exe {command}").

I tried subprocess.run function putting the name of the exe followed by the commands I want to execute inside the brackets and then these keyword args: stdout=subprocess.PIPE,text=True however, oddly it doesn't make data.stdout the whole output for some reason. Only the initial code's output is assigned to that. It is probably because the arguments inside the brackets don't represent different lines. Therefore, I guess the thing I've done using subprocess is the same as how I executed a single line of command via the os library.

Namely, my question is using subprocess or os, how can I execute codes that must be executed in different lines, or if not possible, how to execute commands from a command-line program one after another in python?

Edit: Should I make something like this?

os.system(f"xxx.exe {command1} \n {command2}")
martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

0

It's kind of a shortcut but you could do something like this:

import os
import pyautogui

os.startfile('Path_to_command_prompt')

pyautogui.PAUSE = however long it takes to run each command

pyautogui.write('Command_1')
pyautogui.press('enter')

pyautogui.write('Command_2')
pyautogui.press('enter')
drake14w
  • 157
  • 9
  • I also have to record the output but there may be a way to do so. In order to use this function, it is mandatory to stop what I'm doing. Otherwise, errors may occur. However, apparently, it is the only way :(( – Aldin Baş Jun 30 '21 at 12:03