Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7642615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:14:04+00:00 2026-05-31T09:14:04+00:00

Sorry, but I can’t figure this out from the Python documentation or any of

  • 0

Sorry, but I can’t figure this out from the Python documentation or any of the stuff I’ve found from Google.

So, I’ve been working on renaming files with code 99% from one of the awesome helpers here at StackOverflow.

I’m working on putting together a renaming script that (and this is what I got help with from someone here) works with the name (not the extension).

I’m sure I’ll come up with more replacements, but my problem at the moment is that I can’t figure out how to do more than one re.sub.
Current Code (Replaces dots with spaces):

import os, shutil, re

def rename_file (original_filename):
    name, extension = os.path.splitext(original_filename)
    #Remove Spare Dots
    modified_name = re.sub("\.", r" ", name)
    new_filename = modified_name + extension
    try:
        # moves files or directories (recursively)
        shutil.move(original_filename, new_filename)
    except shutil.Error:
        print ("Couldn't rename file %(original_filename)s!" % locals())
[rename_file(f) for f in os.listdir('.') if not f.startswith('.')]

Hoping to also

re.sub("C126", "Perception", name)
re.sub("Geo1", "Geography", name)

Also, it’d be awesome if I could have it capitalize the first letter of any word except “and|if”

I tried

modified_name = re.sub("\.", r" ", name) && re.sub(... 

but that didn’t work; neither did putting them on different lines. How do I do all the subs and stuff I want to do/make?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:14:05+00:00Added an answer on May 31, 2026 at 9:14 am

    Just operate over the same string over and over again, replacing it each time:

    name = re.sub(r"\.", r" ", name)
    name = re.sub(r"C126", "Perception", name)
    name = re.sub(r"Geo1", "Geography", name)
    

    @DanielRoseman is right though, these are literal patterns that don’t need regexes to be described/found/replaced. You can use timeit to demonstrate plain old replace() is preferrable:

    In [16]: timeit.timeit("test.replace('asdf','0000')",setup="test='asdfASDF1234'*10")
    Out[16]: 1.0641241073608398
    
    In [17]: timeit.timeit("re.sub(r'asdf','0000',test)",setup="import re; test='asdfASDF1234'*10")
    Out[17]: 6.126996994018555
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

sorry this is such a simple question but I can't figure it out. How
Sorry, I'm sure this is simple but I'm tired and can't figure it out.
Sorry if this has been asked elsewhere. I have looked but can't find any
I've tried searching through search engines,MSDN,etc. but can't anything. Sorry if this has been
Sorry for this newbie question, but I can't find on google what I need
Sorry if this has already been answered, but I can't find it if so.
Sorry that I can't find the answer through google - But I know this
I'm sorry if this is too obvious but I can't find any proper answer
Sorry if this seems like a stupid question but can't find any info on
Sorry if this has been asked but can't find anything. I have a table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.