1

当我从命令行运行程序时,我没有收到任何错误,它似乎可以执行,但没有任何反应!我已经准备好从盯着代码看这么久了。我只是想让它起作用,这样我就可以把它上交并完成这项任务。

该程序应该按如下方式运行。

python bulk.py(目录名)(文件名)

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()
4

1 回答 1

5

这里永远不会添加任何文件名matching

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

可能您希望将第二个if放在外部块中,而不是在第一个下缩进if

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

或者,更简单:

if not os.path.isfile(path):
  print "Warning: File type not expected"
else:
  matching.append(name)
于 2012-08-28T00:57:57.457 回答