Reputation: 2339
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
Upvotes: 1
Views: 2139
Reputation: 62373
t = Path.cwd() / 'test_foo_ bar.txt'
.rglob
to find all the .pdf
filesWindowsPath('E:/PythonProjects/stack_overflow/test_foo_ bar.txt')
t.stem
is 'test_foo_ bar'
t.suffix
is '.txt'
t.parent
is WindowsPath('E:/PythonProjects/stack_overflow')
t.parent / 'new_name.txt'
is WindowsPath('E:/PythonProjects/stack_overflow/new_name.txt')
t.rename(...)
renames the filefrom pathlib import Path
p = Path.cwd() # for current working directory or Path('/some_path/files')
for file in p.rglob('*.pdf'): # get all pdfs in all subdirectories
new_file_name = file.stem.replace('_', '').replace('_ ', '') + file.suffix
file.rename(file.parent / new_file_name)
Upvotes: 1
Reputation: 9
import re
import os
path = os.getcwd()
files = os.listdir(path)
for file in files:
os.rename(file, re.sub(r'_ ?', '', file))
Upvotes: 1
Reputation: 147146
You could use a regex to remove an _
with an optional trailing space:
import re
import os
path = os.getcwd()
filenames = os.listdir(path)
for filename in filenames:
os.rename(filename, re.sub(r'_ ?', '', filename))
Upvotes: 2
Reputation: 681
This should get it done:
import os
path = os.getcwd()
filenames = os.listdir(path)
for filename in filenames:
os.rename(filename, filename.replace("_", "").replace("_ ", ""))
Upvotes: 2