fox
fox

Reputation: 13

Python script to run an exe-file on all files in a directory

I have a small .exe file that I'd like to run on all files in a given directory. It is launched as follows "./myprogram.exe file" and converts my files into a different data type.

Now I want to build a python script, that runs this exe on all files in a given directory, including all subdirectories. I am totally new to python, so I have no clue how to do this.

Does anyone have suggestions how to do this? Do I need python or any other script language in the first place?

Thanks a lot!

Upvotes: 1

Views: 3132

Answers (3)

Martin
Martin

Reputation: 6032

For a match of all files from a directory + sub directories, take a look at: Use a Glob() to find files recursively in Python?

To call your program, take a look at : How do I execute a program from python? os.system fails due to spaces in path

Upvotes: 1

Cédric Julien
Cédric Julien

Reputation: 80771

On a linux box, I would have told you to use a find call, but the .exe seems to tell you are on windows.

In python, you should use something like this

for root, dir, files in os.walk(path):
   for name in files:
      subprocess.call(["path_to_your_programm/myprogram.exe", os.path.join(root, name)]

Upvotes: 2

Gerrat
Gerrat

Reputation: 29700

You should be able to modify the example in the python docs for os.walk to do that.

To actually call your external program, you can use subprocess.call.

Upvotes: 0

Related Questions