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

2024-09-19 20:30:56 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我现在正试图编写一个代码,用我刚刚创建的函数运行目录中的每个文件。问题是,目录中的文件将有三种类型(.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()

Tags: 文件代码import程序目录txtinput参数
1条回答
网友
1楼 · 发布于 2024-09-19 20:30:56

您可以使用glob获得具有特定后缀的所有文件的列表,例如all.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()

我不能百分之百肯定这是不是你的目的。如果您想允许不同的参数,请考虑将我使用的常量字符串设置为用户输入或命令行参数。我没有对此进行测试,但它可能看起来像这样:

    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文件,将文件名传递给对齐.py. 其他文件的文件名是使用basename命令basename $f .txt创建的,该命令从文件中删除后缀。然后添加.wav和.TextGrid后缀。你知道吗

我希望这能有所帮助,尽管实验室肯定早就结束了。你知道吗

相关问题 更多 >