Rename the files using the folder name and copy, move to new directory.

Hello everyone in the python community, I’m very new to programming and if anyone can help me, in the future I want to be able to repay the kindness. I’m not an expert as I mentioned before, but I have a first idea for the logic of the script to begin. I can’t say whether what I think would be the best way to do it, so I’m asking for everyone’s help.

The script proposal:
Select a directory, within this directory there are several folders and subfolders, you need to find a certain extension, rename it and move it.

Imagine that “directory_x” has several “folders_a, folders_b, folders_c…”
Within these folders there are several subfolders. My challenge is finding a specific “extension.y”, copying it, moving it with the same name as “folders_a, folders_b, folders_c…”, not using the subfolder names. Within the “directory_x” the “folders_a, folders_b, folders_c…” are organized, but the subfolders are not organized. This is why I need a script for myself.


  • Process 1: Select a specific directory (we can call it “directory_x”), with several folders (we can call them “folders_a, folders_b, folders_c…”), and within each folder there are several subfolders and files archiver (*zip, *rar, etc.)
  • Process 2: Find all compressed files within “directory_x”. Extract these files to the same original location, in a folder with the same name. Example, extract and create folder Nome-01 and keeping the location where the zip or rar file was found.
  • Process 3: Find files with “extension.y”, copy it and move it to “directory_x”, keeping the name of “folders_a, folders_b, folders_c…” where it was found. Example: “folder_a” file of “extension.y” was found, rename it to “folder_a.y” and move it to the selected direct “directory_x”'. If there is more than one “extension.y” file within a subfolder, rename it and copy it, adding a character to not subscribe.

Print result would be:

  • directory_x
  • folder_a
  • folder_b
  • folder_c
  • folder_a.y
  • folder_b.y
  • folder_c.y
  • folder_c1.y
  • folder_c2.y

Aaand, which part of this homework are you having problems with?

I have the following problem:

  • Root Directory, there are several Folders1, Folders2, Folders3… and within these Folders, there are other subfolders, I need to move all the files from the subfolders to their folders, and if I hear the same files, add a character to avoid subwriting.

Problematic structure:

Solved Structure:

Below is a graph to try to better explain my doubts.

Okay, so why does this cause a problem? I see you’ve made an attempt in the first post to describe the process you want the code to follow - what exact difficulty do you encounter if you try to implement it yourself? In particular, does it seem like there’s a specific part of the task you don’t know how to complete (e.g. do you know how to move a file)? Or else, did something go wrong in your attempt?