import json
from os import listdir,getenv
from os.path import isfile,join,isdir
import fnmatch,shutil
with open('./config.json') as config:
d = json.load(config)
downloadsFolder = getenv('HOME')+d["defaultFolder"]
print("Using "+downloadsFolder+" as default Folder")
fileList = [f for f in listdir(downloadsFolder) if isfile(join(downloadsFolder, f))]
folderList = [f for f in listdir(downloadsFolder) if isdir(join(downloadsFolder, f))]
def moveFiles(src,dest):
origSrc = join(downloadsFolder,src)
print("Moving From - ",origSrc,"\nMoving to - ",getenv('HOME')+dest)
try:
shutil.move(origSrc,getenv('HOME')+dest)
except shutil.Error as e:
print(e)
pass
def patternMatching(fileName):
for pattern in d["Patterns"]:
for pathis,extensionList in zip([d["Patterns"][pattern]["path"]],[d["Patterns"][pattern]["extensions"]]):
for ext in extensionList:
if fnmatch.fnmatch(fileName,ext):
moveFiles(fileName,pathis)
def interactiveMode():
for folder in folderList:
letter = input("Where Shall I move "+folder+". m-music,d-Documents,v-Videos,p-Pictures\n")
if (letter=="M" or letter=="m"):
moveFiles(folder,d["Patterns"]["Music"]["path"])
elif (letter=="D" or letter=="d"):
moveFiles(folder,d["Patterns"]["Documents"]["path"])
elif (letter=="V" or letter=="v"):
moveFiles(folder,d["Patterns"]["Videos"]["path"])
elif (letter=="P" or letter=="p"):
moveFiles(folder,d["Patterns"]["Pictures"]["path"])
else:
print("Skipping the folder")
if __name__=="__main__":
for file in fileList:
patternMatching(file)
interactiveMode()