Skip to content

Instantly share code, notes, and snippets.

@jeffgerhard
Created June 21, 2016 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffgerhard/ac7905a1a5979ad7c6f3fed0272b5299 to your computer and use it in GitHub Desktop.
Save jeffgerhard/ac7905a1a5979ad7c6f3fed0272b5299 to your computer and use it in GitHub Desktop.
workflow (in progress) for clearing out directories of cr2s that have been processed into archival jp2s and can be safely deleted from storage
import os
from tkinter.filedialog import askdirectory
from tkinter import messagebox
from sys import exit
from shutil import rmtree
def get_immediate_subdirectories(a_dir):
# stackoverflow.com/questions/800197/how-to-get-all-of-the-immediate-subdirectories-in-python#800201
return [name for name in os.listdir(a_dir)
if os.path.isdir(os.path.join(a_dir, name))]
def checkJP2s(d,cr2count):
count = 0
totalsize = 0
for file in os.listdir(d):
if file.endswith('.jp2'):
totalsize += os.path.getsize(os.path.join(d,file))
count += 1
avgsize = totalsize/count
print("_____________________________________")
print(d)
# print("total size: " + str(totalsize))
print("cr2 file count: " + str(cr2count))
print("jp2 file count: " + str(count))
# print("average file size: " + str(avgsize))
for file in os.listdir(d):
if file.endswith('.jp2'):
if os.path.getsize(os.path.join(d,file)) < (avgsize * 0.8):
if messagebox.askyesno("Alert","File size of " + file + " is suspicious. Continue?",icon=messagebox.WARNING) == False:
exit("Halting this program")
if abs(cr2count - count) > 2:
if messagebox.askyesno("Alert","File count discrepancy. Continue?",icon=messagebox.WARNING) == False:
exit("Halting this program")
return True
def countCR2s(d):
cr2count=0
for file in os.listdir(os.path.join(dirname,d)):
if file.endswith('.CR2'):
cr2count += 1
return cr2count
dirname = askdirectory(title='directory to check',initialdir='Z:\\holding\\books\\hasnt_circulated\\')
dirlist = get_immediate_subdirectories(dirname)
warningslist = []
logname = os.path.join(dirname,"deletes.txt")
for d in dirlist:
mdir = "Z:\\archival_master_files\\books\\general_collection\\" + d + "_master"
if os.path.isdir(mdir):
if checkJP2s(mdir + "\\data\\JP2000",countCR2s(d)):
with open (logname,'a') as fh:
fh.write(d + '\n')
rmtree(os.path.join(dirname,d))
else:
print(d + " has a file count discrepancy")
else:
print("_____________________________________")
msg = d + " DOESN'T HAVE A MASTERS FILE"
print(msg)
warningslist.append(msg)
if len(warningslist)>0:
print("THERE ARE WARNINGS")
print("Check the following for any problems:")
print(*warningslist, sep='\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment