Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7828149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:13:33+00:00 2026-06-02T10:13:33+00:00

Here is my batch file rename and move script so far import os, re,

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T10:13:41+00:00Added an answer on June 2, 2026 at 10:13 am

    The problem has nothing to do with os.rename or os.chdir. You’re missing a closing parenthesis in the line before:

    os.rename(os.path.join(path, f),
              os.path.join(path, basename + '_' + randid + '_' + str(index) + ext) 
    #                                                                             ^
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here i need a batch file which can apply and create label or base
Here I have 2 requirements: Need a batch file to start a process on
I am trying to batch rename old log files, but the script only works
Here's my batch file to parse a directory, and zip files of certain type
I am coding a batch file. Here is a sample: if exist rootsys.txt del
So here's the dealio. I would like to create a simple batch file that'll
Here's a minimal batch file, demo.bat , to illustrate my problem: @ECHO off set
This is a batch file in Windows. Here is my .bat file @echo off
I have a simple batch file here which will print a word document from
I am trying to run a batch file. the file is located here: C:\Program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.