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 datetime import datetime
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())):
raise ValueError("Input files must be a list.")
exif_dict_list = []
files = []
usefull_exif = [
"Image.Model",
"Photo.LensModel",
@ -24,6 +26,16 @@ def extractor(input_files, start, end):
"Image.ImageLength",
"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:
exif_dict = {}
if not isfile(input_file):
@ -134,11 +146,13 @@ def extractor(input_files, start, end):
if __name__ == '__main__':
import argparse
recursive = False
parser = argparse.ArgumentParser()
parser.add_argument(
"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("-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()
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.
@ -150,4 +164,5 @@ if __name__ == '__main__':
except ValueError:
print("Date must be YYYYMMDD.")
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))