當我從命令行運行程序時,我沒有得到任何錯誤,它似乎執行,但沒有任何反應!我已經準備好盯着代碼這麼久了。我只是想讓這個工作,所以我可以把它交給這個任務完成。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()
我的作業指導,如果有幫助。 [鏈接](http://cit.dixie.edu/cs/1410/asst_bulkrename.html) –
你是否縮小了代碼的任何特定區域?這裏有很多需要閱讀。 – jdi
你可以縮小它嗎?調試的一個關鍵是通過區分工作代碼和破壞代碼來隔離錯誤。什麼工作,哪裏出問題? –