qwerty
qwerty

Reputation: 93

Replace part of filename if it's found in a list (Python)

I have a piece of code the majority of which I got from another question here. After making a few adjustments it doesn't seem to work though. The purpose is simple: I have a directory with many files, all of which contain garbage words/characters that need to be replaced with different things.

I have 2 lists with those words. The code looks like this:

import os, string
rootdir = r'C:\Users\me\Downloads\Documents\Folder'
lst = ["this that", "This.that", "THIS.THAT", "this_that", "This.That"]
lst2 = ["_","__","___","."]
for filename in os.listdir(rootdir):
    if any(i in filename for i in lst):
        filepath = os.path.join(rootdir, filename)
        newfilepath = os.path.join(rootdir, filename.replace(i, ""))
        os.rename(filepath, newfilepath)
    if any(i in filename for i in lst2):  
        filepath = os.path.join(rootdir, filename)
        newfilepath = os.path.join(rootdir, filename.replace(i, " "))
        os.rename(filepath, newfilepath)

But when I run it the error is NameError: name 'i' is not defined Not sure why that happens, i should be defined as 'part of the filename that appears in the list`. At least that's what I think. Tried defining it in different ways but no luck.

Any ideas how to fix this? Thank you

Upvotes: 1

Views: 106

Answers (1)

Keisle
Keisle

Reputation: 48

As han solo and Vaibhav have pointed out, the variable i only exits within the scope of the for loop. Since you use a single line for loop, the variable is not accessible in the rest of the code. I'd suggest using a multiple line for loop like so:

import os, string
rootdir = r'C:\Users\me\Downloads\Documents\Folder'
lst = ["this that", "This.that", "THIS.THAT", "this_that", "This.That"]
lst2 = ["_","__","___","."]
for filename in os.listdir(rootdir):
    for i in lst:
        if i in filename:
            filepath = os.path.join(rootdir, filename)
            newfilepath = os.path.join(rootdir, filename.replace(i, ""))
            os.rename(filepath, newfilepath)
            #No need to loop through other possibilities if we've found a match
            break
    for i in lst2:
        if i in filename:
            filepath = os.path.join(rootdir, filename)
            newfilepath = os.path.join(rootdir, filename.replace(i, " "))
            os.rename(filepath, newfilepath)
            break

Doesn't look as pretty, but it should work!

Also, might I suggest checking your list against a lowercase filename string as to not accidentally skip over a filename with an uppercase character somewhere within the string? Might be a good idea if you don't want to keep your search case-sensitive! Here's a suggestion code:

import os, string
rootdir = r'C:\Users\me\Downloads\Documents\Folder'
lst = ["this that", "This.that", "THIS.THAT", "this_that", "This.That"]
lst2 = ["_","__","___","."]
for filename in os.listdir(rootdir):
    for i in lst:
        if i.lower() in filename.lower():
            filepath = os.path.join(rootdir, filename)
            newfilepath = os.path.join(rootdir, filename.replace(i, ""))
            os.rename(filepath, newfilepath)
            #No need to loop through other possibilities if we've found a match
            break
    for i in lst2:
        if i.lower() in filename.lower():
            filepath = os.path.join(rootdir, filename)
            newfilepath = os.path.join(rootdir, filename.replace(i, " "))
            os.rename(filepath, newfilepath)
            break

Upvotes: 1

Related Questions