1

I want to insert a python script, (which just print out input) inside for loop. Problem: in place of taking "i" as a variable - it just prints "i", not "string1", "string2". I tried:

for i in ['string1, string2']:
    os.system("python scriptWhichPrintsInput.py i")
...
i
3
  • 1
    you are using i as a hardcoded string. not as a variable Commented Mar 3, 2019 at 11:07
  • 5
    i should also add, you shouldn't be using os.system to run other python scripts. Take a look at importing other scripts as modules. Commented Mar 3, 2019 at 11:07
  • 1
    I agree with Paritosh Singh. What is the point to call a python module using os.system ? Use import instead. Commented Mar 3, 2019 at 11:17

2 Answers 2

3

You should use format or string concatenation:

os.system("python scriptWhichPrintsInput.py " + i)

Or:

os.system("python scriptWhichPrintsInput.py {}".format(i))

See docs for Python 2 and for Python 3

Sign up to request clarification or add additional context in comments.

5 Comments

Or with new f-strings os.system(f"python scriptWhichPrintsInput.py {i}")
Thanks @ozlevka . I am coming from the world of bash, so I would like to ask this: if I have string1 and string2 written in text file - myFile, what will be a parallel to "for i in $(cat myFile)...."
That's not good style in Bash either. The way to translate while read -r i ... done <myFile to Python is with open('myFile') as f: for line in f: i=line.rstrip('\n') ...
Out of curiosity, would this method lead to an appreciable difference in performance? I would assume so if you're not explicitly importing a module and performing a file lookup each loop....
This is a very good question. Want to publish it as StackOverflow question? Or I can do it?
2

Not too sure why you would want to do it this way, but here goes.

The TLDR:

First, you'll want to parse the argument dynamically at runtime (not just the char 'i' in your string!) So try,

for i in ['string1', 'string2']:
    os.system("python scriptWhichPrintsInput.py {}".format(i)) 
...
i
  • see: the docs here on string formatting.
  • also fixed (I assume typo) on your strings by closing the apostrophes.

Then, I would assume, your other python script is some function, foo(), that takes an input, i, such as;

foo(i, *args, **kwargs):
    # insert your script function here...
    pass

The Long Way:

This is what I'd probably do instead.

Rather than parsing a string representation of my script with the os dependency, I'd make the script importable (call it scriptWhichPrintsInput.py) then just make sure it's on my Python path (read more here). Such as,

# your scriptWhichPrintsInput.py file
foo(i, *args, **kwargs):
    # do something with your input...
    print(i)
    return

Then in your other script (as above),

# your scriptWhichPrintsInput.py file
from .scriptWhichPrintsInput import foo if your script is in the same folder, easy!

for i in ['string1', 'string2']:
    foo(i)
...
i # do whatever you need to do here!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.