Python Forum
How can insert value in external command? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How can insert value in external command? (/thread-10790.html)



How can insert value in external command? - puneet102 - Jun-06-2018

I am trying to create a python script which will take input from text file and run an external command.

import os

with open('media.txt') as f:
    lst = [i.strip() for i in f]

length=len(lst)
print(length)

i=length
for x in range(i):
    os.system('mediacontent.exe -m lst[i]')
    print(lst[i])
    i+=1
Please help me to insert the value of lst[i] in the command.

The content of file media.txt is as below:

1234
6753
9897
5656


RE: How can insert value in external command? - j.crater - Jun-06-2018

Try with:

os.system('mediacontent.exe -m {}'.format(lst[i]))
You can read more on string formatting here.


RE: How can insert value in external command? - volcano63 - Jun-06-2018

(Jun-06-2018, 07:45 PM)puneet102 Wrote:
...
for x in range(i):
    os.system('mediacontent.exe -m lst[i]')
    print(lst[i])
    i+=1

This is extremely inefficient - and unPythonic - way to iterate over list

for value in lst:
    os.system('mediacontent.exe -m {}'.format(value))