Skip to content

Instantly share code, notes, and snippets.

@campenr
Created February 22, 2018 21:21
Show Gist options
  • Save campenr/93c548933a59fb7765b9798d1af4f802 to your computer and use it in GitHub Desktop.
Save campenr/93c548933a59fb7765b9798d1af4f802 to your computer and use it in GitHub Desktop.
Function for cleaning up mothur output directory based on current files and an optional whitelist.
def tidy_output_dir(mothur_obj, whitelist=list()):
"""
Removes files from the output directory that are not listed in the current_files attribute or a whitelist.
USE WITH CAUTION: This will delete any files in the mothur objects output directory (which may just be the current working
directory) which can have unintended consequences.
This function was breifly included in mothur-py.utils, but the danger of its accidental use deleting important files has
relegated it to a gist.
"""
# append current files to whitelist
for file in mothur_obj.current_files.values():
whitelist.append(file)
# tidy up environment to help save space
out_dir = mothur_obj.current_dirs.get('output', None)
# purge non-whitelist files from the output directory
for file in os.listdir(out_dir):
# conditionally correct filepath
if out_dir is not None:
file = os.path.join(out_dir, file)
# only process files, not directories
if os.path.isfile(file):
if file not in whitelist:
os.remove(file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment