Python Scripts: Difference between revisions

Line 685:
<syntaxhighlight lang="python">
#!/usr/bin/env python3
# This script will:
# Verify prevent Overwrite of existing files
# - Add extension if not present
# jpg = jpeg ignore
# - Remove double extensions
# - Correct Extensions
#
# Verify: prevent Overwrite of existing filesfunctions
# Verify: jpg = jpeg ignore
# Append _(1) if existing file
# write list of files before and after operation
# write modified files to list
Line 696 ⟶ 702:
import shutil
import sys
import tarfile
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()
 
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 711 ⟶ 741:
ftype = imghdr.what(file)
newname = file + "." + ftype
 
filechk2 = Path(newname)
if filechk2.is_file():
print (filechk2, "File already exists, not overwriting")
else:
shutil.move(file, newname)
Line 726 ⟶ 755:
filechk2 = Path(filechk)
if filechk2.is_file():
print (filechk2, "File already exists, not overwritting")
else:
shutil.move(file, fn1.replace(ext2,ext1))
Line 734 ⟶ 763:
filechk2 = Path(filechk)
if filechk2.is_file():
print (filechk2, "File already exists, not overwritting")
else:
shutil.move(file, fn1.replace(ext2,ext1))
Line 743 ⟶ 772:
print("Unique extensions done")
 
# Code to correct file Extensions
for root, dirs, files in os.walk(directory):
for name in files:
Line 755 ⟶ 785:
if ftype != ext:
if ftype != None:
if (ftype == "jpeg") & (ext == "jpg"):
print("File type is JPG/JPEG, ignoring")
else:
filechk = file.replace(ext,ftype)
filechk2 = Path(filechk)
if filechk2.is_file():
print (filechk2, "File already exists, not overwritting")
else:
# rename the file
Line 773 ⟶ 806:
 
print("Correcting extension done")
 
</syntaxhighlight>