Everyone is familiar with the rubble of photos and videos, which rest for years after copying from devices.
This is especially true for iphone, ipad, which, when copied directly (without itunes), create
deposits of media content. How can I sort it all out over the years and months?
Yes, there is synchronization, yes, you can sort everything at once. But ...
Someone prefers not to touch anything, since the unity of the dump is observed, someone makes timid attempts to decompose everything accumulated at least over the years.
Undoubtedly, through the conductor, using the tabular view, it will not be difficult to manually scatter all this, but you start looking at old photos, and the process is delayed.
Therefore, we will write a simple program that will decompose files in a folder by years with months in them:
The program will itself determine the date, "looking" at the date of the file modification:
Why do we use the modification date, and not the file creation date?
As a rule, she more correctly indicates the date of the file than her namesake.
Importing modules at start:
import os,time
import datetime
import shutil
We suggest the user to copy the path (windows) to the folder with the files:
p=input(' . : E:/\1')
os.chdir(p)
Let's introduce the function of creating folders with months from 01 to 12 (forgive me for the lack of f 'lines):
# 01 12
def d():
for x in range (1,13):
if x>9:
if not os.path.exists(str(x)):
os.makedirs(str(x))
else:
if not os.path.exists('0'+str(x)):
os.makedirs('0'+str(x))
The following function will process the date obtained from the photo / video file:
def mod_date(file):
t = os.path.getmtime(file)
return datetime.datetime.fromtimestamp(t)
Now, going through the folder, the program will collect all file extensions, and at the same time,
determine what year the file has. For each year, its own folder will be created, and in it,
folders with months will be created:
a=[] #['AAE', 'MOV', 'JPG', 'PNG']
for root, dirs, files in os.walk(p):
for file in files:
if file[-3:] not in a:
a.append(file[-3:])
if file[-3:] in a:
year=str(mod_date(file))[:10][:4]
if not os.path.exists(year):
os.makedirs(year)
os.chdir(p+'/'+year)
d()
os.chdir(p)
* Thus, you can scatter files with completely different (any) extensions into folders, not just jpeg, mov, mkv.
Let's go through the folder with the photo dump again, now transferring the photos to the corresponding, newly created folders:
try:
for root, dirs, files in os.walk(p):
for file in files:
if file[-3:] in a:
year=str(mod_date(file))[:10][:4]
month=str(mod_date(file))[:10][5:7] #
shutil.move(file, year+'/'+month+'/'+file) #
except EnvironmentError:
(' ')
Since the program terminates with an error, without finding the last file in the loop, which does not affect its correct execution, a crutch from try, except is used, which dulls the vigilance of impressionable persons who get red in the console.
PS: The post does not pretend to be scientific, but, perhaps, it will help someone to defeat the photo / video dumps. Download the
program .