Skip to content

Added better file handling and fixed some spelling/grammatical errors #1519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import pickle


def Bdelete():
def bdelete():
# Opening a file & loading it
F = open("studrec.dat", "rb")
stud = pickle.load(F)
F.close()

print(stud)
with open("studrec.dat") as F:
stud = pickle.load(F)
print(stud)

# Deleting the Roll no. entered by user
rno = int(input("Enter the Roll no. to be deleted: "))
F = open("studrec.dat", "wb")
rec = []
for i in stud:
if i[0] == rno:
continue
rec.append(i)
pickle.dump(rec, F)
F.close()
with open("studrec.dat") as F:
rec = []
for i in stud:
if i[0] == rno:
continue
rec.append(i)
pickle.dump(rec, F)


Bdelete()
bdelete()
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import pickle


def binaryread():
B = open("studrec.dat", "rb")
stud = pickle.load(B)
print(stud)
def binary_read():
with open("studrec.dat") as b:
stud = pickle.load(b)
print(stud)

# prints the whole record in nested list format
print("contents of binary file")
# prints the whole record in nested list format
print("contents of binary file")

for ch in stud:
for ch in stud:

print(ch) # prints one of the chosen rec in list
print(ch) # prints one of the chosen rec in list

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

print(Rno, Rname, Rmark, end="\t")
print(rno, rname, rmark, end="\t")

B.close


binaryread()
binary_read()
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
def longlines():
F = open("story.txt", "r")
line = F.readlines()
with open('story.txt', encoding='utf-8') as F:
line = F.readlines()

for i in line:
if len(i) < 50:
print(i, end=" ")

F.close()
for i in line:
if len(i) < 50:
print(i, end=" ")


longlines()
8 changes: 4 additions & 4 deletions 1 File handle/File handle text/happy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ hello how are you
what is your name
do not worry everything is alright
everything will be alright
please dont loose hope
Wonders are in the way
please don't lose hope
Wonders are on the way
Take a walk in the park
In the end of the day you are more important than anything else.
At the end of the day you are more important than anything else.
Many moments of happiness are waiting
You are amazing!
Its true believe.
If you truly believe.
22 changes: 9 additions & 13 deletions 1 File handle/File handle text/input,output and error streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
sys.stdout.write("Enter the name of the file")
file = sys.stdin.readline()

F = open(file.strip(), "r")
with open(file.strip(), ) as F:

while True:
ch = F.readlines()
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
print(i, end="")
else:
sys.stderr.write("End of file reached")
break

while True:
ch = F.readlines()
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
print(i, end="")
else:
sys.stderr.write("End of file reached")
break
F.close()
4 changes: 2 additions & 2 deletions 1 File handle/File handle text/story.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
once upon a time there was a king.
he was powerful and happy.
he had a garden.
all the flowers in his garden was beautiful.
he lived happily forever.
all the flowers in his garden were beautiful.
he lived happily ever after.