I am trying to rename a number of files using Python so that they follow a new naming convention that looks like:
~/directory/yyyy + qq + directory_name + ' Letter'.
Right now, they are in this format:
~/directory/directory_name + yyyy + qq + ' Letter'.
So for example, I have a directory called /Users/Test/rename_test/Salmon 2
, and in it are the following files:
/Users/Test/rename_test/Salmon 2/Salmon 2 2013 Q4 Letter.pdf
/Users/Test/rename_test/Salmon 2/Salmon 2 2018 Q1 Letter.pdf
/Users/Test/rename_test/Salmon 2/Salmon 2 2015 Q2 Letter.pdf
I'd like to rename all of those files to:
/Users/Test/rename_test/Salmon 2/2013 Q4 Salmon 2 Letter.pdf
/Users/Test/rename_test/Salmon 2/2018 Q1 Salmon 2 Letter.pdf
/Users/Test/rename_test/Salmon 2/2015 Q2 Salmon 2 Letter.pdf
I've looked at using os.split
to extract positions [-2]
and [-3]
—since those should always be qq and yyyy—and then renaming the file by moving them to positions [0]
and [1]
. But I have hundreds of directories and thousands of files so I'm worried that one typo or file that deviates from the current convention may result in an error.
So what's the best way to approach this?