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 8503513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:46:36+00:00 2026-06-11T01:46:36+00:00

This program is supposed to run from command line like this: python Filename Folder_photos_are_in

  • 0

This program is supposed to run from command line like this:

python Filename Folder_photos_are_in New_Prefix

It should just rename the files, but it wasn’t working, so I had it print out each function separately as it runs. It seems to work all right until the SortByMTime function at which time it deletes all of the files from my list except the last one.

Here is the code:

    import sys
    import os
    import random

    def filterByExtension(root, allfiles, extensions):
        files = []
        for f in allfiles:
            hasExt = f.rfind('.')
            if(hasExt > 0):
                ext = f[hasExt+1::].lower()
                if(ext in extensions):
                    f2 = os.path.join(root, f)
                    if(os.path.isfile(f2)):
                        files.append(f)
                    else:
                        print "Matching irregular file " + f
        return files


    def sortByMTime(root, matching):
        photos = []
        for f in matching:
            path = os.path.join(root, f)
            mtime = os.path.getmtime(path)
            photos.append((mtime, f))
            photos.sort()
        return photos

    def assignNames(prefix, inorder):
        kount = str(len(inorder))
        digits = len(kount)
        template = '%%0%dd' % digits
        newnames={}
        kount = 0
        for i in inorder:
            kount += 1
            s = template % kount
            newnames [i[1]] = prefix+s+'.'+i[1].split('.')[1]
        return newnames
        print newnames

    def makeTempName(allfiles):
        r = random.randrange(0,1000000000)
        name = "__temp%i__" % r
        while name in allfiles:
            r+=1
            name = "__temp%i__" % r
        return name

    def makeScript(inorder, newnames, tempname):
        chain = []
        inthechain = {}
        script = []
        for i in inorder:
            if i not in newnames:
                continue
            if newnames[i] == id:
                del newnames[i]
                continue
            if newnames[i] not in newnames:
                target = newnames[i]
                script.append( (i,target) )
                del newnames[i]
                    continue
            else:
                link = i
                while True:
                    target = newnames[i]
                    chain.append( (link, target) )
                    inthechain[link] = True
                    link = target
                    if link not in newnames:
                        break
            chain.reverse()
            for (a, b) in chain:
                print "This is in the chain: "
                print chain
                script.append(a,b)
                del newnames[a]
        return script

    def doRenames(root, script):
        for (old, new) in script:
            print "%s -> %s" %(old, new)
            fulloldpath=os.path.join(root, old)
                fullnewpath = os.path.join(root, new)
            if os.path.exists(fullnewpath):
                print "File already exists"
                os.exit(1)
            else:
                os.rename(fulloldpath, fullnewpath)




    def main():
        usrdir = []
        allfiles = []
        path = []
        prefix = ''
        args = sys.argv
        args.pop(0)  #remove first thing from list
        if len(args) == 2: #Directory and Prefix are provided
            print "Directory: ", args[0]
            print "Prefix: ", args[1]
            usrdir = args[0]
            path = os.path.abspath(usrdir)
            prefix = os.path.basename(path)
        if len(args) == 1: #Only directory is provided
            args.append(args[0]) #Makes the directory name the prefix as well
            print "Directory: ", args[0]
            print "Prefix: ", args[1]
            usrdir = args[0]
            path = os.path.abspath(usrdir)
            prefix = os.path.basename(path)
        if len(args) == 0 or len(args) > 2:  #ends the programs because wrong number of arguments.
            print "INVALID Number of Arguments:"
            print "Usage: python bulkrename.py <directory> [<prefix>]"    
            exit(1)
        allfiles = os.listdir(usrdir)
        print "Printout of allfiles"
        print allfiles
        print
        root = os.path.abspath(args[0])
        print "root: ", root
        print
        extensions = ['jpeg', 'jpg', 'png', 'gif']   
        print "What Extensions should be able to be used: " 
        print extensions
        print
        matching = filterByExtension(root, allfiles, extensions)
        print "What comes out of filterByExtension"
        print matching
        print
        inorder = sortByMTime(path, matching)
        print "What comes out of sortByMTime"
        print inorder
        print

        newnames = assignNames(prefix, inorder)
        print "What comes out of assignNames"
        print newnames
        print
        tempname = makeTempName(allfiles)
        print "What comes out of makeTempName"
        print tempname
        print
        script = makeScript(inorder, newnames, tempname)
        print "What comes out of makeScript"
        print script
        print
        doRenames(path, script)
        print "What comes out of doRenames"
        print doRenames
        print

    main()

and here is the output from terminal

    virus-haven:CS1410 chrislebaron$ python bulkrenamer.py bulkrename test
    Directory:  bulkrename
    Prefix:  test
    Printout of allfiles
    ['.DS_Store', '20120902Snow_Canyon02.JPG', '20120902Snow_Canyon03.JPG',         '20120902Snow_Canyon05.JPG', '20120902Snow_Canyon06.JPG', '20120902Snow_Canyon08.JPG',         '20120902Snow_Canyon09.JPG', '20120902Snow_Canyon11.JPG', '20120902Snow_Canyon12.JPG',         'airplane.png', 'BackNoText.jpg', 'blah', 'FrontNoText.jpg', 'glitchbusters.jpg',         'IMG_7663.JPG', 'IMG_7664.JPG', 'Pomegranates.jpg', 'rccar.png']

    root:  /Users/chrislebaron/Documents/School/CS1410/bulkrename

    What Extensions should be able to be used: 
    ['jpeg', 'jpg', 'png', 'gif']

    What comes out of filterByExtension
    ['20120902Snow_Canyon02.JPG', '20120902Snow_Canyon03.JPG', '20120902Snow_Canyon05.JPG', '20120902Snow_Canyon06.JPG', '20120902Snow_Canyon08.JPG', '20120902Snow_Canyon09.JPG', '20120902Snow_Canyon11.JPG', '20120902Snow_Canyon12.JPG', 'airplane.png', 'BackNoText.jpg', 'FrontNoText.jpg', 'glitchbusters.jpg', 'IMG_7663.JPG', 'IMG_7664.JPG', 'Pomegranates.jpg', 'rccar.png']

    What comes out of sortByMTime
    [(1322960835.0, 'rccar.png')]

    What comes out of assignNames
    {'rccar.png': 'bulkrename1.png'}

    What comes out of makeTempName
    __temp55210675__

    What comes out of makeScript
    []

    What comes out of doRenames
    <function doRenames at 0x100dede60>

    virus-haven:CS1410 chrislebaron$ 
  • 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-11T01:46:37+00:00Added an answer on June 11, 2026 at 1:46 am

    You’ve goofed your indentation, mixing spaces and tabs. Use python -tt to verify.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

when I run the program from the command line I get no errors and
Say suppose I am running a java program through command line. And this program
I have a program dnapars I execute the program from command line as following:
This program is supposed to determine how many units are stored in the value
I wrote this short program which has a tiny GUI. Its supposed to allow
I am supposed to modified this Sierpinski's triangle program to count the number of
This program runs just fine, but for some reason when I ask for an
This program I use has it's own variables to set when you run it,
Here is the line I'm having trouble with... Drag the synergys program from the
This is a program I'm writing that's supposed to display some text in a

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.