Why are files not going to the right folder in my file organizer?

42 views Asked by At

I am trying to FTP the files automatically to the server (specific folder). This program sorts the filetypes and moves the files to the correct folder based on certain parameters, then Plex takes care of the metadata.

I am trying to have this script automatically sort when there are new files downloaded. The issue is an album (folder) of songs, gets transferred to the review folder, instead of the music folder.

Code:

import os  # walks through directory
import shutil  # move files
import time # show runtime

music_filetype = ['.mp3', '.flac', '.m4a', '.wav', '.flac', '.wma', '.aac', '.aiff', '.aif',             '.aifc']
video_filetype = ['.mkv', '.mp4', '.m4v', '.mov', '.wmv', '.flv', '.avi', '.mpeg', '.mpg', '.mpe']
archive_filetype = ['.zip', '.rar', '.7z', '.deb', '.tar', '.pkg', '.tar.gz', '.gz', '.rpm', '.z']


# ----------------------------- Need to change directory locations -----    --------------------------

FTP_Folder = r'C:\Users\******\Desktop\Automation Files\Unsorted Files'  # Set to where ever     the download folder is
Movie_Dest = r'C:\Users\******\Desktop\Automation Files\Movies'  # set to where ever the filetype destinations are
TV_Dest = r'C:\Users\******\Desktop\Automation Files\TV'
Music_Dest = r'C:\Users\******\Desktop\Automation Files\Music'
Review_Folder = r'C:\Users\******\Desktop\Automation Files\Review'  # if algorithm does not meet     parameters, move here

# As of 7/3/2021, this auto sorts everything perfectly into the correct folders, no matter if     its a loose file, season
# folder of episodes, folder of songs, band containing album with the songs, movie loose or in a folder.
# The only issue with sorting is an album of songs goes into the movies folder. The weird thing is when running this
# script with just that folder, it sorts just fine putting it in the music folder. When the FTP folder has movies and
# other things with it, then it jacks up.
#
# This tells me its a looping problem of the isMovie() method not exiting in time. Carry over if you will.
# Probably just need to tighten the restrictions or widen the file checker paradigm.


def isMusic(file):
    for root, dirs, files in os.walk(FTP_Folder):  # will iterate through a directory to get to the bottom most file
        for file in files:
            for i in music_filetype:
                if file.lower().endswith(i):
                    print("It is music")
                    return True
                else:
                    return False


def isTV(file):
    for root, dirs, files in os.walk(FTP_Folder):  # will iterate through a directory to get to the bottom most file
        for file in files:
            for i in video_filetype:
                if file.lower().__contains__('.s0' or '.s1') and file.lower().endswith(i):  # For more complexity, '.s' + a number, instead of another
                    # for loop. Runtime will be too long..
                    print("It is a TV show")
                    return True
                else:
                    return False


def isMovie(file):
    for root, dirs, files in os.walk(FTP_Folder):  # will iterate through a directory to get to the bottom most file
        for file in files:
            for i in video_filetype:
                if not file.lower().__contains__('.s0' or '.s1') and file.lower().endswith(i):  # change so it loops
                    # through the music_filetype list above
                    print("It is a movie")
                    return True
                else:
                    return False


def isFile(file):  # broken, see "file_mover" method below - and (os.path.isfile(file) not )
    #print(__file__)
    for i in (video_filetype or music_filetype or archive_filetype):
        if os.path.isfile(file) or file.__contains__(i):  # checks if file
            #print(__file__) # interesting program run results
            return True
        else:
            return False
        #if os.path.isdir(file):  # checks if directory
            #print("It is a directory")
            #return False


def file_mover(file_, destination):
    name, ext = os.path.splitext(file_)
    ext = ext[1:]  # This is going to store the extension type

    if os.path.exists(destination + '/' + file_):  # This checks if file already exists.
        # If it does, ideally move it to review folder.
        print("File already exists")
        shutil.move(FTP_Folder + '/' + file_, Review_Folder + '/' + file_)
        print("The file has moved to " + destination)
    else:
        if isFile(file_):
            os.makedirs(destination + '/' + file_)  # This will create a new directory.
            shutil.move(FTP_Folder + '/' + file_, destination + '/' + file_)
        else:
            shutil.move(FTP_Folder + '/' + file_, destination + '/' + file_)
        print(file_ + " has moved to " + destination)


def filetype_checker():  # Set variable of where to store files, pass through depending on movie, show or music
    list_ = os.listdir(FTP_Folder)  # This will create a properly organized list with all the     filename that is there
    # in the directory
    for file_ in list_:  # This will go through each and every file

        ext = os.path.splitext(file_)  # Stores the extension type
        ext = ext[1:]
        if ext == '':  # This forces the next iteration, if it is the directory
            continue


        if isMusic(file_) and not isTV(file_) and not isMovie(file_):
            file_mover(file_, Music_Dest)
        elif isTV(file_) and not isMusic(file_) and not isMovie(file_):  # For more complexity, '.s' + int.str instead of another for loop. Runtime will be too long..
            file_mover(file_, TV_Dest)
        elif isMovie(file_) and not isTV(file_) and not isMusic(file_):
            file_mover(file_, Movie_Dest)
        else:
            file_mover(file_, Review_Folder)


def main():
    start_time = time.time()
    filetype_checker()
    print("--- %s seconds ---" % (time.time() - start_time))


if __name__ == "__main__":
    main()
0

There are 0 answers