Last active
August 7, 2020 22:07
-
-
Save trolleway/aa830ba09f4f963e21c4 to your computer and use it in GitHub Desktop.
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/python | |
import os | |
import datetime | |
import time | |
def get_args(): | |
import argparse | |
p = argparse.ArgumentParser(description='Move images to folder with his date') | |
p.add_argument('path', help='Path to folder containing JPG files') | |
return p.parse_args() | |
def _get_if_exist(data, key): | |
if key in data: | |
return data[key] | |
return None | |
def get_photo_date(filepath): | |
file_date = datetime.datetime.fromtimestamp(os.path.getmtime(filepath)) | |
return file_date | |
def move_file_to_folderdate(filepath,file_date): | |
foldername=file_date.strftime("%Y-%m-%d") | |
print("Move file {0} to date {1}.".format(filepath, foldername)) | |
directory = os.path.dirname(os.path.abspath(filepath)) | |
directory = os.path.join(directory,foldername) | |
new_filename = os.path.join(directory,os.path.basename(filepath)) | |
print(new_filename) | |
if not os.path.exists(directory): | |
os.makedirs(directory) | |
os.rename(filepath, new_filename) | |
if __name__ == '__main__': | |
args = get_args() | |
if args.path.lower().endswith(".jpg"): | |
# single file | |
file_list = [args.path] | |
else: | |
# folder(s) | |
file_list = [] | |
for root, sub_folders, files in os.walk(args.path): | |
file_list += [os.path.join(root, filename) for filename in files if filename.lower().endswith(".jpg")] | |
print("===\nStarting move of {0} images using {1}.\n===".format(len(file_list), args.path)) | |
for filepath in file_list: | |
file_date=None | |
file_date=get_photo_date(filepath) | |
move_file_to_folderdate(filepath, file_date) | |
print("Done move {0} images.".format(len(file_list))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment