Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import os,errno,shutil,glob,htmlmin,codecs;
# Delete in Background
def trmSilentDelete(filename):
try:
os.remove(filename)
except OSError as e:
if e.errno != errno.ENOENT: #No Raise Exception if no such file or directory
raise # Raise Exception other Error
# Replace Content in File With
def trmReplaceWith(filename,filetype,old_text,new_text):
# Create Temp File with Replaced text
fA = codecs.open(filename + filetype, 'r',"utf-8")
fB = codecs.open(filename + '_temp'+filetype, 'w',"utf-8")
for line in fA:
fB.write(line.replace(old_text, new_text))
fA.close()
fB.close()
# Copy Temp to Orignal
shutil.copy(filename + '_temp'+filetype, filename +filetype)
# Delete Temp
trmSilentDelete(filename + '_temp'+filetype)
# trmReplaceWith for Looped for a folder
def trmReplaceFolder(filetype,old_text,new_text,foldersub=""):
lsfiles = glob.glob(foldersub+"*" + filetype) # Replace Que
for replaceque in lsfiles:
trmReplaceWith(replaceque[:-len(filetype)],filetype,old_text,new_text)
# trmReplaceFolder for every folder
def trmReplaceAllfolder(filetype,old_text,new_text,walk_dir='.',recursive=True):
allSubFolders=next(os.walk(walk_dir))[1]
print(allSubFolders);# Print current folder for debugging crashes
for foldersub in allSubFolders:
print("Currently in "+foldersub);
trmReplaceFolder(filetype, old_text, new_text,walk_dir+"/" +foldersub+"/");
if (recursive==True):
trmReplaceAllfolder(filetype,old_text,new_text,walk_dir+"/" +foldersub);
if walk_dir == '.':
trmReplaceFolder(filetype, old_text, new_text)
#Removes Extra Index Files Generated by WordPress
garbageHTMLSearch = glob.glob("*.html");
for WebHTMLFile in garbageHTMLSearch:
if (("index" in WebHTMLFile) and (len(WebHTMLFile)>10)):
trmSilentDelete(WebHTMLFile);
# Fix Host
trmReplaceAllfolder(".html",'localhost/','www.RonFoods.com');
#Minify Everything
def minitfile(filename,filetype):
fA = codecs.open(filename + filetype, 'r',"utf-8")
fB = codecs.open(filename + '_temp'+filetype, 'w',"utf-8")
# Minify Threw Html
htmlstr = fA.read();
htmlout=htmlmin.minify((htmlstr), True, False, False, True, False, True, False, (u'pre', u'textarea'),'pre')
fA.close();
fB.write(htmlout);
fB.close()
shutil.copy(filename + '_temp'+filetype, filename +filetype)
trmSilentDelete(filename + '_temp'+filetype)
print(filename +filetype+"Done");
def minitfolder(filetype,foldersub=""):
lsfiles = glob.glob(foldersub+"*" + filetype)
for replaceque in lsfiles:
minitfile(replaceque[:-len(filetype)], filetype);
def minitAllfolder(filetype,walk_dir='.',recursive=True):
allSubFolders=next(os.walk(walk_dir))[1]
print(allSubFolders);
for foldersub in allSubFolders:
print("Minify Currently in "+foldersub);
minitfolder(filetype, walk_dir+"/" +foldersub+"/");
if (recursive==True):
minitAllfolder(filetype, walk_dir+"/" +foldersub)
if walk_dir == '.':
minitfolder(filetype, )
minitAllfolder('.html');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.