0

I have the following code:

import os

commands = [r"cd c:\bugs\*722857\*722857 && chrome.exe --no-sandbox --single-process ..\..\cr-1195650-cve-2021-30588-stepped.html",
            r"cd c:\bugs\*722857\*722857 && chrome.exe --no-sandbox --single-process --js-flags=-expose-gc ..\..\cr-1057593-cve-2020-6428-stepped.html",
            r"cd c:\bugs\*722857\*722857 && chrome.exe --no-sandbox --single-process ..\..\cr-1032000-stepped.html",            
            ]

for i in commands:
    os.system(i)

I would like to save the output of each command, either as a string or list of strings. Any advice?

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
NetMax
  • 9
  • 1

2 Answers2

0

You need to have a look at subprocess module. Either subprocess.getstatusoutput or subprocess.getoutput is matching your requirement. If you just want to save the output, the return value from subprocess.getoutput is enough.

Ji Bin
  • 491
  • 3
  • 9
  • When running with subprocesses, either with check_output or popen, I get a file not found error, so I was hoping to use os if possible – NetMax Mar 20 '22 at 07:28
  • If you are creating Popen object by subprocess.Popen, please note, the argument `shell` need to set True, as your input are some commands. like proc = subprocess.Popen('cd c:\somelocation\ && ...', shell=True, ...) – Ji Bin Mar 20 '22 at 07:34
  • when using something like: for i in commands: out = subprocess.Popen(i, shell=True) it only runs one command – NetMax Mar 20 '22 at 08:15
  • Based on your code, does chrome.exe is your %PATH% envirement? if not, I think it will raise 'FileNotFound'. – Ji Bin Mar 21 '22 at 02:31
0

Replace os.system(i) with output = os.popen(i).read(), as the first one only passes command to the system, while the second creates a pipe to communicate with whatever is executed.

vladko312
  • 99
  • 6