Python Scripts: Difference between revisions

Content added Content deleted
Line 693: Line 693:


directory = sys.argv[1]
directory = sys.argv[1]



# Need to add a section for no extensions
# Need to add a section for no extensions
Line 704: Line 703:
# find the correct extension
# find the correct extension
ftype = imghdr.what(file)
ftype = imghdr.what(file)

ext = os.path.splitext(file)[1][1:]
ext = os.path.splitext(file)[1][1:]

# find files with the (incorrect) extension to rename
# find files with the (incorrect) extension to rename
if ftype != ext:
if ftype != ext:
if ftype != None:
if ftype != None:

# rename the file
# rename the file
shutil.move(file, file.replace(ext,ftype))
shutil.move(file, file.replace(ext,ftype))
Line 716: Line 716:
else:
else:
print("could not determine: "+file)
print("could not determine: "+file)
# else:
else:
# print (name, "Correct Extension")
print (name, "correct extension")


print ("done")
# else:
</pre>
# print "Unknown file type"


;List files with double extensions:
<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


for root, dirs, files in os.walk(directory):
for root, dirs, files in os.walk(directory):
Line 727: Line 738:
file = root+"/"+name
file = root+"/"+name


# remove double extensions
# find double extensions
fn1, ext = os.path.splitext(file)
fn1, ext1 = os.path.splitext(file)
fn2, ext2 = os.path.splitext(fn1)
fn2, ext2 = os.path.splitext(fn1)

if ext == ext2:
print (ext2)
if not ext2:
# Merge similar extensions
print (file, "has 1 extension")
shutil.move(file, fn1.replace(ext,ext2))
elif ext2 == ext1:
print (fn2, ext, ("Dup ext removed:"), ext2)
print (file, "has 2 Same extensions")
# shutil.move(file, fn1.replace(ext,ext2))
elif ext2 != ext1:
print (file, "has 2 Diff extensions")
# shutil.move(file, fn1.replace(ext,ext2))
else:
else:
print (file, " has 1 extension")
print ("Something Wrong")


print ("done")
</pre>
</pre>