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

  • Home
  • SEARCH
  • 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 6724411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:41:13+00:00 2026-05-26T09:41:13+00:00

I am trying to write a small python script to rename a bunch of

  • 0

I am trying to write a small python script to rename a bunch of filenames by searching and replacing. For example:

Original filename:
MyMusic.Songname.Artist-mp3.iTunes.mp3

Intendet Result:
Songname.Artist.mp3

what i’ve got so far is:

#!/usr/bin/env python
from os import rename, listdir

mustgo = "MyMusic."
filenames = listdir('.')

for fname in fnames:
  if fname.startswith(mustgo):
    rename(fname, fname.replace(mustgo, '', 1))

(got it from this site as far as i can remember)

Anyway, this will only get rid of the String at the beginning, but not of those in the filename.

Also I would like to maybe use a seperate file (eg badwords.txt) containing all the strings that should be searched for and replaced, so that i can update them without having to edit the whole code.

Content of badwords.txt
MyMusic.
-mp3
-MP3
.iTunes
.itunes

I have been searching for quite some time now but havent found anything. Would appreciate any help!

Thank you!

  • 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-26T09:41:13+00:00Added an answer on May 26, 2026 at 9:41 am
    import fnmatch
    import re    
    import os
    
    with open('badwords.txt','r') as f:
        pat='|'.join(fnmatch.translate(badword)[:-1] for badword in 
                     f.read().splitlines())   
    
    for fname in os.listdir('.'):
        new_fname=re.sub(pat,'',fname)
        if fname != new_fname:
            print('{o} --> {n}'.format(o=fname,n=new_fname))
            os.rename(fname, new_fname)
    
    # MyMusic.Songname.Artist-mp3.iTunes.mp3 --> Songname.Artist.mp3
    
    • Note that it is possible for some files to be overwritten (and thus
      lost) if two names get reduced to the same shortened name after
      badwords have been removed. A set of new fnames could be kept and
      checked before calling os.rename to prevent losing data through
      name collisions.
    • fnmatch.translate takes shell-style patterns and returns the
      equivalent regular expression. It is used above to convert badwords
      (e.g. '.iTunes') into regular expressions (e.g. r'\.iTunes').
    • Your badwords list seems to indicate you want to ignore case. You
      could ignore case by adding '(?i)' to the beginning of pat:

      with open('badwords.txt','r') as f:
         pat='(?i)'+'|'.join(fnmatch.translate(badword)[:-1] for badword in 
                             f.read().splitlines())
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a small Python script to parse the .strings file in
I'm trying to write a small script to change the current directory to my
I'm trying to write a small script which parses and executes Brainfuck code, to
I'm trying to write a small ruby script that detects if a given argument
I am trying to write a small PHP script for managing subscriptions for a
Begginer's question. I'm trying to write small piece of code in Visual Studio (VB.net)
I'm trying to write a small class library for a C++ course. I was
I'm trying to write a small app that monitors how much power is left
I'm trying to write a small wsgi application which will put some objects to
I'm trying to write a small parser with Irony . Unfortunately I get a

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.