2. x=open(“Smahi.txt”,
“w”)
A file memory x created in RAM, and get
connected to a file called “Smahi.txt” in
overwrite mode, which is available in
same folder, if the file not found, it will be
created automatically.
3. x=open(“Smahi.txt”,“w”)
x.write(“Debanshi ” )
A file memory x created in RAM, and get
connected to a file called “Smahi.txt”, in
overwrite mode, which is available in
same folder, if the file not found, it will be
created automatically. A string called
“Debanshi ” is written in file memory.
4. x=open(“Smahi.txt”, “w”)
x.write(“Debanshi ” )
x.close()
A file memory x created in RAM, and get
connected to a file called “Smahi.txt”,
which is available in same folder, if the file
not found, it will be created automatically.
A string called “Debanshi ” is written in
file memory. Now to transfer the contents
of the file memory to the file “Smahi.txt”
we have to disconnect the file memory x
with “Smahi.txt” using the close()
5. x=open(“Smahi.txt”, “w”)
y=“Debanshi ”
x.write(y)
x.close()
A string variable y is created and
initialized with “Debanshi ”, after
that the same is written in file
memory x from which the contents is
transferred to the file “Smahi.txt”
using the function close()
16. write() is used to
write string,
whereas writelines()
is used to write list
or string
17. x=open("Smahi.txt","w")
for y in range(4):
x.write(input("Enter name "))
x.close()
Program will accept 4
names and store in a
file called “Smahi.txt”
18. x=open("Smahi.txt","w")
for y in range(4):
x.write(input("Enter name
")+‘n’)
x.close()
Program will accept 4
names and store in a file
in separate lines, called
“Smahi.txt”
19. x=open("Smahi.txt","w")
for y in range(4):
x.write(input("Enter name
")+‘n’)
x.close()
Program will accept 4
names and store in a file
in separate lines, called
“Smahi.txt”
20. x=open("Smahi.txt","w")
z=[]
for y in range(4):
z.append(input("Enter name
")+'n')
x.writelines(z)
x.close()
Program will accept 4 names
and store in a file in separate
lines, called “Smahi.txt”
21. x=open("Smahi.txt","w")
z=[]
for y in range(4):
z[y].append(input("Enter name ")+'n')
x.writelines(z)
x.close()
Error:
Instead of z[y], we have to
write z
22. x=open("Smahi.txt","w")
z=[]
for y in range(4):
z.append(input("Enter name ")+'n')
x.write(z)
x.close()
Error:
write() is used to input string
not the list
23. w is used to create
the file in overwrite
mode, whereas a is
used to create the
file in append mode
24. x=open("Smahi.txt","a")
z=[]
for y in range(4):
z.append(input("Enter name
")+'n')
x.writelines(z)
x.close()
Initially if the file contains
Amit
Sumit
New names will be added after
Sumit
25. x=open("Smahi.txt","a")
z=[]
for y in range(4):
z.append(input("Enter name
")+'n')
x.writelines(z)
x.close()
Initially if the file contains
nothing, New names will be
added from beginning of the
file
58. In Binary files 1st the
data are serialized
and then stored.
59. Serialization is the
process in which the list,
tuples and the
dictionaries are
converted into byte
stream and then stored in
the file
60. Pickling converts an
object in byte stream in
such a way that it can be
reconstructed in original
form when un - pickled or
de - serialized
61. In order to work with
pickle module, we
must 1st import it
using syntax
import pickle
62. In pickle module there
are two functions
dump() and load(), 1st
one is used to store the
data in file and the 2nd
is used to access data
from the file
63. Storing data in binary file
import pickle
emp1={'name':"amit", "age":23}
emp2={'name':"sumit", "age":38}
emp3={'name':"ajay", "age":18}
em=open("emp.dat","wb")
pickle.dump(emp1,em)
pickle.dump(emp2,em)
pickle.dump(emp3,em)
print("Data dumped-written in file....
")
em.close()
64. Accessing data from binary file
import pickle
emp={}
em=open("emp.dat","rb")
try:
while True:
emp=pickle.load(em)
print(emp)
except EOFError:
em.close()
65. Accessing data from binary file
import pickle
emp={}
em=open("emp.dat","rb")
print("Name Age")
try:
while True:
emp=pickle.load(em)
print(emp['name'],'t',emp['age'])
except EOFError:
em.close()
66. Accessing selected data from binary file
import pickle
emp={ }
em=open("emp.dat","rb")
m=0
print("Name Age")
try:
while True:
emp=pickle.load(em)
if emp['age']>m:
m=emp['age']
n=emp['name']
except EOFError:
em.close()
print(n, m)
67.
68. Let the content of the file Smahi.txt is : -
Ram
Shyam
Ajay
Manoj
Rajeev
x=open("Smahi.txt")
y=x.tell()
print(y) Output
0
69. Let the content of the file Smahi.txt is : -
Ram
Shyam
Ajay
Manoj
Rajeev
x=open("Smahi.txt") Output
y=x.tell()
print(y) 0
z=x.read(2)
y=x.tell()
print(y) 2
72. Let the content of the file Smahi.txt is :
-
Ram
Shyam
Ajay
Manoj
Rajeev
x=open("Smahi.txt")
z=x.readline()
z=x.readline()
y=x.tell() OUTPUT
print(y) 12
73. Let the content of the file Smahi.txt is :
-
Ram
Shyam
Ajay
Manoj
Rajeev
x=open("Smahi.txt")
z=x.readline()
z=x.readline()
y=x.tell() OUTPUT
print(y) 12
75. Let the content of the file Smahi.txt is :
-
Ram
Shyam
Ajay
Manoj
Rajeev
x=open("Smahi.txt") OUTPUT
y=x.tell() 0
print(y) 2
x.seek(2)
y=x.tell()
print(y)
76. Let the content of the file Smahi.txt is :
-
Ram
Shyam
Ajay
x=open("Smahi.txt") Manoj
y=x.tell() Rajeev
print(y)
x.seek(12) OUTPUT
z=x.read(5) 0
print(z) Ajay
y=x.tell()
print(y) 18
77. Let the content of the Ram
file Smahi.txt is : - Shyam
Ajay
x=open("Smahi.txt") Manoj
y=x.tell() OUTPUT Rajeev
print(y) 0
x.seek(12)
z=x.read(6)
print(z) Ajay
y=x.tell()
print(y) 19
78. Let the content of the file Smahi.txt is :
-
Ram
Shyam
Ajay
x=open("Smahi.txt") Manoj
y=x.tell() Rajeev
print(y)
m=x.read(8) OUTPUT
print(m) ERROR
x.seek(-5) indexing is
y=x.tell() always
print(y) positive
81. Let the content of the file Smahi.txt is :
-
Ram
x=open("Smahi.txt", "rb") Shyam
x.seek(12) Ajay
print(x.tell()) Manoj
x.seek(-5,2) Rajeev
print(x.tell())
x.close()
Okay: - Cursor movement is allowed
from any where in “BINARY FILE”
82. Let the content of the file Smahi.txt is :
-
Ram
em=open("Smahi.txt","rb") Shyam
z=em.read() Ajay
print(em.tell()) Manoj
em.seek(0,2) Rajeev
print(em.tell())
em.close() OUTPUT
33
33
83. 0 moves from beginning
1 moves from current
position
2 moves from end
88. Let the content of the file Smahi.txt is : -
Ram
em=open(“Smahi.txt","rb+")Shyam
x=em.read() Ajay
print(em.tell()) Manoj
em.seek(100,0) Rajeev
print(em.tell()) OUTPUT
em.seek(0,0) 33
print(em.tell()) 100
y=em.read() 0
print(len(x)) 33
print(len(y)) 33
em.close() BUT……….
89. If we move the file
pointer beyond the
limit and tried to write
anything, REMEMBER
the same will be written
at that point and the
size of the file will
expand.
92. Storing data in binary file
import pickle
emp1={'name':"amit", "age":23}
emp2={'name':"sumit", "age":38}
emp3={'name':"ajay", "age":18}
em=open("emp.dat","wb")
pickle.dump(emp1,em)
pickle.dump(emp2,em)
pickle.dump(emp3,em)
print("Data dumped-written in file....
")
em.close()
93. Seek and Tell on Binary files
import pickle
emp={ }
em=open("emp.dat","rb")
print(em.tell())
try:
while True:
OUTPUT
emp=pickle.load(em) 0
print(em.tell()) 42
except EOFError: 85
em.close() 127
94. Seek and Tell on Binary files
import pickle
emp={}
bmp=[]
em=open("emp.dat",'rb')
try:
while True:
emp=pickle.load(em)
bmp.append(em.tell())
except EOFError:
em.close()
print(bmp) [42, 85,
127]
95. Seek and Tell on Binary files
import pickle z=0
emp={} em=open("emp.dat",'rb')
bmp=[] emp=pickle.load(em)
em=open("emp.dat",'rb') print(emp)
try: while z<len(bmp)-1:
while True:
em.seek(bmp[z])
emp=pickle.load(em)
emp=pickle.load(em)
bmp.append(em.tell()) print(emp)
except EOFError: z=z+1
em.close() em.close()
96. Modifying the contents of a binary file
import pickle
x=input("Please enter name to be modified ")
y=open("emp.dat","rb+")
a={ }
try:
while True:
z=y.tell()
a=pickle.load(y)
if a['name']==x:
print(a['name'],' ',a['age'])
a['age']+=5
y.seek(z)
pickle.dump(a,y)
except EOFError:
y.close()
98. Modifying the contents of a binary file (Page 2)
x=input("nnPlease enter name to be modified ")
y=open("emp.dat","rb+")
a={ }
k=0
try:
while True:
z=y.tell()
a=pickle.load(y)
if a['name']==x:
print("Name found ")
a['age']=int(input("Enter new age "))
y.seek(z)
pickle.dump(a,y)
k=1
except EOFError:
y.close()
if k==0:
print("Sorry ",x," not found ") continued ……… go to next
slide
99. Modifying the contents of a binary file (Page 3)
y=open("emp.dat","rb+")
a={}
try:
while True:
a=pickle.load(y)
print(a['name'],' ',a['age'])
except EOFError:
y.close()
101. Deleting records from a binary file (Slide
1)
import os
import pickle
y=open("emp.dat","rb+")
a={}
try:
while True:
a=pickle.load(y)
print(a['name'],' ',a['age'])
except EOFError:
y.close()
Continued………... Goto Next
Slide
102. Deleting records from a binary file (Slide 2)
x=input("nnEnter name to be deleted ")
y=open("emp.dat","rb+")
z=open("bmp.dat","wb+")
a={}
b=0
try:
while True:
a=pickle.load(y)
if a['name']!=x:
pickle.dump(a,z)
else:
b=1
except EOFError:
y.close()
z.close()
if b==1:
print("Data found, and deleted ")
else:
print("Deletion not possible ")
os.remove("emp.dat")
os.rename("bmp.dat","emp.dat") Continued………...
Goto Next Slide
103. Deleting records from a binary file (Slide
3)
y=open("emp.dat","rb+")
a={}
try:
while True:
a=pickle.load(y)
print(a['name'],' ',a['age'])
except EOFError:
y.close()
104. def enter():
import pickle
emp={}
em=open("stud.dat","wb")
x=1
for xx in range(25):
print("n")
emp['name']=input("Please enter name ")
emp['roll']=int(input("Please enter roll "))
emp['marks']=int(input("Please enter marks
"))
pickle.dump(emp,em)
print("Data dumped-written in file.... ")
em.close()
106. def modify():
import pickle
for xx in range(25):
print("n")
x=input("Please enter name to be modified ")
y=open("stud.dat","rb+")
a={}
try:
while True:
z=y.tell()
a=pickle.load(y)
if a['name']==x:
print("Data found ")
print(a['name'],' ',a['marks'], ' ',a['roll'])
print(".......and modified ")
a['marks']+=5
y.seek(z)
pickle.dump(a,y)
except EOFError:
y.close()
107. def adding():
import pickle
emp={}
em=open("stud.dat","ab")
x=1
for xx in range(25):
print("n")
emp['name']=input("Please enter name ")
emp['roll']=int(input("Please enter roll "))
emp['marks']=int(input("Please enter marks
"))
pickle.dump(emp,em)
print("Data dumped-written in file.... ")
em.close()