"Moving a directory into itself" error in Python

Question:

I have an application which sorts files upon receiving the path to the folder containing the files.
However, there’s a line of code where I’m supposedly moving a directory into itself, but I can’t see how that’s happening since I’m doing the same thing with the other parts and they aren’t resulting in an error.

Here’s the part containing the error.

the part resulting in an error is the last block, where I’m trying to move other files into "Other" folder.

I tried changing up the destination in the shutil.move() but the error is persisting.

os.chdir(path)
new_folder = "Sorted Files"
os.makedirs(new_folder)
path_2 = path+"/"+new_folder
os.chdir(path_2)
new_folder_doc = "Documents"
new_folder_texts = "Texts"
new_folder_images = "Images"
new_folder_other = "Other"
os.makedirs(new_folder_doc)
os.makedirs(new_folder_texts)
os.makedirs(new_folder_images)
os.makedirs(new_folder_other)


for file in os.listdir(path):
    file_path = os.path.join(path, file)
    if os.path.isfile(file_path):
        file_name = os.path.basename(file_path)

        #  Sorting files
    if file_path.endswith('.png') or file_path.endswith('.gif') or file_path.endswith('.bmp') or
            file_path.endswith('.jpg') or file_path.endswith('.jpeg') is True:
        shutil.move(file_path, new_folder_images)
        continue
    if file_path.endswith('.txt') or file_path.endswith('.ini') or file_path.endswith('.log') is True:
        shutil.move(file_path, new_folder_texts)
        continue
    if file_path.endswith('.pdf') or file_path.endswith('.docx') or file_path.endswith('.doc') or
            file_path.endswith('.xls') or file_path.endswith('.xlsx') or file_path.endswith('.csv') is True:
        shutil.move(file_path, new_folder_doc)
        continue
    if file_path.endswith('.docx') or file_path.endswith('.txt') or file_path.endswith('.bmp') or 
            file_path.endswith('.png') or file_path.endswith('.ini') or file_path.endswith('.log') 
            or file_path.endswith('.gif') or file_path.endswith('.doc') or file_path.endswith('.dir') 
            or file_path.endswith('.xls') or file_path.endswith('.xlsx') or file_path.endswith('.csv') 
            or file_path.endswith('.jpg') or file_path.endswith('.jpeg') or file_path.endswith('.pdf') is not True:
        shutil.move(file_path, new_folder_other)
        continue
Asked By: Buj

||

Answers:

os.listdir lists both files and directories. In your last block, you try to move every file/directory that does not end with ".pdf" which is true for the "Other" directory.

To fix it in your code, you could perform sorting only to files, not directories. You could easily skip directories in your for-loop by adding the following code to check if "file_path"-variable actually refers to a file or not:

if not os.path.isfile(file_path):
    continue

Also, you might want to double-check your ".endswith"-conditions. is True and is not True there only applies to the last condition, not all of them (which I think is what you’re looking for).

Answered By: juhat
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.