Recursive if directory

This commit is contained in:
Antoine Van Elstraete 2019-05-17 18:32:58 +02:00 committed by Antoine Van Elstraete
parent 0111140907
commit 4834f8fa6e

21
exv.py
View File

@ -4,13 +4,15 @@
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from datetime import datetime from datetime import datetime
import json import json
from os.path import isfile from os.path import isfile, isdir
from os import walk
def extractor(input_files, start, end): def extractor(input_files, start, end, recursive):
if not isinstance(input_files, type(list())): if not isinstance(input_files, type(list())):
raise ValueError("Input files must be a list.") raise ValueError("Input files must be a list.")
exif_dict_list = [] exif_dict_list = []
files = []
usefull_exif = [ usefull_exif = [
"Image.Model", "Image.Model",
"Photo.LensModel", "Photo.LensModel",
@ -24,6 +26,16 @@ def extractor(input_files, start, end):
"Image.ImageLength", "Image.ImageLength",
"Photo.DateTimeOriginal" "Photo.DateTimeOriginal"
] ]
for item in input_files:
if isdir(item):
for (dirpath, dirnames, filenames) in walk(item):
for filename in filenames:
files.append("{}/{}".format(dirpath, filename))
if not recursive:
break
else:
files.append(item)
input_files = files
for input_file in input_files: for input_file in input_files:
exif_dict = {} exif_dict = {}
if not isfile(input_file): if not isfile(input_file):
@ -134,11 +146,13 @@ def extractor(input_files, start, end):
if __name__ == '__main__': if __name__ == '__main__':
import argparse import argparse
recursive = False
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument( parser.add_argument(
"infile", help="input file (EXV, JPG, CR2, ...). Can be more than one.", nargs='+') "infile", help="input file (EXV, JPG, CR2, ...). Can be more than one.", nargs='+')
parser.add_argument("-s", "--start-date", help="start date as YYYYMMDD. If omitted, begin of the collection.", const=None) parser.add_argument("-s", "--start-date", help="start date as YYYYMMDD. If omitted, begin of the collection.", const=None)
parser.add_argument("-e", "--end-date", help="end date as YYYYMMDD. If omitted, today", const=None) parser.add_argument("-e", "--end-date", help="end date as YYYYMMDD. If omitted, today", const=None)
parser.add_argument("-R", "--recursive", help="Walk throught directory to find all files", action='store_true')
args = parser.parse_args() args = parser.parse_args()
if not args.start_date: if not args.start_date:
args.start_date = "18250101" # Ok, that's the year of the 1st photography by Nicéphore Niépce. Shoul'd be enough for a start date. args.start_date = "18250101" # Ok, that's the year of the 1st photography by Nicéphore Niépce. Shoul'd be enough for a start date.
@ -150,4 +164,5 @@ if __name__ == '__main__':
except ValueError: except ValueError:
print("Date must be YYYYMMDD.") print("Date must be YYYYMMDD.")
exit() exit()
print(json.dumps(extractor(args.infile, datetime.strptime(args.start_date, "%Y%m%d"), datetime.strptime(args.end_date, "%Y%m%d")), indent=4, sort_keys=True)) recursive = args.recursive
print(json.dumps(extractor(args.infile, datetime.strptime(args.start_date, "%Y%m%d"), datetime.strptime(args.end_date, "%Y%m%d"), recursive), indent=4, sort_keys=True))