pythonshutilfile-sorting

File sorting program makes nested directories if executed again


When the code is run for the first time everything works perfectly but when i execute it a second time the already present directories are nested and the code stops after the error that it cannot put the folder in the same named folder

import os
import shutil

path = "your_path"



file_names = os.listdir(path)

folder_names=["image files","text files","archives","setups","word files","pdf files","other documents","miscellaneous"]

for i in range(len(folder_names)):
    if not os.path.exists(path+folder_names[i]):
        os.mkdir(path+folder_names[i])
    else:
        break

for file in file_names:
    if ".txt" in file and not os.path.exists(path + "text files/" + file):
        shutil.move(path + file, path + "text files/" + file)
    elif ".pdf" in file and not os.path.exists(path + "pdf files/" + file):
        shutil.move(path + file, path + "pdf files/" + file)
    elif ".exe" in file and not os.path.exists(path + "setups/" + file):
        shutil.move(path + file, path + "setups/" + file)
    elif any(ext in file for ext in [".zip", ".rar"]) and not os.path.exists(path + "archives/" + file):
        shutil.move(path + file, path + "archives/" + file)
    elif any(ext in file for ext in [".docx", ".doc"]) and not os.path.exists(path + "word files/" + file):
        shutil.move(path + file, path + "word files/" + file)
    elif any(ext in file for ext in [".jpg",".gif", ".jpeg", ".png"]) and not os.path.exists(path + "image files/" + file):
        shutil.move(path + file, path + "image files/" + file)
    elif any(ext in file for ext in [".pptx", ".xlsx", ".csv"]) and not os.path.exists(path + "image files/" + file):
        shutil.move(path + file, path + "other documents/" + file)
    else:
        shutil.move(path + file, path + "miscellaneous/" + file)

The following code after executing the second time stops after the following error code

Traceback (most recent call last):
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 563, in move
    os.rename(src, real_dst)
OSError: [WinError 87] The parameter is incorrect: 'C:/Users/Dell/Downloads/miscellaneous' -> 'C:/Users/Dell/Downloads/miscellaneous/miscellaneous'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Dell\PycharmProjects\file_sorter\main.py", line 32, in <module>
    shutil.move(path + file, path + "miscellaneous/" + file)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 572, in move
    " '%s'." % (src, dst))
shutil.Error: Cannot move a directory 'C:/Users/Dell/Downloads/miscellaneous' into itself 'C:/Users/Dell/Downloads/miscellaneous/miscellaneous'.


Solution

  • The first time you run this script, the folders from folder_names (likely) don't exist, so they're not in file_names. The second time though, they do, so they're treated like any other files, which means the else: part of the script will try to move them into miscellaneous.

    What you want to do is exclude these folders from the treatment; you can do that either in the final else: part of the script, by replacing the else: block with

    elif file not in folder_names:
        shutil.move(path + file, path + "miscellaneous/" + file)
    

    or at the start of the script, for example by defining file_names thus:

    folder_names=["image files","text files","archives","setups","word files","pdf files","other documents","miscellaneous"]
    
    file_names = set(os.listdir(path)) - set(folder_names)