-1

I'm working with Python and have to move files from a folder to its sub-folder. I tried using shutil.move(), but it gives an error:

Cannot move a directory '%s' into itself

Here's the code:

for file in your_files:
    if file in images:
        shutil.move(your_folder, images_folder)
    elif file in docs:
        shutil.move(your_folder, docs_folder)
    elif file in texts:
        shutil.move(your_folder, texts_folder)
    else:
        shutil.move(your_folder, others_folder)

images_folder, docs_folder, texts_folder and others_folder are all sub-folders of your_folder.

How do I move files from your_folder to the corresponding sub-folders?

Azhar Khan
  • 3,829
  • 11
  • 26
  • 32

1 Answers1

0

Everything is a file: A directory is a file. The destination directory is a file in the source folder.

You are trying to move the destination folder into it self.

You could:

  • Add an additional elif, to catch it and do nothing.
  • Ignore it.
ctrl-alt-delor
  • 7,506
  • 5
  • 40
  • 52