Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#This program helps to remove the given words in a file to all the files inside a directory, recursively. # Got the sed idea from http://www.linuxask.com/questions/replace-multiple-strings-using-sed
#This program helps to remove the given words in a file to all the files inside a directory, recursively.
# Got the sed idea from http://www.linuxask.com/questions/replace-multiple-strings-using-sed
import sys
import glob
import os
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("input_directory", help="The name of the input directory that contains files")
parser.add_argument("replace_words_file", help="The File path that contains the list of words to be removed")
args = parser.parse_args()
input_directory = args.input_directory
input_file = args.replace_words_file
all_files = []
for root,d_names,f_names in os.walk(input_directory):
for f in f_names:
all_files.append(os.path.join(root, f))
#print("all_files = %s" %all_files)
replace_file = open(sys.argv[2]).readlines()
sed_file = open("/tmp/sed.file","w")
for line in replace_file:
sed_file.write("s/" + line.strip() + "/ /g\n")
sed_file.close()
for file in all_files:
replace_command = 'sed -f /tmp/sed.file -i "' + file + '"'
print("Replacing for the file " + file)
os.system(replace_command)
print("Done")
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.