Python's os.system within a for loop: how to wait for a command to finish before re-starting the loop?

1.6k Views Asked by At

I am new in using os.system (in Python 3) and am trying to execute a program that I would normally run in a Linux terminal. I need to iterate a few times and am using something like the following loop:

for item in item_list:
    os.system("gnome-terminal -- program_execute "+item) 

The problem is that this loop simutaneously opens n terminal windows (the length of my item_list is n) and executes all of them at the same time. My question is:

How could I run the loop len(item_list) times with the next run starting only after the current one finishes?

I can't use sleep() because the running time varies from item to item and I would like to optimize the process.

EDIT: I've tried using .communicate() and .wait() without success. I would like os.system (or os.subprocess) to understand that the gnome-terminal is still running, only passing by the next element in the loop after gnome-terminal from previous loop has closed.

1

There are 1 best solutions below

0
On

I used subprocess with the code below and it worked, i.e. the software runs once and finishes completely before going to the next loop.

for item in item_list:
    subprocess.call(["gnome-terminal","--disable-factory","--","my_command"])