Python Forum
Rename files in two different folders
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rename files in two different folders
#1
Hi everyone, I have a problem to solve. I have two folders like this

FOLDER 1:
- 05.xml
- 06.xml
- 07.xml
- 08.xml

FOLDER 2:
- 01_2500.jpeg
- 02_2501.jpeg
- 03_2502.jpeg
- 04_2503.jpeg
- 05_2504.jpeg
- 06_2505.jpeg
- 07_2506.jpeg
- 08_2507.jpeg

I need to rename the folder 1 as follows:
- 2504.xml
- 2505.xml
- 2506.xml
- 2507.xml

How can I do it? I have been trying this for days. Obviously this is a simplified example, because there are actually thousands of files to rename.

I thought that I could use split function for the folder 2 and then apply if, but I can't.
Reply
#2
If the files in your second folder are defining the names, then you can read the files of this folder (see: https://stackoverflow.com/questions/3207...-directory)
since the last digits of the files in both folders match you can use the split function on the .jpeg file names
for example
some_file_2dir = '07_2506.jpeg'
new_name = '%s.xml'%some_file_2dir.split('_')[1].split('.')[0]
and since you can call system functions like the move command mv you can then move the files of the first folder to the same location but with the new_name. Remember to check the names of the files in the first directory :)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020