Skip to content

Commit 3b15d25

Browse files
Merge pull request #1519 from akjroller/master
Added better file handling and fixed some spelling/grammatical errors
2 parents c43bce9 + 951948c commit 3b15d25

6 files changed

+45
-56
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
import pickle
22

33

4-
def Bdelete():
4+
def bdelete():
55
# Opening a file & loading it
6-
F = open("studrec.dat", "rb")
7-
stud = pickle.load(F)
8-
F.close()
9-
10-
print(stud)
6+
with open("studrec.dat") as F:
7+
stud = pickle.load(F)
8+
print(stud)
119

1210
# Deleting the Roll no. entered by user
1311
rno = int(input("Enter the Roll no. to be deleted: "))
14-
F = open("studrec.dat", "wb")
15-
rec = []
16-
for i in stud:
17-
if i[0] == rno:
18-
continue
19-
rec.append(i)
20-
pickle.dump(rec, F)
21-
F.close()
12+
with open("studrec.dat") as F:
13+
rec = []
14+
for i in stud:
15+
if i[0] == rno:
16+
continue
17+
rec.append(i)
18+
pickle.dump(rec, F)
2219

2320

24-
Bdelete()
21+
bdelete()
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
import pickle
22

33

4-
def binaryread():
5-
B = open("studrec.dat", "rb")
6-
stud = pickle.load(B)
7-
print(stud)
4+
def binary_read():
5+
with open("studrec.dat") as b:
6+
stud = pickle.load(b)
7+
print(stud)
88

9-
# prints the whole record in nested list format
10-
print("contents of binary file")
9+
# prints the whole record in nested list format
10+
print("contents of binary file")
1111

12-
for ch in stud:
12+
for ch in stud:
1313

14-
print(ch) # prints one of the chosen rec in list
14+
print(ch) # prints one of the chosen rec in list
1515

16-
Rno = ch[0]
17-
Rname = ch[1] # due to unpacking the val not printed in list format
18-
Rmark = ch[2]
16+
rno = ch[0]
17+
rname = ch[1] # due to unpacking the val not printed in list format
18+
rmark = ch[2]
1919

20-
print(Rno, Rname, Rmark, end="\t")
20+
print(rno, rname, rmark, end="\t")
2121

22-
B.close
2322

24-
25-
binaryread()
23+
binary_read()
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
def longlines():
2-
F = open("story.txt", "r")
3-
line = F.readlines()
2+
with open('story.txt', encoding='utf-8') as F:
3+
line = F.readlines()
44

5-
for i in line:
6-
if len(i) < 50:
7-
print(i, end=" ")
8-
9-
F.close()
5+
for i in line:
6+
if len(i) < 50:
7+
print(i, end=" ")
108

119

1210
longlines()

1 File handle/File handle text/happy.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ hello how are you
22
what is your name
33
do not worry everything is alright
44
everything will be alright
5-
please dont loose hope
6-
Wonders are in the way
5+
please don't lose hope
6+
Wonders are on the way
77
Take a walk in the park
8-
In the end of the day you are more important than anything else.
8+
At the end of the day you are more important than anything else.
99
Many moments of happiness are waiting
1010
You are amazing!
11-
Its true believe.
11+
If you truly believe.

1 File handle/File handle text/input,output and error streams.py

+9-13
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@
44
sys.stdout.write("Enter the name of the file")
55
file = sys.stdin.readline()
66

7-
F = open(file.strip(), "r")
7+
with open(file.strip(), ) as F:
8+
9+
while True:
10+
ch = F.readlines()
11+
for (i) in ch: # ch is the whole file,for i in ch gives lines, for j in i gives letters,for j in i.split gives words
12+
print(i, end="")
13+
else:
14+
sys.stderr.write("End of file reached")
15+
break
816

9-
while True:
10-
ch = F.readlines()
11-
for (
12-
i
13-
) in (
14-
ch()
15-
): # ch is the whole file,for i in ch gives lines, for j in i gives letters,for j in i.split gives words
16-
print(i, end="")
17-
else:
18-
sys.stderr.write("End of file reached")
19-
break
20-
F.close()
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
once upon a time there was a king.
22
he was powerful and happy.
33
he had a garden.
4-
all the flowers in his garden was beautiful.
5-
he lived happily forever.
4+
all the flowers in his garden were beautiful.
5+
he lived happily ever after.

0 commit comments

Comments
 (0)