I have created a function that parses a directory tree, and returns a list of lists that groups specific files within for later program specific parsing. I feel like it could be done better, considering I've used dummy variables, and was hoping some of you could help.
def get_output_files(path) -> list:
"""
This routine crawls through a directory tree, and checks for groups of job files, and groups based on:
.sh - run script file
.hin - hamiltonian creation input file
.in - iterate input file
.hout - hamiltonian creation output file
.out - iterate output file
to be used for parsing.
Checks for grouping based on base name of file, if file suffix does not exist, then returns None in place.
Example: directory "/home/test" contains the following files:
run.sh
run.hin
run2.in
run.hout
run2.out
will return the following list:
[
[run.sh, run.hin, None, run.hout, None],
[None, None, run2.in, None, run2.out]
]
"""
jobs = []
suffixes = ['.sh', '.hin', '.in', '.hout', '.out']
for root, dirs, files in os.walk(path):
for file in files:
base, end = file.rsplit('.', 1)
dummy_list = []
for suffix in suffixes:
if base + suffix in files:
dummy_list.append(os.path.join(root, base + suffix))
else:
dummy_list.append(None)
jobs.append(dummy_list)
real_jobs = []
for num, job in enumerate(jobs):
if not all(element is None for element in job):
real_jobs.append(job)
return jobs