-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_io.py
35 lines (28 loc) · 869 Bytes
/
file_io.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
# def copy(file_name, new_file_name):
# with open(file_name) as file:
# text = file.read()
#
# with open(new_file_name, "w") as new_file:
# new_file.write(text[::-1])
#
# copy("brett.txt", "reversed.txt")
#
# def statistics(file_name):
# with open(file_name) as file:
# lines = file.readlines()
#
# return {
# "lines": len(lines),
# "words": sum(len(line.split(" ")) for line in lines),
# "characters": sum(len(line) for line in lines)}
#
#
# print(statistics("brett.txt"))
def find_and_replace(file_name, find_word, replace_word):
with open(file_name, mode="w") as file:
lines = file.read()
new_lines = lines.replace(find_word, replace_word)
file.seek(0)
file.write(new_lines)
file.truncate()
print(find_and_replace("brett.txt", "Google", "Brett Inc"))