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'
don’t use exec or eval for this, and just write
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:
Also replace the ugly:
with
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 ‘/’