Cannot access local folders to rename them

I would like to rename a folder. Everything outside the two “__” should be removed. The program responds with “path not found”.

Repl link:

import os

def rename_folder(path):
    try:
        for foldername in os.listdir(path):
            if "__" in foldername:
                new_name = foldername.split("__")[1]
                old_path = os.path.join(path, foldername)
                new_path = os.path.join(path, new_name)
                os.rename(old_path, new_path)
                print(f"renamed from '{foldername}' to '{new_name}'.")
    except FileNotFoundError:
        print("path not found.")

parent_folder = "C:/test"
rename_folder(parent_folder)

:wave: Welcome @cadsl!

Is C:/test on your computer? If so, the Repl won’t be able to access that (I don’t see a test folder in your Repl).

3 Likes

thank you very much for answer my question, qwerty88

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.