I have a bunch of pdf files which has the file name as follows:
And so on. I would like to change the name of the files to this format:
How do I do this in python? I do have a beginner level knowledge in python. I tried with the code taken from here
import os
path = os.getcwd()
filenames = os.listdir(path)
for filename in filenames:
os.rename(filename, filename.replace("_", ""))
With this code I could change the title from AuthorA_2014_ This is a good article to AuthorA2014 This is a good article, which deletes the underscores, but I do not want any empty spaces between the year and title of the article. How do I accomplish this?
I am using Python 3.7.7
This should get it done:
import os
path = os.getcwd()
filenames = os.listdir(path)
for filename in filenames:
os.rename(filename, filename.replace("_", "").replace("_ ", ""))