Python Scripts: Difference between revisions

Line 680:
print("Exception::errorcode="+str(e.errorcode)+",message="+ e.message)
</pre>
 
 
= Extensions Doctor =
 
<pre>
#!/usr/bin/env python3
import os
import imghdr
import shutil
import sys
 
directory = sys.argv[1]
 
 
# Need to add a section for no extensions
# Need to write Results to a file in script dir
 
for root, dirs, files in os.walk(directory):
for name in files:
file = root+"/"+name
 
# find the correct extension
ftype = imghdr.what(file)
 
ext = os.path.splitext(file)[1][1:]
# find files with the (incorrect) extension to rename
if ftype != ext:
if ftype != None:
# rename the file
shutil.move(file, file.replace(ext,ftype))
print (file, ext, ("=>"), ftype)
 
# in case it can't be determined, mention it in the output
else:
print("could not determine: "+file)
# else:
# print (name, "Correct Extension")
 
# else:
# print "Unknown file type"
 
 
for root, dirs, files in os.walk(directory):
for name in files:
file = root+"/"+name
 
# remove double extensions
fn1, ext = os.path.splitext(file)
fn2, ext2 = os.path.splitext(fn1)
if ext == ext2:
print (ext2)
# Merge similar extensions
shutil.move(file, fn1.replace(ext,ext2))
print (fn2, ext, ("Dup ext removed:"), ext2)
else:
print (file, " has 1 extension")
 
</pre>
 
 
<br />