Skip to content

Commit b79e440

Browse files
committed
Added File Handling - Lab
1 parent 3cd26bf commit b79e440

File tree

13 files changed

+96
-0
lines changed

13 files changed

+96
-0
lines changed

Advanced/5.File Handling/# TODO.txt

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
try:
2+
3+
file = open("text.txt", 'r')
4+
print("File found")
5+
6+
except FileNotFoundError:
7+
print("File not found")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This is some random line
2+
This is the second line
3+
And this is the third one
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
try:
2+
3+
file = open("numbers.txt")
4+
print(sum([int(num) for num in file]))
5+
6+
except ValueError:
7+
print("String cannot be summed")
8+
9+
except FileNotFoundError:
10+
print("File not found")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
1
2+
2
3+
3
4+
4
5+
5
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
with open("my_first_file.txt", "w") as file:
2+
file.write("I just created my first file!")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# This file does not exist, it is created by the code.
2+
3+
I just created my first file!
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import os
2+
3+
try:
4+
os.remove("my_first_file.txt")
5+
except FileNotFoundError:
6+
print("File already deleted!")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
I just created my first file!
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
words = list([word.lower().split() for word in open("words.txt")][0])
2+
3+
words_count = {}
4+
5+
file = open("text.txt", "w")
6+
7+
line = input()
8+
while line:
9+
file.write(f"{line.lower()}\n")
10+
line = input()
11+
12+
file.close()
13+
14+
text = list([word for word in open("text.txt", "r")])
15+
16+
for word in words:
17+
18+
alts = [f'{word}.', f'{word},', f'-{word}', f'-{word},', f'-{word}.']
19+
20+
for sentence in text:
21+
22+
if word in sentence.split() or alts[0] in sentence.split() or alts[1] in sentence.split() or alts[2] in \
23+
sentence.split() or alts[3] in sentence.split() or alts[4] in sentence.split():
24+
25+
if word not in words_count:
26+
27+
words_count[word] = sentence.split().count(word) + sentence.count(alts[0]) + \
28+
sentence.count(alts[1]) + sentence.count(alts[2]) + sentence.count(alts[3]) + \
29+
sentence.count(alts[4])
30+
continue
31+
words_count[word] += sentence.split().count(word) + sentence.count(alts[0]) + \
32+
sentence.count(alts[1]) + sentence.count(alts[4])
33+
34+
sorted_result = sorted(words_count.items(), key=lambda x: -x[1])
35+
36+
result = open("result.txt", "w")
37+
38+
for word, count in sorted_result:
39+
result.write(f"{word} - {count}\n")
40+
41+
result.close()
42+
43+
show_result = list([word for word in open("result.txt", "r")])
44+
45+
print(''.join(show_result))
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# This file does not exist, it is created by the program.
2+
3+
4+
is - 3
5+
quick - 2
6+
fault - 1
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This file does not exist, it is created by the program.
2+
3+
4+
5+
-i was quick to judge him, but it wasn't his fault.
6+
-is this some kind of joke?! is it?
7+
-quick, hide here�it is safer.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
quick is fault

0 commit comments

Comments
 (0)