Python Forum
Add list item into executable 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: Add list item into executable command (/thread-23264.html)



Add list item into executable command - zhome888 - Dec-18-2019

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)



RE: Add list item into executable command - Gribouillis - Dec-18-2019

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])



RE: Add list item into executable command - zhome888 - Dec-19-2019

Works great, thank you