Created
March 31, 2019 11:00
-
-
Save tshrinivasan/550e33803afe62679b256560d57d4d8c to your computer and use it in GitHub Desktop.
#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 file contains hidden or 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
#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