0

私は学校の課題http://cit.dixie.edu/cs/1410/asst_bulkrename.htmlのためにこれに取り組んでいます。コーディングを終えたばかりで、何が間違っていたのか理解できません。「pythonbulk.pysubiphone」「sub」を画像のフォルダー、「iphone」をプレフィックスとして使用してコマンドラインから実行すると、次のエラーが発生します。

linux@ubuntu:~/Documents$ python bulk.py sub iphone
Directory: ['sub']
Prefix: iphone
['pic 4', 'pic 5', 'red sky (another copy).jpg', 'pic 3', 'pic 8 ', 'red sky (copy).jpg', 'pic 7', 'pic 6', 'red sky.jpg', 'pic 1', 'pic 2', 'red sky (4th copy).jpg', 'red sky (3rd copy).jpg']
Traceback (most recent call last):
File "bulk.py", line 173, in <module>
main()
File "bulk.py", line 152, in main
inorder = sortByMTime(path, matching)
File "bulk.py", line 37, in sortByMTime
for file in matching:
TypeError: 'NoneType' object is not iterable

iv'eはコードを長い間見つめていましたが、何かが完全に欠けていると思います。どんな助けでも素晴らしいでしょう。

import os
import sys
import random

directory = []
prefix = []
path = []




def filterByExtension(root, allfiles, extensions):
  matching = []
  ext = []
  for i in allfiles:
    name = i
    dot = name.rfind('.')
    ext = name[dot::].lower()
    if ext not in extensions:
      continue
    else:
      path = os.path.join(root, name)
      if not os.path.isfile(path):
        print "Error 404, file not found. Loading"
        print "blue screen of death in 10 seconds"
        print "unless mistake is corrected accordinly"
        continue
        matching.append(name)
    return matching





def sortByMTime(root, matching):
  presort = []
  for file in matching:
    path = os.path.join(root, file)
    mtime = os.path.getmtime(path)
    presort.append((mtime, file))
    presort.sort()
  print "Here is the presorted shtuff",presort
  return 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]
  print "Here are the new names that will be used",newnames
  return newnames





def makeTempName(allfiles):
  n = random.randant(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
    elif newnames[elt] == elt:
      del newnames[elt]
      continue
    elif newnames[elt] not in newnames:
      print "Script Output"
      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()
    for (a,b) in chain:
      script.append(a,b)
      del newnames[a]
  return script




def doRenames(path, 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 404, file name seems to alrady exist"
    sys.exit(1)
  os.rename(oldpath, newpath)





def main():

  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

1

はforループ内にあるため、が空の場合、または各エントリが。でスキップされる場合は、スキップできます(戻り値はfilterByExtension)になります。インデントを修正して、このバグを削除します。return matchingNoneallfilescontinue

于 2012-08-27T23:31:24.147 に答える