コマンドラインからプログラムを実行すると、エラーは発生せず、実行されているように見えますが、何も起こりません! 長い間コードを見つめることから目をそらす準備ができています。これを機能させて、提出してこの課題を完了できるようにしたいだけです。
プログラムは次のように実行することになっています。
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()