简体   繁体   中英

“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.

Not sure why but using this worked:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM