Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected NameError

Tags:

python

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'
like image 562
Axim Avatar asked Sep 24 '26 04:09

Axim


2 Answers

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 '/'

like image 137
gurney alex Avatar answered Sep 25 '26 18:09

gurney alex


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.

like image 31
Björn Pollex Avatar answered Sep 25 '26 17:09

Björn Pollex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!