简体   繁体   English

UNIX shell脚本调用python

[英]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. 我有一个以以下方式在三个文件上运行的python脚本align.py * .wav * .txt * .TextGrid但是,我有一个充满要循环浏览的文件的目录。 The original author suggests creating a shell script to loop through the files. 原始作者建议创建一个Shell脚本来循环浏览文件。 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? 谁能帮我弄清楚如何创建一个Shell脚本来循环遍历文件目录,根据名称匹配其中三个(具有三个不同的扩展名)并在每个三元组上运行python脚本? 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: 你可以使用glob.glob只列出wav文件,然后构建subprocess.Popen调用就像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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