(office-04) VS python mobile photos and videos are sorted by date folder

Posted by daijames on Wed, 02 Mar 2022 12:01:06 +0100

Background needs

As a kindergarten teacher, I take a lot of photos of my daily life every day, so my mobile phone often prompts "insufficient memory". Before 2020, the method adopted by Asha is as follows:

1.QQ - my file assistant - export mobile photo album

 2. Create a new folder and manually drag and drop categories according to the date

3. Operation trouble - Manual photo sorting is too inefficient

Usually organize mobile phone photos once a month. You need to create 30 folders at one time and manually modify the date, and select and drag photos of the same day through visual inspection. It is easy to drag photos or folders. After months of sorting, I'm very tired of this inefficient and mechanical repetition.

2, Practical test

Through nearly a year's research, in the process of realizing my sorting requirements, I found two photo classification codes that can execute successfully in CSDN. The technology is limited. I can't understand the meaning of the code and can't modify it, so I can only apply it completely. Therefore, in practical application, they have shown their strengths and weaknesses that are difficult for me to crack.

(1) First:

This is the first photo sorting code I use. It can copy photos from source documents to new documents. The problems are: 1. Photos can be transferred, but videos cannot be transferred. 2. Original file not deleted

(2) second:

On the basis of the former, I found a program that can delete the original document and transfer photos and videos. The problem is: 1. I don't need sub classification. Even if all sub classifications are hidden, the exported pictures and videos are still saved in the "2022-01-02-NONE" file (I want the pictures and videos to be directly stored in the 2022-01-02 file)

3, Application process

From May 2021 to February 2022, I used the photo classification code of blogger lidashent. The original code is as follows:

# --------
# Copyright notice: This article is the original article of CSDN blogger "lidashent", which follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this notice for reprint.
# Original link: https://blog.csdn.net/lidashent/article/details/113919375
# --------

import os,shutil
import time
import datetime
allFileNum = 0
myfile=[]
mydir=[]
movie_file_format=['avi','mpeg','mp4','mov','ProRes','DNxHR','mfx','mkv','wmv','flv','rmvb','webm','asf']
text_file_format=['DOC','PDF','HTML','TXT','HTL','DOCX']
img_file_format=['bmp','jpg','jpeg','png','tif','gif','pcx','tga','exif','fpx','svg','psd',
                 'cdr','pcd','dxf','ufo','eps','ai','raw','WMF','webp','avif','hdri','flic','emf','ico']
zip_file_format=['rar' ,'zip','7z','CAB','ARJ','LZH','TAR','GZ','ACE','UUE','BZ2','JAR','ISO','MPQ']
music_file_format=['PCM','WAV','AIFF','MP3','AAC','OGG','WMA','FLAC','ALAC','WMA']
this_folder=input("Original path:").replace("\\",'/')
# this_folder='D: photo export '
those_folder=input("Destination path:").replace("\\",'/')
# those_folder='D: photo sorting '
def printPath(level, path):
    global allFileNum
    '''''
    Print all folders and files in a directory
    '''
    # For all folders, the first field is the level of the secondary directory
    dirList = []
    # All documents
    fileList = []
    # Returns a list containing the names of the entries in the directory (google translation)
    files = os.listdir(path)
    # Add directory level first
    dirList.append(str(level))
    for f in files:
        if (os.path.isdir(path + '/' + f)):
            # Exclude hidden folders. Because there are too many hidden folders
            if (f[0] == '.'):
                pass
            else:
                # Add non hidden folder
                dirList.append(f)
                mydir.append(path + '/' + f)
        if (os.path.isfile(path + '/' + f)):
            # Add file
            fileList.append(f)
            myfile.append(path + '/' + f)
    # When a flag is used, the first level of the folder list is not printed
    i_dl = 0
    for dl in dirList:
        if (i_dl == 0):
            i_dl = i_dl + 1
        else:
            # print("obtained folder", '-' * (int (dirlist [0]), DL)
            # Print all folders and files under the directory, directory level + 1
            printPath((int(dirList[0]) + 1), path + '/' + dl)
    for fl in fileList:
        # print("obtained file path", '-' * (int (dirlist [0]), FL)
        # Just calculate how many files there are
        allFileNum = allFileNum + 1
#File processing
def judge_file(oldPath,location):
    def TimeStampToTime(timestamp):
        timeStruct = time.localtime(timestamp)
        return time.strftime('%Y-%m-%d %H:%M:%S', timeStruct)
    def get_file_format(file_path):
        file_name=file_path.split("/")[-1]
        if file_name.find(".")>0:
            file_format=file_name.split('.')[-1]
            if file_format.lower() in movie_file_format or file_format.upper() in movie_file_format:
                return "video"
            elif file_format.lower() in text_file_format or file_format.upper() in text_file_format:
                return "text"
            elif file_format.lower() in img_file_format or file_format.upper() in img_file_format:
                return "picture"
            elif file_format.lower() in music_file_format or file_format.upper() in music_file_format:
                return "audio frequency"
            elif file_format.lower() in zip_file_format or file_format.upper() in zip_file_format:
                return "compress"
            else:
                return "other"
        else:
            return "text"
    def move_file(new_dir):
        old_file_name = oldPath.split("/")[-1]
        # Move files to a new folder
        shutil.move(oldPath, new_dir + '/' + old_file_name)  # a->b
        print("-"*50+"Completed:%.2f" % ((location + 1) / allFileNum*100))
    # ImageDate = datetime.datetime.strftime(time.ctime(os.stat(imgPath).st_mtime), "%Y-%m-%d %H:%M:%S")
    a = os.stat(oldPath).st_mtime
    #Get the file creation time and judge whether the folder exists
    creat_time=TimeStampToTime(a)[:-9]
    print(creat_time) #str 2021-01-10 10:05:31
    folder_format=get_file_format(oldPath)
    new_dir="%s/%s/%s"%(those_folder,creat_time,folder_format)
    #Create folder if no folder exists
    if not os.path.exists(new_dir):
        os.makedirs(new_dir)
        move_file(new_dir)
    #If it exists, judge whether it is a duplicate file
    else :
        if oldPath.split("/")[-1] in os.listdir(new_dir):
            print("Duplicate file, skip")
            pass
        else:
            move_file(new_dir)
def do_all():
    for i in myfile:
        judge_file(i,myfile.index(i))
printPath(1, this_folder)
do_all()
input()

Since I don't need to save the photos and videos taken every day in the "picture" and "video" files respectively, I annotate some contents involving formats.

import os,shutil
import time
import datetime
allFileNum = 0
myfile=[]
# mydir=[]
# movie_file_format=['avi','mpeg','mp4','mov','ProRes','DNxHR','mfx','mkv','wmv','flv','rmvb','webm','asf']
# text_file_format=['DOC','PDF','HTML','TXT','HTL','DOCX']
# img_file_format=['bmp','jpg','jpeg','png','tif','gif','pcx','tga','exif','fpx','svg','psd',
#                  'cdr','pcd','dxf','ufo','eps','ai','raw','WMF','webp','avif','hdri','flic','emf','ico']
# zip_file_format=['rar' ,'zip','7z','CAB','ARJ','LZH','TAR','GZ','ACE','UUE','BZ2','JAR','ISO','MPQ']
# music_file_format=['PCM','WAV','AIFF','MP3','AAC','OGG','WMA','FLAC','ALAC','WMA']


this_folder= 'D:\\03 Photo export' 
# this_folder=input("original path:") replace("\",'/')
those_folder='D:\\04 Photo arrangement'
# those_folder=input("target path:") replace("\",'/')
def printPath(level, path):
    global allFileNum
    '''''
    Print all folders and files in a directory
    '''
    # For all folders, the first field is the level of the secondary directory
    dirList = []
    # All documents
    fileList = []
    # Returns a list containing the names of the entries in the directory (google translation)
    files = os.listdir(path)
    # Add directory level first
    dirList.append(str(level))
    for f in files:
        if (os.path.isdir(path + '/' + f)):
            # Exclude hidden folders. Because there are too many hidden folders
            if (f[0] == '.'):
                pass
            else:
                # Add non hidden folder
                dirList.append(f)
                # mydir.append(path + '/' + f)
        if (os.path.isfile(path + '/' + f)):
            # Add file
            fileList.append(f)
            myfile.append(path + '/' + f)
    # When printing a folder level, do not use the first flag
    i_dl = 0
    for dl in dirList:
        if (i_dl == 0):
            i_dl = i_dl + 1
        else:
            # print("obtained folder", '-' * (int (dirlist [- 1]), DL)
            # Print all folders and files under the directory, directory level + 1
            printPath((int(dirList[0]) + 1), path + '/' + dl)
    for fl in fileList:
        # print("obtained file path", '-' * (int (dirlist [- 1]), FL)
        # Just calculate how many files there are
        allFileNum = allFileNum + 1
 
def judge_file(oldPath,location):
    def TimeStampToTime(timestamp):
        timeStruct = time.localtime(timestamp)
        return time.strftime('%Y-%m-%d %H:%M:%S', timeStruct)
    def get_file_format(file_path):
        file_name=file_path.split("/")[-1]
        # if file_name.find(".")>0:
        #     file_format=file_name.split('.')[-1]
        #     if file_format.lower() in movie_file_format or file_format.upper() in movie_file_format:
        #         return "video"
        #     # elif file_format.lower() in text_file_format or file_format.upper() in text_file_format:
        #     #     return "text."
        #     elif file_format.lower() in img_file_format or file_format.upper() in img_file_format:
        #         return "picture"
        #     elif file_format.lower() in music_file_format or file_format.upper() in music_file_format:
        #         return "audio."
        #     elif file_format.lower() in zip_file_format or file_format.upper() in zip_file_format:
        #         return "compress."
        #     else:
        #         return "other"  
        # else:
        #     return "text."
    def move_file(new_dir):
        old_file_name = oldPath.split("/")[-1]
        # Move files to a new folder
        shutil.move(oldPath, new_dir + '/' + old_file_name)  # a->b
        print("-"*50+"Completed:%.2f" % ((location + 1) / allFileNum*100))
    # ImageDate = datetime.datetime.strftime(time.ctime(os.stat(imgPath).st_mtime), "%Y-%m-%d %H:%M:%S")
    a = os.stat(oldPath).st_mtime
    #Get the file creation time and judge whether the folder exists
    creat_time=TimeStampToTime(a)[:-9]
    print(creat_time) #str 2021-01-10 10:05:31
    folder_format=get_file_format(oldPath)
    new_dir="%s/%s/%s"%(those_folder,creat_time,folder_format)
    #Create folder if no folder exists
    if not os.path.exists(new_dir):
        os.makedirs(new_dir)
        move_file(new_dir)
    #If it exists, judge whether it is a duplicate file
    else :
        if oldPath.split("/")[-1] in os.listdir(new_dir):
            print("Duplicate file, skip")
            pass
        else:
            move_file(new_dir)
def do_all():
    for i in myfile:
        judge_file(i,myfile.index(i))
printPath(1, this_folder)
do_all()
input()
# --------
# Copyright notice: This article is the original article of CSDN blogger "lidashent", which follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this notice for reprint.
# Original link: https://blog.csdn.net/lidashent/article/details/113919375

Although I annotated the unnecessary contents, when saving, I found that the pictures and videos of each day are saved in the none file under the date instead of directly under the date. When sorting, I need to click the none file again, drag the pictures and videos to 2022-02-22, and then manually sort them again. If there are many sorting dates, Such repeated dragging is boring.

(pictures and videos are under none file)

Of course, this code has helped me solve the need of quickly sorting photos by date and improved time efficiency. But the last step is to group the photos of the whole day through manual observation (it is difficult to process programmatically), so I always hope to put the photos and videos directly under the date without the existence of none file to increase the extra workload.   

5, Demand realization

Under the guidance of Mr. Ma, on March 2, I began to try to interpret the photo classification code of blogger lidashent, put each piece of code I didn't understand into Baidu, search for possible meaning and comment. After commenting from top to bottom, you finally get the desired result on line 95. (since it has been solved here, the following code comments have not been interpreted in detail, so we will have a chance to study them later)

The reason why there are none files under the original date is that "the new list defines three-level folders (new_dir="%s/%s%s "path date format). If the new list defines two-level folders (new_dir="%s/%s "path date), it can realize the result that photos and videos are uniformly placed in the date directory.

Specific code

import os,shutil
import time
import datetime

allFileNum = 0# The number of all files starts from 0
myfile=[]# My files
mydir=[]# My list
movie_file_format=['avi','mpeg','mp4','mov','ProRes','DNxHR','mfx','mkv','wmv','flv','rmvb','webm','asf']# Video format
text_file_format=['DOC','PDF','HTML','TXT','HTL','DOCX']# Text format
img_file_format=['bmp','jpg','jpeg','png','tif','gif','pcx','tga','exif','fpx','svg','psd',
                 'cdr','pcd','dxf','ufo','eps','ai','raw','WMF','webp','avif','hdri','flic','emf','ico']# Picture format
zip_file_format=['rar' ,'zip','7z','CAB','ARJ','LZH','TAR','GZ','ACE','UUE','BZ2','JAR','ISO','MPQ']# Compression format
music_file_format=['PCM','WAV','AIFF','MP3','AAC','OGG','WMA','FLAC','ALAC','WMA']# Music format


this_folder= 'D:\\03 Photo export' # Folder of photos before sorting, original path:
# this_folder=input("original path:") replace("\",'/')
those_folder='D:\\04 Photo arrangement'# Folder of the sorted photos (original photos deleted), destination path:
# those_folder=input("target path:") replace("\",'/')

def printPath(level, path):# Define output path (hierarchy, path string)
    global allFileNum    # Returns the global variable of allFileNum
    '''''
    Print all folders and files in a directory
    '''
    # For all folders, the first field is the level of the secondary directory
    dirList = []    # List of contents
    # All documents
    fileList = []
    # Returns a list containing the names of the entries in the directory (google translation)
    files = os.listdir(path)
    # Add directory level first
    dirList.append(str(level))
    for f in files:
        if (os.path.isdir(path + '/' + f)):  # os.path.isdir() is used to determine whether the object is a directory
            if (f[0] == '.'):# Exclude hidden folders. Because there are too many hidden folders
                pass
            else:
                # Add non hidden folder
                dirList.append(f)
                # mydir.append(path + '/' + f)
        if (os.path.isfile(path + '/' + f)):
            # Add file
            fileList.append(f)
            myfile.append(path + '/' + f)
    # When a flag is used, the first level of the folder list is not printed
    i_dl = 0
    for dl in dirList:
        if (i_dl == 0):
            i_dl = i_dl + 1
        else:
            # print("obtained folder", '-' * (int (dirlist [- 1]), DL)
            # Print all folders and files under the directory, directory level + 1
            printPath((int(dirList[0])+1), path + '/' + dl)
    for fl in fileList:
        # print("obtained file path", '-' * (int (dirlist [- 1]), FL)
        # Just calculate how many files there are
        allFileNum = allFileNum + 1
 
def judge_file(oldPath,location):    # Definition judgment file (old file, location)
    def TimeStampToTime(timestamp):# Define time to timestamp (timestamp)
        timeStruct = time.localtime(timestamp)#The time structure is equal to, and the formatted timestamp is the local time (timestamp)
        return time.strftime('%Y-%m-%d %H:%M:%S', timeStruct)#Return local timestamp (structure tired, time structure)
    def get_file_format(file_path):        # Define the obtained file format (file path)
        file_name=file_path.split("/")[-1]        #The file name is equal to the last segment of the file path. split("/")[-1] takes' / 'as the delimiter f and retains the last segment
        # if file_name.find(".")>0:
        #     file_format=file_name.split('.')[-1] #The file name is equal to the last paragraph of the file path
        #     if file_format.lower() in movie_file_format or file_format.upper() in movie_file_format:
        #         return "video"
        #     # elif file_format.lower() in text_file_format or file_format.upper() in text_file_format:
        #     #     return "text."
        #     elif file_format.lower() in img_file_format or file_format.upper() in img_file_format:
        #         return "picture"
        #     elif file_format.lower() in music_file_format or file_format.upper() in music_file_format:
        #         return "audio."
        #     elif file_format.lower() in zip_file_format or file_format.upper() in zip_file_format:
        #         return "compress."
        #     else:
        #         return "other"  
        # else:
        #     return "text."
    def move_file(new_dir):#Define a new list of transfer files
        old_file_name = oldPath.split("/")[-1]#The old file name is equal to the last segment of the old path
        # Move files to a new folder
        shutil.move(oldPath, new_dir + '/' + old_file_name)  # a->b
        print("-"*50+"Completed:%.2f" % ((location + 1) / allFileNum*100))
    # ImageDate = datetime.datetime.strftime(time.ctime(os.stat(imgPath).st_mtime), "%Y-%m-%d %H:%M:%S")
    a = os.stat(oldPath).st_mtime    # File timestamp OS Stat (old path file) st_mtime
    #Get the file creation time and judge whether the folder exists
    creat_time=TimeStampToTime(a)[:-9]
    # Create timestamp
    print(creat_time) #Print creation time str 2021-01-10 10:05:31
    folder_format=get_file_format(oldPath)
    # The new list is displayed in a three-stage structure - the sorted path (first level folder), the creation time (second level folder), and the file format (third level folder such as pictures and videos)
    # new_dir="%s/%s/%s"%(those_folder,creat_time,folder_format)    
    # Asha needs two-level folders (the sorted path (the first level folder), and the created time (the second level folder) contains mixed photos and videos. (manual sorting)
    new_dir="%s/%s"%(those_folder,creat_time)
  
    #Create folder if no folder exists
    if not os.path.exists(new_dir):
        os.makedirs(new_dir)
        move_file(new_dir)
    #If it exists, judge whether it is a duplicate file
    else :
        if oldPath.split("/")[-1] in os.listdir(new_dir):
            print("Duplicate file, skip")
            pass
        else:
            move_file(new_dir)
def do_all():
    for i in myfile:
        judge_file(i,myfile.index(i))
printPath(1, this_folder)
do_all()
input()
# --------
# Copyright notice: This article is the original article of CSDN blogger "lidashent", which follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this notice for reprint.
# Original link: https://blog.csdn.net/lidashent/article/details/113919375

Thanks:

Thanks to the original article of CSDN blogger "lidashent" for the convenience of my class work (photo sorting), which also reminds me that I need to really understand the code in order to apply what I have learned and modify it to meet my own needs.

 

 

Topics: Python