![]() |
rename same file names in different directories - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: rename same file names in different directories (/thread-38609.html) |
rename same file names in different directories - elnk - Nov-04-2022 I have a directory which has 10 other dirs with many files: - directory - dir1 - dir2 - dir3 - ... How can i rename duplicate files from all dirs? For ex. we have 2 files with name "file1.txt" in "dir1" and "dir2" and we must rename one. RE: rename same file names in different directories - AdamHensley - Jul-11-2024 import os; base_dir='path/to/your/directory'; seen_files={}; [os.rename(os.pa Replace 'path/to/your/directory' with the actual path. This will rename duplicates by adding a counter. RE: rename same file names in different directories - menator01 - Jul-11-2024 Here one way import os # Path for executing script path = os.path.realpath(os.path.dirname(__file__)) # Get all folders in cyrrent folder folders = [folder for folder in os.listdir(f'{path}') if os.path.isdir(f'{path}/{folder}')] # Loop through folders for folder in folders: # Get all files in each folder files = os.listdir(f'{path}/{folder}') # Loop through files and change file name by adding folder prefix for file in files: os.rename(f'{path}/{folder}/{file}', f'{path}/{folder}/{folder}_{file}') RE: rename same file names in different directories - Larz60+ - Jul-12-2024 see this post. RE: rename same file names in different directories - Pedroski55 - Jul-12-2024 The files are different, because they lie in different paths. Change 1, the others will not change. I put a.txt and b.txt in the main folder and in 3 subfolders. If you really want to do this, (I think it is unescessary), maybe like this: from pathlib import Path def myApp(): mydir = Path('/home/pedro/temp/') file_name_list = [filename.name for filename in mydir.rglob("*") if filename.is_file()] len(file_name_list) # 95 # can't do this with a generator can't use .count(f) duplicates = [f for f in file_name_list if file_name_list.count(f) > 1] # get tuples of name and number of duplicates duplicates = [(f, file_name_list.count(f)) for f in file_name_list if file_name_list.count(f) > 1] # get rid of the duplicates in duplicates duplicates_set = set(duplicates) # can't change sets so convert the set to a list dl = list(duplicates_set) # now change each tuple in dl to a list # because lists are mutable for i in range(len(dl)): dl[i] = list(dl[i]) # a generator to get all file names file_name_gen = (filename for filename in mydir.rglob("*") if filename.is_file()) # for each file name look if it is 1 of the lists in dl for f in file_name_gen: for d in dl: if d[0] == f.name: print(f.name) newname = f.rename(Path(f.parent, f"{f.stem}_{d[1]}_{f.suffix}")) print(newname) # reduce the duplicate count by 1 d[1] = d[1] - 1Now, the numbering starts from the highest number for each subset of different duplicate files and decreases.
RE: rename same file names in different directories - snippsat - Jul-12-2024 Also when removing duplicates it can better to use file hash,then sure that file is a dublicate. It's easy to use as hashlib and also pathlib is in standard library. So rglob('*') is recursively and will iterate through all subdirectories. from pathlib import Path import hashlib import os def remove_duplicate(path: Path) -> Path: ''' Choice <path> will recursively iterate through all subdirectories Remove comment <os.remove> and will delete duplicate ''' unique = {} for file in Path(path).rglob('*'): if file.is_file(): with open(file, 'rb') as f: filehash = hashlib.md5(f.read()).hexdigest() if filehash not in unique: unique[filehash] = file else: # Test print before removing print(f'Removing --> {unique[filehash]}') '''try: os.remove(unique[filehash]) except OSError: pass''' if __name__ == '__main__': path = Path(r'G:\div_code\reader_env\my_folder') remove_duplicate(path) |