File size: 1,540 Bytes
26d4801
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
from   tqdm     import tqdm


def rename_files(input_dir: str, word: str, new_word: str = '') -> bool:
    """

    Desc: 

        This method renames the files that has {word} in the filename

    Args:

        input_dir (str): Path to the input directory 

        word (str): A word to look for in the filename

        new_word (str): A new word that is used to replace

    Returns:

        True, if renaming operation is success, else False.

    """
    try:        
        if not os.path.isdir(input_dir):
            print(f"The directory {input_dir} does not exist.")
            return False
        
        for file in tqdm(os.listdir(input_dir)):
            basename, ext = os.path.splitext(file)

            if word in basename:
                new_name = f"{basename.replace(word, new_word)}{ext}"
                
                # Construct full paths for renaming
                old_file_path = os.path.join(input_dir, file)
                new_file_path = os.path.join(input_dir, new_name)
                
                try:
                    # Rename the file
                    os.rename(old_file_path, new_file_path)
                except:
                    continue
        
        return True
    
    except Exception as rename_ex:
        print(f"An error occurred while renaming: {rename_ex}")
        return False

   


if __name__ == "__main__":
    inp_dir = ""
    word = ""
    new_word = ""
    rename_files(inp_dir, word, new_word)