Skip to content
Advertisement

File not found error in os.rename

I am trying to write a program to categorize into folders a large amount of files according to their respective groups indicated in the file name. I wrote the followin code, but when I run it it gives me a file not found error, even though the file is in the given path. I’d appreciate any help in figuring out what is wrong.

import os

old_dir = '/Users/User/Desktop/MyFolder'

for f in os.listdir(old_dir):
    file_name, file_ext = os.path.splitext(f)
    file_name.split('-')

    split_file_name = file_name.split('-')

    new_dir = os.path.join(old_dir,
                           '-'.join(split_file_name[:3]),
                           split_file_name[5],
                           f)

    os.rename(os.path.join(old_dir, f), new_dir)

Here’s the error:

Traceback (most recent call last):
  File "/Users/User/Documents/Sort Files into Folders/Sort Files into Folders.py", line 19, in <module>
    os.rename(os.path.join(old_dir, f), new_dir)
FileNotFoundError: [Errno 2] No such file or directory: '/Users/User/Desktop/MyFolder/AHA35-3_30x1_12-31-7d-g1a1-ArmPro.jpg' -> '/Users/User/Desktop/MyFolder/AHA35-3_30x1_12-31/ArmPro/AHA35-3_30x1_12-31-7d-g1a1-ArmPro.jpg

Advertisement

Answer

os.rename does not automatically create new directories (recursively), if the new name happens to be a filename in a directory that does not exist.

To create the directories first, you can (in Python 3) use:

os.makedirs(dirname, exist_ok=True)

where dirname can contain subdirectories (existing or not).


Alternatively, use os.renames, that can handle new and intermediate directories. From the documentation:

Recursive directory or file renaming function. Works like rename(), except creation of any intermediate directories needed to make the new pathname good is attempted first

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement