Alex
Alex

Reputation: 1

UNIX shell script to call python

I have a python script that runs on three files in the following way align.py *.wav *.txt *.TextGrid However, I have a directory full of files that I want to loop through. The original author suggests creating a shell script to loop through the files. The tricky part about the loop is that I need to match three files at a time with three different extensions for the script to run correctly. Can anyone help me figure out how to create a shell script to loop through a directory of files, match three of them according to name (with three different extensions) and run the python script on each triplet? Thanks!

Upvotes: 0

Views: 1319

Answers (2)

alex vasi
alex vasi

Reputation: 5344

Assuming you're using bash, here is a one-liner:

for f in *.wav; do align.py $f ${f%\.*}.txt ${f%\.*}.TextGrid; done

Upvotes: 3

unutbu
unutbu

Reputation: 879561

You could use glob.glob to list only the wav files, then construct the subprocess.Popen call like so:

import glob
import os
import subprocess

for wav_name in glob.glob('*.wav'):
    basename,ext = os.path.splitext(wav_name)
    txt_name=basename+'.txt'
    grid_name=basename+'.TextGrid'
    proc=subprocess.Popen(['align.py',wav_name,txt_name,grid_name])
    proc.communicate()

Upvotes: 0

Related Questions