Skip to content

Instantly share code, notes, and snippets.

@m3ck0
Last active March 7, 2016 11:23
Show Gist options
  • Save m3ck0/073778d011178e02929e to your computer and use it in GitHub Desktop.
Save m3ck0/073778d011178e02929e to your computer and use it in GitHub Desktop.
#imports
import os
import shutil
from tkinter import *
from fnmatch import fnmatch
from datetime import datetime
from threading import Thread
#global variables
takeoutPaths = []
patterns = ['*.jpg', '*.jpeg', '*.png']
copyPath = "takeout-copy"
total = 0
#functions
def get_takeout_path():
global takeoutPaths, copyPath
top = filedialog.askdirectory()
for path, subdirs, files in os.walk(top):
if 'takeout' in path:
takeoutPaths.append(path)
return True if len(takeoutPaths) > 0 else False
def start_migration():
global takeoutPaths, copyPath
while get_takeout_path() is False:
pass
while os.path.exists(os.path.join(os.getcwd(), copyPath)):
copyPath += '1'
os.mkdir(copyPath)
copyPath = os.path.join(os.getcwd(), copyPath)
thread = Thread(target = migration_wrapper)
thread.start()
def migration_wrapper():
global takeoutPaths, copyPath, total
for takeoutPath in takeoutPaths:
migrate(takeoutPath, copyPath)
statusLabel.config(text="{0} moved, status: OK".format(total))
def migrate(frm, to):
global total
status = "{0} files moved"
try:
for path, subdirs, files in os.walk(frm):
for name in files:
for pattern in patterns:
if fnmatch(name, pattern):
if not os.path.exists(os.path.join(to, name)):
shutil.copy(os.path.join(path, name),
os.path.join(to, name))
total += 1
statusLabel.config(text=status.format(total))
return total
except Exception as ge:
print(ge)
#gui section
root = Tk()
root.title("takeout explorer")
frame = Frame(root)
frame.grid()
statusLabel = Label(frame, text='status of transfer', pady=10)
statusLabel.grid(column=0, row=1)
startBtn = Button(frame, text='start migration', width=25,
pady=10, command=start_migration)
startBtn.grid(column=0, row=0)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment