所以我现在正试图编写一个代码,用我刚刚创建的函数运行目录中的每个文件。问题是,目录中的文件将有三种类型(.wav,.txt,和程序的输出,.TextGrid),并且需要在代码中作为参数传入。因此,例如,特定的.wav文件必须与特定的.txt文件一起使用,才能生成一个特定的names.TextGrid文件。这些文件是我正在通过一个实际运行另一个程序的子进程传递的参数,这个程序是来自Penn State的Forced Aligner。如果您对代码或其功能有任何疑问,请告诉我。你知道吗
另外,我是新的编码,所以我知道我的代码可能不是最有效的事情,因为它是。我觉得在这个例子中使用input而不是argv会更容易,主要是因为我不知道如何指定每次可能有不同数量的参数(我试图使这个代码更通用,但出于我的目的,每次只有1个程序和3个参数)。你知道吗
import subprocess
import sys
def run_file(num_args):
prog = input('Enter the program directory: ')
args = input('Enter the arguments\' directories separated by a space: ').split(' ', len(num_args)-1)
subprocess.call([prog, args])
def main():
run_file(sys.argv)
if __name__ == '__main__':
main()
您可以使用glob获得具有特定后缀的所有文件的列表,例如all.txt文件。然后可以检查是否存在匹配的.wav文件,然后将其传递给对齐器。你知道吗
我不能百分之百肯定这是不是你的目的。如果您想允许不同的参数,请考虑将我使用的常量字符串设置为用户输入或命令行参数。我没有对此进行测试,但它可能看起来像这样:
旁注
如果您使用的是linux机器,那么编写bash脚本来解决这个问题可能会更简单(或者至少更短):
它遍历所有.txt文件,将文件名传递给对齐.py. 其他文件的文件名是使用basename命令
basename $f .txt
创建的,该命令从文件中删除后缀。然后添加.wav和.TextGrid后缀。你知道吗我希望这能有所帮助,尽管实验室肯定早就结束了。你知道吗
相关问题 更多 >
编程相关推荐