Python Forum

Full Version: Add list item into executable command
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
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])
Works great, thank you