Python Script to Remove Characters and Replace in Filenames

Viewed 3770

I have a python script that looks at all the files within a folder for a specific word and replaces that word with a space. Instead of changing the words to look at after each time running the script, I'd like to continue to add new words for the script to look for and perform the same replace action.

I'm running this on macOS El Capitan. Below is the script:

import os

paths = (os.path.join(root, filename)
        for root, _, filenames in os.walk('/Users/Test/Desktop/Test')
        for filename in filenames)

for path in paths:
    # the '#' in the example below will be replaced by the '-' in the filenames in the directory
    newname = path.replace('.File',' ')
    if newname != path:
        os.rename(path, newname)

for path in paths:
    # the '#' in the example below will be replaced by the '-' in the filenames in the directory
    newname = path.replace('Generic',' ')
    if newname != path:
        os.rename(path, newname)

Any help you can provide to this newbie would be appreciated.

2 Answers
Related