Skip to content

Instantly share code, notes, and snippets.



Last active Apr 19, 2016
What would you like to do?
Recursively search for and submit (qsub) files matching a pattern.
#!/usr/bin/env python
"""Recursively search for and submit (qsub) files matching a pattern."""
import os
import sys
import argparse
import subprocess
import fnmatch
def recursive_glob(rootdir='.', pattern='*'):
""" A function to search recursively for files matching a specified pattern.
Adapted from """
matches = []
for root, dirnames, filenames in os.walk(rootdir):
for filename in fnmatch.filter(filenames, pattern):
matches.append(os.path.join(root, filename))
return matches
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="""
Recursively search for and submit (qsub) files matching a pattern.
Search a specified directory for files matching a given pattern, and submit them
all via qsub.""")
help="glob pattern of files to be submitted.",
help="path to search for files matching the pattern.",
args = parser.parse_args()
pattern = args.pattern
search_dir = args.search_dir
# search for files
files_found = recursive_glob(rootdir=search_dir,
# make the paths absolute
files_found = [os.path.abspath(file) for file in files_found]
# report files found
print 'Found %d files matching: %s\n' % (len(files_found), pattern)
for file in files_found:
print file
# save the cwd
orig_dir = os.getcwd()
# navigate to directories and submit
# (navigating to the directories sets the $SGE_O_WORKDIR correctly)
for file in files_found:
path,filename = os.path.split(file)
# navigate to the directory
os.chdir(path)['qsub', filename])
# go back to the main directory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment