Skip to content

Instantly share code, notes, and snippets.

@niklasravnsborg
Created October 11, 2015 13:41
Show Gist options
  • Save niklasravnsborg/8336cac55afdfbe7d0c3 to your computer and use it in GitHub Desktop.
Save niklasravnsborg/8336cac55afdfbe7d0c3 to your computer and use it in GitHub Desktop.
File and Image Sorting
import os, datetime, time
for dir in os.listdir('.'):
if os.path.isdir(dir):
dates = []
for file in os.listdir(dir):
if file.endswith(('.jpg', '.jpeg')):
photo = os.path.join(dir, file)
timetuple = (os.stat(photo).st_atime, os.stat(photo).st_mtime)
dates.append(timetuple)
print 'Folder:', dir
print '0:', datetime.datetime.fromtimestamp(dates[0][1])
print '1:', datetime.datetime.fromtimestamp(dates[1][1])
userchoice = int(raw_input('Which one to use?'))
for file in os.listdir(dir):
if file.endswith(('.jpg', '.jpeg')):
photo = os.path.join(dir, file)
os.utime(photo, dates[userchoice])
print '\n'
import csv, sys, shutil, os
f = open('duplicates.csv', 'rt')
try:
reader = csv.DictReader(f)
for row in reader:
src = row['Folder'] + '/' + row['Filename']
fldr = row['Folder'] + '/' + row['Group ID'] + '/'
dst = fldr + row['Filename']
if not os.path.exists(fldr):
os.makedirs(fldr)
shutil.move(src, dst)
raw_input('Press Enter to continue...')
finally:
f.close()
import os, datetime, time
for file in os.listdir('.'):
if file.endswith(('.mp4')):
prevdt = datetime.datetime.fromtimestamp(os.stat(file).st_mtime)
print 'processing: ' + file
print 'old datetime: ' + str(prevdt)
t = file.split('-')[1]
tyear = int(t[0:4])
tmonth = int(t[4:6])
tday = int(t[6:8])
newdt = datetime.datetime(
tyear,
tmonth,
tday,
prevdt.hour,
prevdt.minute,
prevdt.second
)
print 'new datetime: ' + str(newdt)
newtimetuple = (os.stat(file).st_atime, time.mktime(newdt.timetuple()))
os.utime(file, (newtimetuple))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment