简体   繁体   中英

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!

假设您正在使用bash,这是一个单行代码:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM