Search code examples
pythonfile-rename

"IsADirectoryError: [Errno 21] Is a directory" when it is a file


I tried using:

import os
my_path = [files for pth, dirs, files in os.walk(path)]
result_list = ['one', 'two', 'three']
for original_path, new_name in zip(my_path, result_list):
    os.rename(original_path, 'path_to_save'.format(new_name))

where I get:

IsADirectoryError: [Errno 21] Is a directory: 'path-of-original-path' -> 'path-of-new-name'

but I can confirm that 'original_path' leads to a file as on using os.path.isfile returns True.

Any help would be highly appreciated.


Solution

  • Not sure why but using this worked:

    import shutil
    shutil.move(original_path, 'path_to_save'.format(new_name))