import os, eyed3, re, shutil, sys, getopt def organizeFiles(path, dest): with os.scandir(path) as dir_entries: for entry in dir_entries: if bool(re.search(r'.*\.mp3', entry.name)) is True: file = eyed3.load(entry) artist = file.tag.artist artist = re.sub(r'(\/)|(\\)|(\:)|(\*)|(\?)|(\")|(\<)|(\>)|(\|)|(\ )$', ' ', artist) album = file.tag.album album = re.sub(r'(\/)|(\\)|(\:)|(\*)|(\?)|(\")|(\<)|(\>)|(\|)|(\ )$', ' ', album) artistIsExist = os.path.exists(f'{dest}\\{artist}') if not artistIsExist: os.makedirs(f'{dest}\\{artist}') albumIsExist = os.path.exists(f'{dest}\\{artist}\\{album}') if not albumIsExist: os.makedirs(f'{dest}\\{artist}\\{album}') song = entry.name song = re.sub(r'(\/)|(\\)|(\:)|(\*)|(\?)|(\")|(\<)|(\>)|(\-)|(\|)|(\ )$', '', song) song = re.sub(r'\ +', ' ', song) try: shutil.move(entry.path, str(f'{dest}\\{artist}\\{album}\{song}')) except: print(song) def main(argv): if len(argv) == 0: print('Usage: main.py -s SOURCE_DIR -d DEST_DIR') else: opts, args = getopt.getopt(argv, "s:d:") for opt, arg in opts: if opt == '-s': path = arg elif opt == '-d': dest = arg try: organizeFiles(path, dest) except: print('Arguments not entered correctly') if __name__ == "__main__": main(sys.argv[1:])