sreeraj t
sreeraj t

Reputation: 2339

How to replace characters and rename multiple files?

I have a bunch of pdf files which has the file name as follows:

  1. AuthorA_2014_ This is a good article
  2. BIsanotherAuthor_1994_ Gr8 artcle
  3. CIsFatherOfB_1994_Minor article but not bad

And so on. I would like to change the name of the files to this format:

  1. AuthorA2014This is a good article
  2. BIsanotherAuthor1994Gr8 artcle
  3. CIsFatherOfB1994Minor article but not bad

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

Answers (4)

Trenton McKinney
Trenton McKinney

Reputation: 62373

Using pathlib:

  • This module offers classes representing filesystem paths with semantics appropriate for different operating systems.
  • Given a pathlib object created with t = Path.cwd() / 'test_foo_ bar.txt'
    • .rglob to find all the .pdf files
    • WindowsPath('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 file
from 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

Ankush Singh Gandhi
Ankush Singh Gandhi

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

Nick
Nick

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

pecey
pecey

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

Related Questions