-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_handling_4.py
More file actions
55 lines (41 loc) · 1.58 KB
/
file_handling_4.py
File metadata and controls
55 lines (41 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
## updating in a binary file
#
# tell() and seek()
#
import pickle
from sys import flags
infile = open("trial.txt",'r')
print("initially the file_pointer is at = ",infile.tell())
print("reading 4 bytes : ",infile.read(4))
print("now the file pointer is art :",infile.tell())
## the tell function gives the current position of the file pointer
print("now printing results of the seek function")
infile.seek(10) ## default mode , puts the file pointer to the
# 10th byte from the beginning
infile.seek(5,1) ## mode 1 , puts the file pointer to the 5th byte(forward direction) from
# the current file pointer position
infile.seek(-10,2) ## mode 2 , puts the file pointer to the 10th byte backward from the
# end of file
#######################################################################
#### UPDATING THE RECORDS OF A FILE AT A PARTICULAR POSITION ###
#######################################################################
## read the file student_new.dat and give 10 marks bonus to students who have scored more that 550
##
upfile = open("student_new.dat","rb+")
stu = {} ## crating a empty file to store the records in the file
found = False
try:
while True:
rpos = upfile.tell()
stu = pickle.load(upfile)
if stu['Marks'] > 550:
stu['Marks'] += 10
upfile.seek(rpos)
pickle.dump(stu,upfile)
found = True
except EOFError:
if found == False:
print("no such records found")
else:
print("record(s) have been updated")
upfile.close()