Last active
March 28, 2018 09:32
-
-
Save hlzz/68767022a52aeca7a075c9c5e047fa0e to your computer and use it in GitHub Desktop.
List many many files from a folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Copyright 2016, Tianwei Shen, HKUST. | |
""" | |
A handy script to execute a command recursively in a folder, | |
such as 'ls' or 'rm' files. | |
""" | |
import os | |
def get_files_from_folder(folder_path, suffix, recursive=False, only_name=False, exclude=None): | |
file_list_tmp = os.listdir(folder_path) | |
file_list_tmp.sort() # sort the filenames | |
file_list = [] | |
if exclude is not None and os.path.abspath(folder_path) in exclude: | |
return file_list | |
for item in file_list_tmp: | |
full_path = os.path.join(folder_path, item) | |
if os.path.isfile(full_path): | |
is_file_suffix = False | |
for suffix_each in suffix: # check each suffix | |
is_file_suffix = is_file_suffix or item.endswith(suffix_each) | |
if is_file_suffix: | |
if only_name: | |
file_list.append(item) | |
else: | |
file_list.append(full_path) | |
if recursive and os.path.isdir(full_path): | |
file_list.extend(get_files_from_folder(full_path, suffix, recursive, only_name, exclude)) | |
return file_list | |
if __name__ == '__main__': | |
from argparse import ArgumentParser | |
parser = ArgumentParser() | |
parser.add_argument('--folder', dest='folder', type=str, nargs='+', required=True, | |
help='the folder to generate file list from') | |
parser.add_argument('--command', dest='command', type=str, default='ls', | |
help='path to save output') | |
parser.add_argument('--suffix', dest='suffix', type=str, nargs='+', default='.jpg', | |
help='the file suffix(es) to extract') | |
parser.add_argument('--exclude', dest='exclude', type=str, nargs='+', default=None, | |
help='exclude folders') | |
parser.add_argument('--only_name', dest='only_name', action='store_true', default=False, | |
help='only the name of the file (basename) is saved') | |
parser.add_argument('-r', dest='recursive', action='store_true', default=False, | |
help='get file names recursively') | |
args = parser.parse_args() | |
# format folder names | |
if args.exclude is not None: | |
for i in xrange(len(args.exclude)): | |
args.exclude[i] = os.path.abspath(args.exclude[i]) | |
for folder in args.folder: | |
file_list = get_files_from_folder(folder, args.suffix, args.recursive, args.only_name, args.exclude) | |
if args.command == 'ls': | |
for line in file_list: | |
print line | |
elif args.command == 'rm': | |
for line in file_list: | |
os.remove(line) | |
print 'rm', line | |
else: | |
print 'Not a known command' | |
exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment