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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:59:29+00:00 2026-06-10T09:59:29+00:00

when I run the program from the command line I get no errors and

  • 0

when I run the program from the command line I get no errors and it seems to execute, but nothing happens! I’m ready to stab my eyes out from staring at code for so long. I just want this to work so I can turn it in and be done with this assignment.

The program is supposed to run as follows.

python bulk.py (directory name) (name for files)

import os
import sys
import random


def filterByExtension(root, allfiles, extensions):
  matching = []
  ext = []
  for i in allfiles:
    name = i
    dot = name.rfind('.')
    ext = name[dot+1:].lower()
    if ext not in extensions:
      continue
    path = os.path.join(root, name)
    if not os.path.isfile(path):
      print "Warning: File type not expected"
      continue
      if os.path.isfile(path):
        matching.append(name)
  return matching    

def sortByMTime(path, matching):
  presort = []
  for file in matching:
    path = os.path.join(path, file)
    mtime = os.path.getmtime(path)
    presort.append((mtime, file))
    presort.sort()
  return presort
  print "Here is the presorted shtuff",presort


def assignNames(prefix, inorder):
  count = ''
  digits = 0
  count = str(len(inorder))
  digits = len(count)
  template = '%%0%dd' % digits
  newnames = {}
  count = 0
  for i in inorder:
    count += 1
    s = template % count
    newnames[i[1]] = prefix+s+'.'+i[1].split('.')[1]
  return newnames
  print "Here are the new names that will be used",newnames


def makeTempName(allfiles):
  n = random.randint(1, 1000000000)
  t = '__temp' + str(n) + '__'
  while t in allfiles:
    n += 1
    t = '__temp' + str(n) + '__'
  return t



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

        chain.reverse()
        print "this is the chain output before loop:"
    print chain
    for ( a, b ) in chain:
          print "This is the output of chain:"
          print chain
          script.append( a, b )
          del newnames[a]


          print 'here is the inthechain output in function'
          print inthechain
          print '=========================================='

          print 'here is the inorder output in function'
          print inorder
          print '=========================================='

          print 'here is the newnames output in function'
          print newnames
          print '=========================================='

          print 'here is the tempname output in function'
          print tempname
          print '=========================================='

          print 'here is the script output in function'
          print script
          print '=========================================='
  return script




def doRenames(pathfull, script):
  for entry in script:
    print entry[0], '->', entry[1]
    oldpath = os.path.join(path, entry[0])
    newpath = os.path.join(path, entry[1])
    if os.path.exists(newpath):
      print 'Error: file name already exists.'
      os.exit(1)
    else:
      os.rename(oldpath, newpath)




def main():
  directory = []
  prefix = []
  path = []
  tempname = []
  if len(sys.argv) <= 1 or len(sys.argv) > 3:
    print "You have messed up, please check your arguments again"
    sys.exit(1)

  elif len(sys.argv) == 3:    
    directory = sys.argv[1]
    path = os.path.abspath(directory)
    dirname = os.path.basename(path)
    print "Directory: ", sys.argv[1:-1]
    print "Prefix: ", sys.argv[-1]
    allfiles = []
    allfiles = os.listdir(sys.argv[1])
    print allfiles
    extensions = []
    extensions = ['jpeg','jpg','png','gif']
    matching = filterByExtension(path, allfiles, extensions)
    inorder = sortByMTime(path, matching)
    newnames = assignNames(prefix, inorder)
    tempname = makeTempName(allfiles)
    script = makeScript(inorder, newnames, tempname)
    renamed = doRenames(path, script)

  else:
    directory = sys.argv[1]
    path = os.path.abspath(directory)
    dirname = os.path.basename(path)    
    print "Directory: ", path
    print "Prefix: ", dirname









main()
  • 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-10T09:59:30+00:00Added an answer on June 10, 2026 at 9:59 am

    No file names will ever be added to matching here:

    if not os.path.isfile(path):
      print "Warning: File type not expected"
      continue
      if os.path.isfile(path):
        matching.append(name)
    

    Probably you wanted to have the second if in the outer block, not indented under the first if:

    if not os.path.isfile(path):
      print "Warning: File type not expected"
      continue
    if os.path.isfile(path):
      matching.append(name)
    

    Or, simpler:

    if not os.path.isfile(path):
      print "Warning: File type not expected"
    else:
      matching.append(name)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Can I run from command line program created by Eclipse? I am
I am trying to compile and run a program from the command line. When
I want to run the following command from a C program to read the
I need to run this line from my c++ program: java -jar test.jar text1
I'm able to run a program from the commmand line by typing java main
I need to run MSBuild from the command line using the Visual Studio Command
I am trying run a program from a qmake .pro file which modifies the
I'm using QProcess to run the s3 program from libs3 . QString S3::runS3(const QStringList
Is there a well-designed method that can run my ruby program from anywhere? I
When I run my program (which decrypts a paragraph from a certain document), I

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.