Perform python script on every file selected in a folder

I have a python script that I need to execute on every file in a folder to perform a specific conversion.

The script needs to be handed the path to each file, as well as an output directory to save into.

python scriptname.py $inputFile -flags $outputDir

Output directory could be $sourcedir/converted/

Does anyone have an example of a script that can send each selected file through a

This will run the command on every selected file/folder:

python scriptname.py {filepath$} -flags {sourcepath$}/converted
1 Like

Thank you. This works well. However, it does pop up a command window for every conversion. Is there a way to repress the command window from popping up?

Add a line with @runmode:hide to suppress the command prompt entirely.

Or switch the button type to MS-DOS Batch Function to have just a single command prompt which shows all output in one place.

More detail and options here: https://www.gpsoft.com.au/help/opus12/index.html#!Documents/MSDOS_Batch_commands.htm

Ah, that is actually really helpful. Wonderful.

I guess my next question would be - instead of selecting all files in a folder, it would be nice to be able to select 10 parent folders, and have the script run for every file inside of the folder recursively. Possible?

You could do that with scripting. The default script, shown when a new script button is created, has an example of how to loop through folder content.

Of course, since you’re running a Python script, another option is to change that so it can be passed a folder name.

Generally, you want to interact with DO and its API as little as possible. The way to go is to read minimum amount of information you need from DO (like selected folder(s)), do all the stuff in python and then just set/create elements specific to DO (like open a tab, save layout etc).
Anything else is easier and better done in python, including things like metadata manipulation, image conversions etc etc.
In your case, you just read from DO selected folders (or have them passed as parameters, as above) and then use os.walk or Path.glob to traverse directory trees doing your stuff with files.