Python Scripts: Difference between revisions

Line 678:
</syntaxhighlight>
 
= Extensions Doctorcorrector =
Source: [https://askubuntu.com/questions/631900/correct-file-extensions askubuntu.com]
 
<syntaxhighlight lang="python">
#!/usr/bin/env python3
 
# This script will:
# - Add extension if not present
# - Remove double extensions
# - Correct Extensions
#
# Verify: prevent Overwrite functions
# Verify: jpg = jpeg ignore
#
# Unsupported PNG -> JPG
# WEBP Convertor to PNG
# Space in Path
# Verbose Output to file
# File in Dest address
# Append _(1) if existing file
# Do not run command recursively
# Duplicate remover
# Display File count before & after operation
# : replace _
 
import os
import re
import imghdr
import shutil
import sys
import tarfilesubprocess
from pathlib import Path
 
directory = sys.argv[1]
 
# Code for Archiving existing files & save list of files
wr = open("file_list.txt", "w")
wr.write("List of files:\n")
wr.write("\n")
wr.close()
 
# Code to add Ext if not present & remove double Ext
for root, dirs, files in os.walk(directory):
tardir = root + '.backup.tar.gz'
tar = tarfile.open(tardir, "w:gz")
for name in files:
file = root+"/"+name
tar.add(file)
wr = open("file_list.txt", "a")
wr.write(name)
wr.write("\n")
wr.close()
tar.close()
print("Done Backing up files")
 
# Code to make Extensions Unique
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 existsEXISTS, not overwriting")
else:
shutil.move(file, newname)
Line 751 ⟶ 723:
else:
if not ext2:
# print("1 Extension only")
continue
print("1 Extension only")
elif ext2 == ext1:
filechk = file.replace(ext1,ftype)
filechk2 = Path(filechk)
if filechk2.is_file():
print (filechk2, "File already existsEXISTS, not overwritting")
else:
shutil.move(file, fn1.replace(ext2,ext1))
Line 765 ⟶ 737:
filechk2 = Path(filechk)
if filechk2.is_file():
print (filechk2, "File already existsEXISTS, not overwritting")
else:
shutil.move(file, fn1.replace(ext2,ext1))
print (file, "has 2 Diff ext, Removing:", ext2)
else:
print ("Something Wrongwrong with file or logic")
 
print("Unique extensions done\n\n")
print("---===========Unique extensions done=============---")
print("\n\n")
 
 
# Code to correct file Extensions
# 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 type is JPG/JPEG, ignoring")continue
# 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 existsEXISTS, not overwritting")
else:
# rename the file
Line 800 ⟶ 776:
# in case it can't be determined, mention it in the output
else:
print(file,if "Couldext not== determine file type")png":
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:
#continue
# print(file, "Correct Extension")
else:
print(file, "No Extension detected")
 
print("Correcting extension done\n")
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>