Skip to content

Commit d244266

Browse files
Merge pull request #1511 from Harivansh-coder/master
improved the way of file handling
2 parents 09b3bfe + 5d33a50 commit d244266

File tree

4 files changed

+66
-65
lines changed

4 files changed

+66
-65
lines changed

1 File handle/File handle text/question 2.py

+16-11
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@
33
using read function
44
and display those words, which are less than 4 characters. """
55

6-
F = open("story.txt", "r")
7-
value = F.read()
8-
lines = value.split()
9-
count = 0
6+
def display_words():
7+
with open("story.txt") as F:
8+
lines = F.read()
9+
words = lines.split()
10+
count = 0
11+
for word in words:
12+
if (len(word) < 4):
13+
print(word)
14+
count += 1
15+
return count
16+
17+
if __name__ == "__main__":
18+
print(display_words())
19+
20+
21+
1022

11-
for i in lines:
12-
if len(i) < 4:
13-
print(i)
14-
count += 1
15-
else:
16-
pass
1723

18-
print("The total number of words with length less than 4 are", count)

1 File handle/File handle text/question 5.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33

44

55
def lowercase():
6-
F = open("happy.txt", "r")
7-
count = 0
8-
count_ = 0
9-
value = F.read()
10-
for i in value:
11-
if i.islower():
12-
count += 1
13-
elif i.isupper():
14-
count_ += 1
15-
print("The total number of lower case letters are", count)
16-
print("The total number of upper case letters are", count_)
17-
print("The total number of letters are", count + count_)
6+
with open("happy.txt") as F:
7+
count_lower = 0
8+
count_upper = 0
9+
value = F.read()
10+
for i in value:
11+
if i.islower():
12+
count_lower += 1
13+
elif i.isupper():
14+
count_upper += 1
15+
print("The total number of lower case letters are", count_lower)
16+
print("The total number of upper case letters are", count_upper)
17+
print("The total number of letters are", count_lower + count_upper)
1818

19-
20-
lowercase()
19+
if __name__ == "__main__":
20+
lowercase()

1 File handle/File handle text/question 6.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33

44

55
def lowercase():
6-
F = open("happy.txt", "r")
7-
count = 0
8-
count_ = 0
9-
value = F.read()
10-
for i in value:
11-
if i.islower():
12-
count += 1
13-
elif i.isupper():
14-
count_ += 1
15-
print("The total number of lower case letters are", count)
16-
print("The total number of upper case letters are", count_)
17-
print("The total number of letters are", count + count_)
6+
with open("happy.txt") as F:
7+
count_lower = 0
8+
count_upper = 0
9+
value = F.read()
10+
for i in value:
11+
if i.islower():
12+
count_lower += 1
13+
elif i.isupper():
14+
count_upper += 1
15+
print("The total number of lower case letters are", count_lower)
16+
print("The total number of upper case letters are", count_upper)
17+
print("The total number of letters are", count_lower + count_upper)
1818

19-
20-
lowercase()
19+
if __name__ == "__main__":
20+
lowercase()

1 File handle/File handle text/question3.py

+22-26
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,29 @@
44

55
# step1:
66
def write_to_file():
7-
F = open("happy.txt", "a")
8-
while True:
9-
text = input("enter any text")
10-
11-
F.write(
12-
text + "\n"
13-
) # write function takes exactly 1 arguement so concatenation
14-
choice = input("do you want to enter more, y/n")
15-
if choice == "n":
16-
break
17-
F.close()
18-
19-
7+
with open("happy.txt", "a") as F:
8+
while True:
9+
text = input("enter any text")
10+
F.write(
11+
text + "\n"
12+
) # write function takes exactly 1 arguement so concatenation
13+
choice = input("do you want to enter more, y/n")
14+
if choice == "n":
15+
break
16+
2017
# write_to_file()
2118

2219
# step2:
2320
def check_first_letter():
24-
F = open("happy.txt", "r")
25-
value = F.read()
26-
count = 0
27-
line = value.split()
28-
for i in line:
29-
if i[0] in ["m", "M", "i", "I"]:
30-
count += 1
31-
print(i)
32-
33-
print("The total number of sentences starting with I or M are", count)
34-
35-
36-
check_first_letter()
21+
with open("happy.txt") as F:
22+
value = F.read()
23+
count = 0
24+
line = value.split()
25+
for i in line:
26+
if i[0] in ["m", "M", "i", "I"]:
27+
count += 1
28+
print(i)
29+
print("The total number of sentences starting with I or M are", count)
30+
31+
if __name__ == "__main__":
32+
check_first_letter()

0 commit comments

Comments
 (0)