Unexpected NameError
<p>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.</p>

<p>Here's what I've got so far:</p>

<pre><code>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
</code></pre>

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

<pre><code>&gt;&gt;&gt; def f(s):
    return 'S08'+s

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

Traceback (most recent call last):
  File "&lt;pyshell#39&gt;", line 1, in &lt;module&gt;
    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
</code></pre>

<p>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.</p>

<p>Can somebody please help me figure out where I went wrong?</p>

<p>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:</p>

<pre><code>&gt;&gt;&gt; exec('cat="test"')
&gt;&gt;&gt; cat
'test'
</code></pre>
