0

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!

3
  • 1
    If there will be a wave and txt file for every TextGrid file, then I would just alter the python script to infer the names of those two, and only pass it the *.TextGrid list. Is it feasible for you to change the python script? Commented Feb 2, 2011 at 21:12
  • Please add some example files and how you'd want them to be processed. Your question doesn't sufficiently describe what you're looking for. Commented Feb 2, 2011 at 21:15
  • 1
    You don't need shell scripting at all; Python is able to look through directories itself.. Commented Feb 2, 2011 at 21:15

2 Answers 2

3

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

for f in *.wav; do align.py $f ${f%\.*}.txt ${f%\.*}.TextGrid; done
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.