Python Forum
Add list item into executable command
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add list item into executable command
#1
What I am trying to do is get a list of files in a directory and run a command on each of those files.
The command needs the file name in there as to call the file and I would like to use the file name in the output file name.


import os
dirlist = os.listdir('C:\\Users\\user\\Desktop\\test\\logs') #this pulls the file names and puts it into 
                                                             #a list


                    # take each item in the list and execute the following command where dirlist is a 
                    #file name in my list
for dirlist in os.listdir():
    myCmd = "C:\\Users\\cmueller\\Desktop\\test\\logs\\this.exe -E [b]['dirlist'][/b] -c [b]['dirlist'][/b]_end_dump" # -E is the file to run command against and -c is the output #filename
    os.system(myCmd)
Reply
#2
Use subprocess.run() (for python >= 3.5). It is simpler than os.system()

import os
import subprocess as sp
program = "C:\\Users\\cmueller\\Desktop\\test\\logs\\this.exe"

for dirlist in os.listdir():
    outfile = dirlist + '_end_dump'
    sp.run([program, '-E', dirlist, '-c', outfile])
Reply
#3
Works great, thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Finding string in list item jesse68 8 1,871 Jun-30-2022, 08:27 AM
Last Post: Gribouillis
  how to easily create a list of already existing item CompleteNewb 15 3,541 Jan-06-2022, 12:48 AM
Last Post: CompleteNewb
  Remove an item from a list contained in another item in python CompleteNewb 19 5,731 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  count item in list korenron 8 3,472 Aug-18-2021, 06:40 AM
Last Post: naughtyCat
  Time.sleep: stop appending item to the list if time is early quest 0 1,879 Apr-13-2021, 11:44 AM
Last Post: quest
  How to run a pytest test for each item in a list arielma 0 2,369 Jan-06-2021, 10:40 PM
Last Post: arielma
  How do I add a number to every item in a list? john316 2 1,977 Oct-28-2020, 05:29 PM
Last Post: deanhystad
  Passing List of Objects in Command Line Python usman 7 3,173 Sep-27-2020, 03:45 PM
Last Post: ndc85430
  Ignoring a list item hank4eva 2 2,123 Aug-17-2020, 08:40 AM
Last Post: perfringo
  Select correct item from list for subprocess command pythonnewbie138 6 3,306 Jul-24-2020, 09:09 PM
Last Post: pythonnewbie138

Forum Jump:

User Panel Messages

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