Skip to content

Instantly share code, notes, and snippets.

@telmotrooper
Created April 30, 2017 22:57
Show Gist options
  • Save telmotrooper/6fd5243c662af2994cc0244536f10440 to your computer and use it in GitHub Desktop.
Save telmotrooper/6fd5243c662af2994cc0244536f10440 to your computer and use it in GitHub Desktop.
Simple script to help appending the "www." prefix to entries in your hosts file
from shutil import copyfile
copyfile("in.txt", "out.txt")
inputFile = open("in.txt", "r")
outputFile = open("out.txt", "a")
outputFile.write("\n\n")
for line in inputFile.readlines():
if line.startswith("0.0.0.0"):
# makes a clean line removing the "0.0.0.0", spaces and tabs
cleanLine = line.lstrip("0. ")
# finds the index where the website address starts
index = len(line) - len(cleanLine)
newLine = ""
if (cleanLine[0:4]) != "www.":
# makes a list of characters out of the line
# so we can add the "www". to the address
charList = list(line)
charList[index] = "www." + charList[index]
newLine = "".join(charList)
outputFile.write(newLine)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment