Skip to content

Commit 6dda33c

Browse files
committed
Fixed problem with file reading
1 parent 096eab2 commit 6dda33c

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

lab-12/occurance-remover.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# TODO: make sure that program does not overwrite file contents
2+
import os
3+
def occurance_remover(file_path, removal_string):
4+
target_file = open(file_path, "r+")
5+
occurances = target_file.read().count(removal_string)
6+
if removal_string in target_file.read():
7+
removal_string = removal_string.replace(removal_string, "")
8+
target_file.close()
9+
print(f"Removed {occurances} of {removal_string} from {os.path.basename(file_path)}")
10+
11+
12+
occurance_remover(r"C:\Users\s158658\src\python-programming\lab-12\test.txt", "foo")

0 commit comments

Comments
 (0)