Python Scripts: Difference between revisions
→Extensions Doctor
Line 678:
</syntaxhighlight>
= Extensions
Source: [https://askubuntu.com/questions/631900/correct-file-extensions askubuntu.com]
<syntaxhighlight lang="python">
#!/usr/bin/env python3
# Append _(1) if existing file
# Do not run command recursively
# Duplicate remover
# Display File count before & after operation
import os
import re
import imghdr
import shutil
import sys
import
from pathlib import Path
directory = sys.argv[1]
# Code to add Ext if not present & remove double Ext
for root, dirs, files in os.walk(directory):
for name in files:
Line 741 ⟶ 713:
print(file, "Unsupported file")
else:
newname = file +"."+ ftype
if not ext1:
filechk2 = Path(newname)
if filechk2.is_file():
print (filechk2, "File already
else:
shutil.move(file, newname)
Line 751 ⟶ 723:
else:
if not ext2:
# print("1 Extension only")
continue
elif ext2 == ext1:
filechk = file.replace(ext1,ftype)
filechk2 = Path(filechk)
if filechk2.is_file():
print (filechk2, "File already
else:
shutil.move(file, fn1.replace(ext2,ext1))
Line 765 ⟶ 737:
filechk2 = Path(filechk)
if filechk2.is_file():
print (filechk2, "File already
else:
shutil.move(file, fn1.replace(ext2,ext1))
print (file, "has 2 Diff ext, Removing:", ext2)
else:
print ("Something
print("
print("---===========Unique extensions done=============---")
print("\n\n")
# Code to Correct File Extensions
for root, dirs, files in os.walk(directory):
for name in files:
Line 788 ⟶ 763:
if ftype != None:
if (ftype == "jpeg") & (ext == "jpg"):
# print(file, "File type is JPG/JPEG, ignoring")
else:
filechk = file.replace(ext,ftype)
filechk2 = Path(filechk)
if filechk2.is_file():
print (filechk2, "File already
else:
# rename the file
Line 800 ⟶ 776:
# in case it can't be determined, mention it in the output
else:
filechk = file.replace(ext,"jpg")
filechk2 = Path(filechk)
if filechk2.is_file():
print (filechk2, "File already EXISTS, not overwritting")
else:
shutil.move(file, file.replace(ext,"jpg"))
print (file, "File type not determined for PNG =>", file.replace(ext,"jpg"))
else:
print(file, "Could not determine file type")
else:
# print(file, "Correct Extension")
else:
print(file, "No Extension detected")
print("
print("---===============Correcting extension done================---")
print("\n")
# Code to convert WEBP to PNG
for root, dirs, files in os.walk(directory):
for name in files:
file = root + "/" + name
fn, ext = os.path.splitext(file)
if ext == ".webp":
fnpng = fn + ".png"
fpath = Path(fnpng)
if fpath.is_file():
print (fnpng, "File already EXISTS, not overwritting")
else:
conv = subprocess.Popen(["dwebp", file, "-o", fnpng], stdout=subprocess.PIPE)
output, err = conv.communicate()
rmfile = subprocess.Popen(["rm", file], stdout=subprocess.PIPE)
output2, err2 = rmfile.communicate()
else:
continue
# print (file, "File is not Webp, Skipping")
print("\n")
print("---==================WEBP to PNG conversion done===============---")
print("\n")
# Code to replace : with _ in file names
for root, dirs, files in os.walk(directory):
for name in files:
file = root + "/" + name
if re.search(r':', file):
filechk = file.replace(":","_")
filechk2 = Path(filechk)
if filechk2.is_file():
print (filechk2, "File already EXISTS, not overwriting")
else:
# rename the file
shutil.move(file, file.replace(":","_"))
print (file, ("Colon => "), file.replace(":","_"))
else:
continue
# print(name, " Colon Not Found in name")
print("\n")
print("---===============Colon replace space done=============---")
print("\n")
print("---=====================All Done=======================---")
print("\n")
</syntaxhighlight>
|