-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword_swapper.py
45 lines (34 loc) · 1.22 KB
/
word_swapper.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Importing re module
import re
import os
import glob
# Creating a function to
# replace the text
def replacetext(search_text,replace_text):
# Opening the file in read and write mode
for filename in os.listdir("C:/Users/admin/Desktop/"):
with open(os.path.join("C:/Users/admin/Desktop/", filename), 'r+', encoding='utf-8') as f:
# Reading the file data and store
# it in a file variable
file = f.read()
# Replacing the pattern with the string
# in the file data
file = re.sub(search_text, replace_text, file)
# Setting the position to the top
# of the page to insert data
f.seek(0)
# Writing replaced data in the file
f.write(file)
# Truncating the file size
f.truncate()
# Return "Text replaced" string
return "Text replaced"
# Creating a variable and storing
# the text that we want to search
search_text = "Kingsman"
#Creating a variable and storing
# the text that we want to update
replace_text = "NewWord"
# Calling the replacetext function
# and printing the returned statement
print(replacetext(search_text,replace_text))