Python 处理目录中的所有文件-不同类型

Python 处理目录中的所有文件-不同类型,python,python-3.x,Python,Python 3.x,所以我现在正试图编写一个代码,用我刚刚创建的函数运行目录中的每个文件。问题是,目录中的文件将有三种类型(.wav、.txt和,.TextGrid),并且需要在代码中作为参数传入。因此,例如,特定的.wav文件必须与特定的.txt文件一起使用,才能生成一个特定的names.TextGrid文件。这些文件是我正在通过一个子进程传递的参数,该子进程实际上运行另一个程序,来自宾夕法尼亚州立大学的强制对齐器。如果您对代码或其功能有任何疑问,请告诉我 另外,我对编码是新手,所以我知道我的代码可能不是最有效的

所以我现在正试图编写一个代码,用我刚刚创建的函数运行目录中的每个文件。问题是,目录中的文件将有三种类型(.wav、.txt和,.TextGrid),并且需要在代码中作为参数传入。因此,例如,特定的.wav文件必须与特定的.txt文件一起使用,才能生成一个特定的names.TextGrid文件。这些文件是我正在通过一个子进程传递的参数,该子进程实际上运行另一个程序,来自宾夕法尼亚州立大学的强制对齐器。如果您对代码或其功能有任何疑问,请告诉我

另外,我对编码是新手,所以我知道我的代码可能不是最有效的。我觉得在这个例子中使用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()
您可以使用获取具有特定后缀的所有文件的列表,例如所有.txt文件。然后您可以检查是否存在匹配的.wav文件,然后将其传递给对齐器

import glob
import subprocess
import os

def main():
    # specify program
    prog = input('Enter the program directory: ')
    # no program is given, use a default program
    if not prog:
        prog = "./align.py"

    # compile a list of all .txt files in the current directory
    # using glob wildcards
    all_txt_files = glob.glob("*.txt")

    for txt_file in all_txt_files:
        # get the basename of the txt file, triming off the extension
        filename, ext = os.path.splitext(txt_file)
        # compile filenames for wav and textgrid file
        wav_file = filename + ".wav"
        grid_file = filename + ".TextGrid"
        # make sure the wav file exists
        if os.path.exists(wav_file):
            # call the program
            args = [txt_file, wav_file, grid_file]
            subprocess.Popen([prog] + args)


if __name__ == '__main__':
    main()

我不是100%确定如果这是你想做的。如果你想允许不同的参数,考虑我使用了一个常数字符串,我使用了一个用户输入或命令行参数。我没有测试过,但是它看起来有点像:

    input_suffixes = input('Enter input suffixes\' separated by spaces').split(" ")
    if not input_suffixes:
        input_suffixes = ".txt .wav"

    output_suffixes = input('Enter output suffixes\' separated by spaces').split(" ")
    if not output_suffixes:
        output_suffixes = ".TextGrid"

    first_suffix, *input_suffixes = input_suffixes    
    all_files = glob.glob("*"+first_suffix)

    for in_file in all_txt_files:
        filename, ext = os.path.splitext(txt_file)
        in_files = [filename+suffix for suffix in input_suffixes]
        out_files = [filename+suffix for suffix in output_suffixes]

        args = [in_file] + in_files + out_files
        subprocess.Popen([prog] + args)
旁注 如果您使用的是linux机器,那么编写bash脚本来解决此问题可能会更简单(或至少更短):

for f in *.txt; do
  python align.py $f `basename $f .txt`.wav `basename $f .txt`.TextGrid;
done
这将遍历所有.txt文件,将文件名传递到align.py。其他文件的文件名是使用basename命令
basename$f.txt
创建的,该命令将从文件中删除后缀。然后添加.wav和.TextGrid后缀


我希望这会有所帮助,尽管实验室肯定已经结束很久了。

那么基本上你想接收两个扩展名为.wav和.txt的文件,并创建第三个.TextGrid文件,分别以前两个文件的名称命名吗?@JayT.sort,我将使用强制对齐器处理这些文件,强制对齐器是另一个将音频转换成脚本并生成一个文件(文本网格),该文件可由程序praat读取。这是为语言学实验室准备的。