开发者

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 l开发者_StackOverflowoop 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!


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

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜