-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_replace_all.py
More file actions
25 lines (21 loc) · 977 Bytes
/
Copy pathsearch_replace_all.py
File metadata and controls
25 lines (21 loc) · 977 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import os
import fileinput
word = raw_input("enter word to search:")
replc = raw_input("enter word to replace:")
def get_filepaths(directory):
file_paths = [] # List which will store all of the full filepaths.
# Walk the tree.
for root, directories, files in os.walk(directory):
for filename in files:
# Join the two strings in order to form the full filepath.
filepath = os.path.join(root, filename)
file_paths.append(filepath) # Add it to the list.
return file_paths # Self-explanatory.
# Run the above function and store its results in a variable.
full_file_paths = get_filepaths("/home/praveen/NEW/root_dir")
for i in range (len(full_file_paths)):
tempFile = open(full_file_paths[i],'r+')
for line in fileinput.input( full_file_paths[i] ):
if word in line :
tempFile.write( line.replace( word, replc))
tempFile.close()