Here is my batch file rename and move script so far
import os, re, shutil, random
mainpath = 'C:\\Users\\s3z\\Desktop\\pytest'
dirs = [d for d in os.listdir('.') if os.path.isdir(d)]
for index, name in enumerate(dirs):
subpath = name
os.chdir(name)
images = [i for i in os.listdir('.') if i[-4:] in ('.png', '.jpg', 'jpeg')]
basename = re.sub(r'\W+', '', name[0:10])
randid = str(random.uniform(0,1))[-4:]
for index, f in enumerate(images):
path = os.path.join(mainpath, subpath)
if f[-4:] == 'jpeg':
ext = '.jpeg'
else:
ext = f[-4:]
os.rename(os.path.join(path, f), os.path.join(path, basename + '_' + randid + '_' + str(index) + ext)
shutil.move(f, '..\..\COMMON')
os.chdir(os.pardir)
I am having issues with the os.rename() function. When I run the script I get
File "bulk_image_organizer.py", line 19
shutil.move(f, '..\..\COMMON')
^
SyntaxError: invalid syntax
And when I take out line 19
shutil.move(f, '..\..\COMMON')
and try to run it again the error changes to
File "bulk_image_organizer.py", line 19
os.chdir(os.pardir)
^
SyntaxError: invalid syntax
But when I remove the os.rename() line the script works fine. Also when I run the os.rename() line in the Python interpretter in a for loop like in the following
>>> for index, f in enumerate(images):
... os.rename(os.path.join(path, f), os.path.join(path, "new" + st
r(index) + ".jpg"))
...
It works fine. So what is going haywire in my script?
The problem has nothing to do with
os.renameoros.chdir. You’re missing a closing parenthesis in the line before: