Skip to content

Instantly share code, notes, and snippets.

@PaulGwamanda
Created October 12, 2018 10:46
Show Gist options
  • Save PaulGwamanda/e05d849420f512c0c71aa31f02a2983f to your computer and use it in GitHub Desktop.
Save PaulGwamanda/e05d849420f512c0c71aa31f02a2983f to your computer and use it in GitHub Desktop.
Python program to find replace and clean files in directory
def findReplace(directory, find, replace, filePattern):
for path, dirs, files in os.walk(os.path.abspath(directory)):
for filename in fnmatch.filter(files, filePattern):
filepath = os.path.join(path, filename)
with open(filepath) as f:
s = f.read()
s = s.replace(find, replace)
with open(filepath, "w") as f:
f.write(s)
MYPATH = "folder/folder"
# Clean all files in input_path
findReplace(MYPATH, "{", " { ", "*.txt")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment