Python Forum
How can insert value in external command?
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can insert value in external command?
#1
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
Reply
#2
Try with:

os.system('mediacontent.exe -m {}'.format(lst[i]))
You can read more on string formatting here.
Reply
#3
(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))
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  managing command codes for external controller box Oolongtea 0 1,893 Sep-19-2019, 08:32 AM
Last Post: Oolongtea
  Insert a variable in a Python > Cellular AT command ElectronicsNut 1 2,044 Jul-07-2019, 02:26 PM
Last Post: joe_momma
  'videodigest' is not recognized as an internal or external command MM2018 2 2,734 Oct-12-2018, 02:43 PM
Last Post: MM2018
  Insert using psycopg giving syntax error near "INSERT INTO" olgethorpe 4 15,497 Jul-21-2017, 07:39 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020