python - Processing all files in a directory- different types -
so i'm trying write code right run every file in directory function made. thing is, files in directory of 3 types (.wav, .txt, and, output of program, .textgrid), , need passed in arguments in code. so, example, specific .wav file must go it's specific .txt file, make names .textgrid file. these files arguments i'm passing through subprocess runs program, forced aligner penn state. let me know if confused code or function.
also i'm new coding know code not efficient thing is. felt using input instead of argv easier instance, because don't know how specify there different amount of arguments each time (i'm trying make code more universal, purposes, there 1 program , 3 arguments each time.
import subprocess import sys def run_file(num_args): prog = input('enter program directory: ') args = input('enter arguments\' directories separated space: ').split(' ', len(num_args)-1) subprocess.call([prog, args]) def main(): run_file(sys.argv) if __name__ == '__main__': main()
you can use glob list of files suffix, example .txt files. can check if matching .wav file present , pass aligner.
import glob import subprocess import os def main(): # specify program prog = input('enter program directory: ') # no program given, use default program if not prog: prog = "./align.py" # compile list of .txt files in current directory # using glob wildcards all_txt_files = glob.glob("*.txt") txt_file in all_txt_files: # basename of txt file, triming off extension filename, ext = os.path.splitext(txt_file) # compile filenames wav , textgrid file wav_file = filename + ".wav" grid_file = filename + ".textgrid" # make sure wav file exists if os.path.exists(wav_file): # call program args = [txt_file, wav_file, grid_file] subprocess.popen([prog] + args) if __name__ == '__main__': main()
i not 100% sure if to. if allow different parameters, consider making constant strings used user input or command line argument. have not tested this, might this:
input_suffixes = input('enter input suffixes\' separated spaces').split(" ") if not input_suffixes: input_suffixes = ".txt .wav" output_suffixes = input('enter output suffixes\' separated spaces').split(" ") if not output_suffixes: output_suffixes = ".textgrid" first_suffix, *input_suffixes = input_suffixes all_files = glob.glob("*"+first_suffix) in_file in all_txt_files: filename, ext = os.path.splitext(txt_file) in_files = [filename+suffix suffix in input_suffixes] out_files = [filename+suffix suffix in output_suffixes] args = [in_file] + in_files + out_files subprocess.popen([prog] + args)
sidenote
if using linux machine might simpler (or @ least shorter) write bash script solve this:
for f in *.txt; python align.py $f `basename $f .txt`.wav `basename $f .txt`.textgrid; done
this iterates through .txt files, passes filename on align.py. filenames other files created using basename command basename $f .txt
trims off suffix file. .wav , .textgrid suffix added.
i hope helps, though lab, certainly, long over.
Comments
Post a Comment