Axim
Axim

Reputation: 332

Unexpected NameError

I'm writing a script to batch-rename all files inside a folder. I'm trying to make it modular, so the core algorithm (the one that generates the new file name) is easily exchangeable.

Here's what I've got so far:

from os import listdir, rename

def renamer(path, algorithm, counter=False, data=None, data2=None, safe=True):

    call_string = 'new=algorithm(i'
    if counter:
        call_string += ', file_index'
    if data != None:
        call_string += ', data'
    if data2 != None:
        call_string += ', data2'
    call_string += ')'

    if safe:
        print('Press Enter to accept changes. '+
        'Type anything to jump to the next file.\n')

    files_list = listdir(path)
    for i in files_list:
        file_index = files_list.index(i)
        old = i
        exec(call_string)
        if safe:
            check = input('\nOld:\n'+old+'\nNew:\n'+new+'\n\nCheck?\n\n')
            if check is not '':
                continue
        rename(path+old, path+new)

    return

Now for some reason (seems unexplicable to me), calling the function raises NameError:

>>> def f(s):
    return 'S08'+s

>>> path='C:\\Users\\****\\test\\'
>>> renamer(path, f)
Press Enter to accept changes. Type anything to jump to the next file.

Traceback (most recent call last):
  File "<pyshell#39>", line 1, in <module>
    renamer(path, f)
  File "C:\Python32\renamer.py", line 25, in renamer
    check = input('\nOld:\n'+old+'\nNew:\n'+new+'\n\nCheck?\n\n')
NameError: global name 'new' is not defined

Unexplicable because, at line 25 it should already have executed call_string, thereby defining the name new. I've been trying to figure out my mistake for over an hour now, I've went through the entire code entering line for line into the shell twice, and it worked fine, and I can't seem to figure out the problem.

Can somebody please help me figure out where I went wrong?

Edit: I've already guessed it might be possible you can't assign names using exec, so I've tested it as follows, and it worked:

>>> exec('cat="test"')
>>> cat
'test'

Upvotes: 3

Views: 682

Answers (4)

gurney alex
gurney alex

Reputation: 13645

don't use exec or eval for this, and just write

new = algorithm(i, file_index, data, data2)

make sure all your algorithms can use the 4 arguments (ignoring those they don't need).

If you don't like this, the following is much more pythonic and efficient than using eval:

args = [i] 
if counter:
    args.append(file_index)
for arg in (data, data2):
    if arg is not None:
        args.append(arg)

new = algorithm(*args)

Also replace the ugly:

for i in files_list:
    file_index = files_list.index(i)

with

for index, filename in enumerate(file_list):
    ...

finally, use os.path.join to concatenate paths parts instead of string concatenation. This will save you debugging time when you call the function with a directory name without a trailing '/'

Upvotes: 3

Ned Batchelder
Ned Batchelder

Reputation: 375494

You don't need exec. You can adjust the arguments passed to the function so that it adapts:

def renamer(path, algorithm, counter=False, data=None, data2=None, safe=True):

    if safe:
        print('Press Enter to accept changes. '+
        'Type anything to jump to the next file.\n')

    files_list = listdir(path)
    for file_index, old in enumerate(files_list):
        opt_args = []
        if counter:
            opt_args.append(file_index)
        if data is not None:
            opt_args.append(data)
        if data2 is not None:
            opt_args.append(data2)
        new = algorithm(old, *opt_args)
        if safe:
            check = input('\nOld:\n'+old+'\nNew:\n'+new+'\n\nCheck?\n\n')
            if check:
                continue
        rename(path+old, path+new)

A few other minor points: use "is not None" instead of "!= None"; to see if a string is empty, just use "if check"; and you don't need a bare return at the end of a function. I've also included the enumerate improvement suggested by @gurney alex.

Upvotes: 1

Bj&#246;rn Pollex
Bj&#246;rn Pollex

Reputation: 76778

You declare the name new inside the exec-call. It is not visible outside. That is why the the error is generated when you try to access new after the call to exec, and not inside exec.

I do not see any reason why you would use exec here in the first place. The way you build call_string, you could just call algorithm directly.

If you really want your algorithms to be able to take variable arguments, use keyword arguments.

Upvotes: 1

agf
agf

Reputation: 176750

Change call_string = 'new=algorithm(i' to call_string = 'algorithm(i' and change exec(call_string) to new = eval(call_string) and you shouldn't have any problems.

Upvotes: 0

Related Questions