Skip to content

Instantly share code, notes, and snippets.

@raczben
Forked from 89465127/argparse_files.py
Last active November 20, 2020 08:40
Show Gist options
  • Save raczben/7fbc803097485e283215c52cb705c243 to your computer and use it in GitHub Desktop.
Save raczben/7fbc803097485e283215c52cb705c243 to your computer and use it in GitHub Desktop.
Get a list of filenames using argparse (updated to use default formatter)
import argparse
import os
import glob
def main():
#Does not currently have support to read files from folders recursively
parser = argparse.ArgumentParser(description='Read in a file or set of files, and return the result.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('path', nargs='+', help='Path of a file or a folder of files.')
parser.add_argument('-e', '--extension', action='append', default=[''], help='File extension to filter by.')
args = parser.parse_args()
# Parse paths
full_paths = [os.path.join(os.getcwd(), path) for path in args.path]
files = set()
for path in full_paths:
if os.path.isfile(path):
files.add(path)
else:
for ext in args.extension:
files |= set(glob.glob(path + '/*' + ext))
for f in files:
print f
if __name__ == '__main__':
main()
@raczben
Copy link
Author

raczben commented Nov 20, 2020

Updated, with multiple extension

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment