Help with Python. Please and thanks.

Hi everybody,

I've been experimenting with Python lately and for the most part it's been a smooth ride. I have one little problem that maybe one of you can help me with.

PROBLEM:
I have list with one word per line.
EXAMPLE
apples
oranges
pears
grapes
etc...

I also have a shell command containing a variable that I want to execute within the python code.
EXAMPLE
"do something with this $variable"

I need to execute the command for each item on the list. with each item on the list being equal to the variable in consecutive sequence.

Any help would be much appreciated :slight_smile:

Something around those lines:

#!/usr/bin/env python
import os

# assume we got the list from somewhere
list = ["apple", "oranges", "pears", "grapes"]

for fruit in list:
    os.system("./myscript "+fruit); # call myscript with corresponding fruit

HTH, Lo�c

1 Like
#! /usr/bin/env python

from __future__ import with_statement
import os

with open("../tmp/ordered_list.txt") as f:
    for list_item in f:
        myCommand = "THIS IS WHERE I TYPE MY COMMAND"+list_item
        os.system(myCommand)
        break