Created
October 12, 2018 10:46
-
-
Save PaulGwamanda/e05d849420f512c0c71aa31f02a2983f to your computer and use it in GitHub Desktop.
Python program to find replace and clean files in directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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