Skip to content

Instantly share code, notes, and snippets.

@cristovaov
Created February 3, 2016 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cristovaov/403cc6ecefbb9518efd5 to your computer and use it in GitHub Desktop.
Save cristovaov/403cc6ecefbb9518efd5 to your computer and use it in GitHub Desktop.
Search and stripping in a file. My first Python script y'all! I needed this to strip a SQL string,
import os
import sys
def main():
print "\n"
fn = raw_input("Enter name of file: ")
print "Opening file: %s\n" % fn
if not os.path.exists(fn):
print("File does not exist!\n")
sys.exit
else:
str_search = raw_input("String to strip in file: ")
print "Searching for: %s\n" % str_search
with open(fn, "r") as infile, open("copy.txt", "w") as outfile:
data = infile.readlines()
for line in data:
rewrite = line.replace(str_search, "")
outfile.write(rewrite)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment