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: |
|||
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 |
||
# |
# find double extensions |
||
fn1, |
fn1, ext1 = os.path.splitext(file) |
||
fn2, ext2 = os.path.splitext(fn1) |
fn2, ext2 = os.path.splitext(fn1) |
||
⚫ | |||
if not ext2: |
|||
print (file, "has 1 extension") |
|||
elif ext2 == ext1: |
|||
print ( |
print (file, "has 2 Same extensions") |
||
# shutil.move(file, fn1.replace(ext,ext2)) |
|||
⚫ | |||
print (file, "has 2 Diff extensions") |
|||
# shutil.move(file, fn1.replace(ext,ext2)) |
|||
else: |
else: |
||
print ( |
print ("Something Wrong") |
||
print ("done") |
|||
</pre> |
</pre> |
||