예제 #1
0
 def __init__(self, names, top_level_path, target_folder_path, files_to_exclude):
     self.top_level_dir_path = top_level_path
     self.target_folder_path = target_folder_path
     self.fileNames = os.listdir(top_level_path)
     self.names = names
     self.dirNames = []
     self.printFormatter = PrintFormatter()
     self.repeatedDirName = "REPEATED_DIRECTORY"
     self.dirs_to_move = {}
     self.files_to_exclude = set(files_to_exclude)
예제 #2
0
 def __init__(self, rootPath, exceptedDirs=[], delSamples=False):
     self.exceptedDirs = exceptedDirs
     self.rootPath = rootPath
     self.deleteSamples = delSamples
     self.printFormatter = PrintFormatter()
예제 #3
0
class FilesToTop:
    def __init__(self, rootPath, exceptedDirs=[], delSamples=False):
        self.exceptedDirs = exceptedDirs
        self.rootPath = rootPath
        self.deleteSamples = delSamples
        self.printFormatter = PrintFormatter()

    def makeNewFile(self, nameList, aFile, pathToFile, fileNumber):
        if FileFormatter().file_contains_format(aFile, pathToFile):
            newFile = FileNamer().makeNewFileName(nameList, aFile, pathToFile)
            if newFile != aFile:
                src = pathToFile + aFile
                dst = pathToFile + newFile
                shutil.move(src, dst)
            print(
                fileNumber, '-- from: ' + aFile + '\n' + str(fileNumber) +
                ' -- to:   ' + newFile + '\n')
        elif not os.path.isdir(pathToFile + aFile):
            os.remove(pathToFile + aFile)
            print(fileNumber, '-- removing file: ' + aFile + '\n')
            newFile = None
        else:
            print('File is a dir: ' + pathToFile + aFile)
            newFile = None
        return newFile

    def addNameToAllFilesInDir(self, nameList, topDir, currentDirPath):
        files = os.listdir(currentDirPath)
        currentDirPath = self.addSlashToDir(currentDirPath)
        counter = 1
        for aFile in files:
            filePath = os.path.join(currentDirPath, aFile)
            if os.path.isdir(
                    filePath) and 'keep_dir_together' not in aFile.lower():
                self.addNameToAllFilesInDir(nameList, topDir, filePath)
            else:
                newFile = self.makeNewFile(nameList, aFile, currentDirPath,
                                           counter)
                if newFile is not None:
                    counter += 1
                    topPath = topDir + newFile
                    if topDir != currentDirPath:
                        if not os.path.exists(topPath):
                            shutil.move(currentDirPath + newFile, topPath)
                            print('Moving file: ' + currentDirPath + newFile)
                            print('To: ' + topPath + '\n')
                        else:
                            message = currentDirPath + newFile + ' exisits in: ' + topDir
                            print(
                                self.printFormatter.acrossScreenWithName(
                                    '*', message))
        if len(os.listdir(currentDirPath)) == 0:
            shutil.rmtree(currentDirPath)
            print('Removing dir: ' + currentDirPath + '\n')
        else:
            print('Not removing dir: ' + currentDirPath + '\n')
        return None

    def addSlashToDir(self, dirPath):
        if dirPath[len(dirPath) - 1:] != '/':
            dirPath += '/'
        return dirPath

    def moveFilesToTop(self):
        files = os.listdir(self.rootPath)
        for aFile in files:
            dirPath = os.path.join(self.rootPath, aFile)
            if os.path.isdir(
                    dirPath) and aFile.lower() not in self.exceptedDirs:
                print('\n' +
                      self.printFormatter.acrossScreenWithName('-', aFile) +
                      '\n')
                dirPath = self.addSlashToDir(dirPath)
                self.addNameToAllFilesInDir(aFile.split('_', 1), dirPath,
                                            dirPath)
        return None
예제 #4
0
 def __init__(self, src_path, dst_path):
     self.src_path = src_path
     self.dst_path = dst_path
     self.files = os.listdir(self.src_path)
     self.print_formatter = PrintFormatter()
     self.moved_files = {}
예제 #5
0
class DirectoryMover:
    def __init__(self, src_path, dst_path):
        self.src_path = src_path
        self.dst_path = dst_path
        self.files = os.listdir(self.src_path)
        self.print_formatter = PrintFormatter()
        self.moved_files = {}

    def move_dirs(self):
        for file_name in self.files:
            source_path = os.path.join(self.src_path, file_name)
            destination_path = os.path.join(self.dst_path, file_name)
            self.move_single_dir(source_path, destination_path)
            print(self.print_formatter.acrossScreen('-') + '\n')
        return None

    def move_single_dir(self, src_path, dst_path):
        if os.path.isdir(src_path):
            file_name = src_path.split('/').pop()
            self.moved_files[file_name] = []
            if os.path.isdir(dst_path):
                print("Directory named " + file_name + " exists in target directory ...Moving individual files...\n")
                target_files = os.listdir(src_path)
                for target_file in target_files:
                    sourceFile = os.path.join(src_path, target_file)
                    if os.path.exists(os.path.join(dst_path, target_file)):
                        print("Pre-existing file: " + target_file + " already exists in " + dst_path + "\n")
                    else:
                        shutil.move(sourceFile, dst_path)
                        print("Moving: " + sourceFile + "\nTo: " + os.path.join(dst_path, target_file) + "\n")
                        self._add_file_to_moved_files(file_name, target_file)
            else:
                print("No Directory named: " + file_name + " exists in " + self.dst_path)
                print('Creating: ' + dst_path + '\n')
                print("Moving: " + src_path + "\nTo: " + dst_path + "\n")
                names = os.listdir(src_path)
                for name in names:
                    self._add_file_to_moved_files(file_name, name)
                    print("Moving: " + os.path.join(src_path, name) + "\nTo: " + os.path.join(dst_path, name) + "\n")
                shutil.move(src_path, dst_path)
        return None

    def _del_empty_dirs(self):
        self.files = os.listdir(self.src_path)
        for aFile in self.files:
            path = self.src_path + aFile
            if os.path.isdir(path):
                dirFiles = os.listdir(path)
                size = len(dirFiles)
                shouldRemove = False
                if size == 0:
                    shouldRemove = True
                if size == 1:
                    if dirFiles[0] == ".DS_Store":
                        shouldRemove = True
                if shouldRemove:
                    shutil.rmtree(path)
                    print("Deleting: " + path)
        return None

    def _add_file_to_moved_files(self, dirName, fileName):
        try:
            self.moved_files[dirName].append(fileName)
        except KeyError:
            self.moved_files[dirName] = [fileName]
            pass
        return None

    def save_recently_moved_names(self):
        with open('files/.latest.txt', 'a') as namesFile:
            namesFile.write('\n\n' + 'MOVE DATE: ' +  self._timestamp() + '\n\n')
            for name in sorted(self.moved_files.keys()):
                header = '\n---------------------------------- ' + name + ' ----------------------------------------\n'
                namesFile.write(header)
                for entry in self.moved_files[name]:
                    namesFile.write(entry + '\n')
        namesFile.close()
        return None

    def _timestamp(self):
        date = datetime.datetime.now()
        return "%d-%d-%d %d:%d:%d" % (date.year, date.month, date.day, date.hour, date.minute, date.second)
예제 #6
0
 def __init__(self, src_path, dst_path):
     self.src_path = src_path
     self.dst_path = dst_path
     self.files = os.listdir(self.src_path)
     self.print_formatter = PrintFormatter()
     self.moved_files = {}
예제 #7
0
class DirectoryMover:
    def __init__(self, src_path, dst_path):
        self.src_path = src_path
        self.dst_path = dst_path
        self.files = os.listdir(self.src_path)
        self.print_formatter = PrintFormatter()
        self.moved_files = {}

    def move_dirs(self):
        for file_name in self.files:
            source_path = os.path.join(self.src_path, file_name)
            destination_path = os.path.join(self.dst_path, file_name)
            self.move_single_dir(source_path, destination_path)
            print(self.print_formatter.acrossScreen('-') + '\n')
        return None

    def move_single_dir(self, src_path, dst_path):
        if os.path.isdir(src_path):
            file_name = src_path.split('/').pop()
            self.moved_files[file_name] = []
            if os.path.isdir(dst_path):
                print(
                    "Directory named " + file_name +
                    " exists in target directory ...Moving individual files...\n"
                )
                target_files = os.listdir(src_path)
                for target_file in target_files:
                    sourceFile = os.path.join(src_path, target_file)
                    if os.path.exists(os.path.join(dst_path, target_file)):
                        print("Pre-existing file: " + target_file +
                              " already exists in " + dst_path + "\n")
                    else:
                        shutil.move(sourceFile, dst_path)
                        print("Moving: " + sourceFile + "\nTo: " +
                              os.path.join(dst_path, target_file) + "\n")
                        self._add_file_to_moved_files(file_name, target_file)
            else:
                print("No Directory named: " + file_name + " exists in " +
                      self.dst_path)
                print('Creating: ' + dst_path + '\n')
                print("Moving: " + src_path + "\nTo: " + dst_path + "\n")
                names = os.listdir(src_path)
                for name in names:
                    self._add_file_to_moved_files(file_name, name)
                    print("Moving: " + os.path.join(src_path, name) +
                          "\nTo: " + os.path.join(dst_path, name) + "\n")
                shutil.move(src_path, dst_path)
        return None

    def _del_empty_dirs(self):
        self.files = os.listdir(self.src_path)
        for aFile in self.files:
            path = self.src_path + aFile
            if os.path.isdir(path):
                dirFiles = os.listdir(path)
                size = len(dirFiles)
                shouldRemove = False
                if size == 0:
                    shouldRemove = True
                if size == 1:
                    if dirFiles[0] == ".DS_Store":
                        shouldRemove = True
                if shouldRemove:
                    shutil.rmtree(path)
                    print("Deleting: " + path)
        return None

    def _add_file_to_moved_files(self, dirName, fileName):
        try:
            self.moved_files[dirName].append(fileName)
        except KeyError:
            self.moved_files[dirName] = [fileName]
            pass
        return None

    def save_recently_moved_names(self):
        with open('files/.latest.txt', 'a') as namesFile:
            namesFile.write('\n\n' + 'MOVE DATE: ' + self._timestamp() +
                            '\n\n')
            for name in sorted(self.moved_files.keys()):
                header = '\n---------------------------------- ' + name + ' ----------------------------------------\n'
                namesFile.write(header)
                for entry in self.moved_files[name]:
                    namesFile.write(entry + '\n')
        namesFile.close()
        return None

    def _timestamp(self):
        date = datetime.datetime.now()
        return "%d-%d-%d %d:%d:%d" % (date.year, date.month, date.day,
                                      date.hour, date.minute, date.second)
예제 #8
0
 def __init__(self, rootPath, exceptedDirs=[], delSamples=False):
     self.exceptedDirs = exceptedDirs
     self.rootPath = rootPath
     self.deleteSamples = delSamples
     self.printFormatter = PrintFormatter()
예제 #9
0
class FilesToTop:
    def __init__(self, rootPath, exceptedDirs=[], delSamples=False):
        self.exceptedDirs = exceptedDirs
        self.rootPath = rootPath
        self.deleteSamples = delSamples
        self.printFormatter = PrintFormatter()

    def makeNewFile(self, nameList, aFile, pathToFile, fileNumber):
        if FileFormatter().file_contains_format(aFile, pathToFile):
            newFile = FileNamer().makeNewFileName(nameList, aFile, pathToFile)
            if newFile != aFile:
                src = pathToFile + aFile
                dst = pathToFile + newFile
                shutil.move(src, dst)
            print(fileNumber, '-- from: ' + aFile + '\n' + str(fileNumber) + ' -- to:   ' + newFile + '\n')
        elif not os.path.isdir(pathToFile + aFile):
            os.remove(pathToFile + aFile)
            print(fileNumber, '-- removing file: ' + aFile + '\n')
            newFile = None
        else:
            print('File is a dir: ' + pathToFile + aFile)
            newFile = None
        return newFile

    def addNameToAllFilesInDir(self, nameList, topDir, currentDirPath):
        files = os.listdir(currentDirPath)
        currentDirPath = self.addSlashToDir(currentDirPath)
        counter = 1
        for aFile in files:
            filePath = os.path.join(currentDirPath, aFile)
            if os.path.isdir(filePath) and 'keep_dir_together' not in aFile.lower():
                self.addNameToAllFilesInDir(nameList, topDir, filePath)
            else:
                newFile = self.makeNewFile(nameList, aFile, currentDirPath, counter)
                if newFile is not None:
                    counter += 1
                    topPath = topDir + newFile
                    if topDir != currentDirPath:
                        if not os.path.exists(topPath):
                            shutil.move(currentDirPath + newFile, topPath)
                            print('Moving file: ' + currentDirPath + newFile)
                            print('To: ' + topPath + '\n')
                        else:
                            message = currentDirPath + newFile + ' exisits in: ' + topDir
                            print(self.printFormatter.acrossScreenWithName('*', message))
        if len(os.listdir(currentDirPath)) == 0:
            shutil.rmtree(currentDirPath)
            print('Removing dir: ' + currentDirPath + '\n')
        else:
            print('Not removing dir: ' + currentDirPath + '\n')
        return None

    def addSlashToDir(self, dirPath):
        if dirPath[len(dirPath) - 1:] != '/':
            dirPath += '/'
        return dirPath

    def moveFilesToTop(self):
        files = os.listdir(self.rootPath)
        for aFile in files:
            dirPath = os.path.join(self.rootPath, aFile)
            if os.path.isdir(dirPath) and aFile.lower() not in self.exceptedDirs:
                print('\n' + self.printFormatter.acrossScreenWithName('-', aFile) + '\n')
                dirPath = self.addSlashToDir(dirPath)
                self.addNameToAllFilesInDir(aFile.split('_', 1), dirPath, dirPath)
        return None
예제 #10
0
class Organizer:
    def __init__(self, names, top_level_path, target_folder_path, files_to_exclude):
        self.top_level_dir_path = top_level_path
        self.target_folder_path = target_folder_path
        self.fileNames = os.listdir(top_level_path)
        self.names = names
        self.dirNames = []
        self.printFormatter = PrintFormatter()
        self.repeatedDirName = "REPEATED_DIRECTORY"
        self.dirs_to_move = {}
        self.files_to_exclude = set(files_to_exclude)

    def newDirName(self, name):
        nameList = self.names.separate_names(name)
        if len(nameList) == 2:
            dirName = nameList[0] + "_" + nameList[1]
        else:
            dirName = nameList[0]
        return dirName

    def doesNameListHaveRepeatedFirstName(self, name):
        hasSameFirstName = False
        names = self.names.seperateNames(name)
        if self.names.hasFirstAndLastName(name):
            for aName in self.names.nameList:
                splitNames = self.names.seperateNames(aName)
                counter = 0
                if len(splitNames) == 2:
                    if splitNames[0] == names[0] and splitNames[1] != names[1]:
                        hasSameFirstName = True
                        break
                else:
                    if splitNames[0] == names[0]:
                        counter += 1
                        if counter == 2:
                            hasSameFirstName = True
                            break
        else:
            counter = 0
            for aName in self.names.nameList:
                splitNames = self.names.seperateNames(aName)
                if len(splitNames) == 1:
                    if splitNames[0] == names[0]:
                        counter += 1
                        if counter == 2:
                            hasSameFirstName = True
                            break
        return hasSameFirstName

    def directoryAlreadyCreatedForFirstName(self, name):
        firstName = self.names.seperateNames(name)[0]
        dirName = self.repeatedDirName
        for aDir in self.dirNames:
            if firstName in aDir:
                dirName = aDir
                break
        return dirName

    def makeNewDirectoryPath(self, name, repeat=False):
        newDirName = self.newDirName(name)
        if repeat:
            newDirName = newDirName + "_repeat/"
        newDirPath = os.path.join(self.target_folder_path, newDirName)
        return newDirPath

    def makeNewDirectory(self, name, repeat=False):
        newDirPath = self.makeNewDirectoryPath(name, repeat)
        if os.path.isdir(newDirPath):
            return False
        else:
            os.mkdir(newDirPath)
            self.dirNames.append(self.newDirName(name) + "/")
            return True

    def filesForFirstAndLastName(self, name):
        files = []
        for aFile in self.fileNames:
            if self.names.file_has_name(name, aFile):
                files.append(aFile)
        for aFile in files:
            self.fileNames.remove(aFile)
        return files

    def filesForFirstName(self, name):
        firstName = self.names.separate_names(name)[0]
        files = []
        for aFile in self.fileNames:
            if firstName.lower() in aFile.lower():
                files.append(aFile)
        for aFile in files:
            self.fileNames.remove(aFile)
        return files

    def moveFilesForFirstAndLastName(self):
        self.printNumberOfFiles()
        files = []
        for name in self.names.all_names():
            if self.names.has_fist_and_last_name(name):
                files = self.filesForFirstAndLastName(name)
            else:
                files = self.filesForFirstName(name)
            if len(files) > 0:
                dirPath = self.makeNewDirectoryPath(name)
                newDir = self.makeNewDirectory(name)
                printNames = []
                for aFile in files:
                    src = os.path.join(self.top_level_dir_path, aFile)
                    dst = os.path.join(dirPath, aFile)
                    if src != dst:
                        shutil.move(src, dst)
                        printNames.append(aFile)
                self.printInfo(name, printNames, dirPath, newDir)
        self.printNumberOfFiles()
        return None

    def move_files(self):
        files = os.listdir(self.top_level_dir_path)
        to_move = {}
        for file in files:
            if file not in self.files_to_exclude:
                names_in_file = [name for name in self.names.all_names()
                                 if self.names.file_has_name(name, file)]
                ranking_name = self.ranker.rank(names_in_file)
                if ranking_name:
                    if ranking_name in self.dirs_to_move:
                        files_for_name = self.dirs_to_move[ranking_name]
                        files_for_name.append(file)
                        to_move[ranking_name] = files_for_name
                    else:
                        to_move[ranking_name] = [file]
        for dir_name, files in to_move:
            is_new = self.makeNewDirectory(dir_name)
            dir_path = self.makeNewDirectoryPath(dir_name)
            printNames = []
            for file in files:
                src = os.path.join(self.top_level_dir_path, file)
                dst = os.path.join(dir_path, file)
                if src != dst:
                    #shutil.move(src, dst)
                    printNames.append(file)
            self.printInfo(dir_name, printNames, dir_path, is_new)
        return None


    def moveFilesForFirstName(self):
        self.printNumberOfFiles()
        for name in self.names.nameList:
            files = self.filesForFirstName(name)
            if len(files) > 0:
                firstName = self.names.seperateNames(name)[0]
                # if there are repeated first names in namelist, makes new directory for first name only and moves files into it
                if self.doesNameListHaveRepeatedFirstName(name):
                    dirPath = self.makeNewDirectoryPath(firstName, True)
                    isNewDir = self.makeNewDirectory(firstName, True)
                    names = []
                    for aFile in files:
                        src = os.path.join(self.top_level_dir_path, aFile)
                        dst = os.path.join(dirPath, aFile)
                        if src != dst:
                            shutil.move(src, dst)
                            names.append(aFile)
                    if len(names) > 0:
                        self.printInfo('Fist Name Only -- Repeated -- ' + name, names, dirPath, isNewDir)
                # if there are not repeated first names in namelist, will move files into folder with same first name
                else:
                    dirPath = self.makeNewDirectoryPath(name)
                    isNewDir = self.makeNewDirectory(name)
                    names = []
                    for aFile in files:
                        src = os.path.join(self.top_level_dir_path, aFile)
                        dst = os.path.join(dirPath, aFile)
                        if src != dst:
                            shutil.move(src, dst)
                            names.append(aFile)
                    if len(names) > 0:
                        self.printInfo('Fist Name Only -- ' + name, names, dirPath, isNewDir)
        self.printNumberOfFiles()
        return None

    def printInfo(self, name, files, dst, isNewDirectory):
        print(self.printFormatter.acrossScreenWithName('-', name))
        if isNewDirectory:
            print('\nMaking New Directory: ' + dst)
        else:
            print('\nNot Making New Directory...Directory Exists: ' + dst)
        print("\nFiles Moved:")
        counter = 1
        for aFile in files:
            print(counter, '--', aFile)
            counter += 1
        print("\nMoved to: " + dst + '\n')
        return None

    def printNumberOfFiles(self):
        numOfFiles = len(self.fileNames)
        if numOfFiles > 0:
            title = 'Files in root directory: ' + str(numOfFiles)
            print('\n' + self.printFormatter.acrossScreenWithName('*', title) + '\n')
        return None