我正在为此完成一项学校作业http://cit.dixie.edu/cs/1410/asst_bulkrename.html。我刚刚完成编码,现在无法弄清楚我做错了什么。当我从命令行运行它使用“python bulk.py sub iphone”“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
我认为我已经长时间盯着代码并完全遗漏了一些东西。任何帮助都会很棒。
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()
答案 0 :(得分:1)
在filterByExtension
中,return matching
位于for循环中,因此如果None
为空或每个条目,则可以跳过(导致返回值为allfiles
)被continue
跳过。修复缩进以删除此错误。