Skip to content

Instantly share code, notes, and snippets.

@brandonlou
Last active May 18, 2022 21:50
Show Gist options
  • Save brandonlou/d8112c8c55acf75c533ff2e5be551e29 to your computer and use it in GitHub Desktop.
Save brandonlou/d8112c8c55acf75c533ff2e5be551e29 to your computer and use it in GitHub Desktop.
Keep only exe files in a directory
import argparse
import filetype
import os
def main():
parser = argparse.ArgumentParser(description='Keep all exe files in a directory and remove the rest')
parser.add_argument('directory')
args = parser.parse_args()
directory = args.directory
for filename in os.listdir(directory):
filepath = f'{directory}/{filename}'
kind = filetype.guess(filepath)
if kind is None or kind.extension != 'exe':
os.remove(filepath)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment