ch3 Notes Word
ch3 Notes Word
Disk file
1. The generated output is stored in these files, if I am storing
something in my file it is called disk writing. When we retrieve
the data from the file we read it.
2. Data is stored in the form of bytes in the disk.
3. We can read data off disk files in the python program or write
back data from the python program to disk files.
Standard input device- mouse, keyboard
Standard output device- monitor
4. Data will be permanently stored in the disk files
5. .txt extension is used for text files
Types of files:
Binary files- Binary files are used to store binary data such as
images, video files, audio files, etc. A binary file contains arbitrary
binary data, usually numbers stored in the file which can be used for
numerical operations. In a binary file, there is no delimiter for a
line. Also, no character translation can be carried out in a binary
file. As a result, binary files are much easier and faster than text files
for carrying out reading and writing operations on the data. Binary
files require specific programs to access their content.
Ski code and Unicode characters
Unicode is the Information Technology standard that is used for
encoding, representation, and handling of texts in the writing
systems
Flush func()
Jab tak myfile.close() use nhi hoga tab tak text temporary buffer k
andr store hota h.
If we simultaneously want to write our text in the notepad file
without closing our function then we use flush ()
Binary Description Notes
Text File File
Mode Mode
‘a+’ ‘a+b’ or ‘ab+’ Write and read Same as above but previous
content will be retained and
both read and write.
Closing of a file:
1. fileobject.close()
2. a close() function breaks the link of file object and the file on
the disk
3. after closing a file(using close()), no tasks can be performed on
that file through the file-object
import pickle
#create a dictionary
phonebook = {
'Deepak':98107593,
'Saksham':92104584,
'John':87989898
}
import pickle
update() =explanation
rb+ stands for reading and writing in binary file
tell ()-
26th aug
[0:18 pm, 26/08/2021] Aman: import pickle
l=[]
def add():
while True:
f=open("Bus.dat","wb")
a=int(input("Enter Bus Number...?"))
b=input('Enter Starting Point')
c=input("Enter Destination")
dat=[a,b,c]
l.append(dat)
n=input("Do you want to enter more..?")
if n=="n":
break
f.close()
pickle.dump(l,f)
def search():
with open("Bus.dat","rb") as f:
s=pickle.load(f)
for i in s:
if i[2]=="cochin":
print(i)
add()
search()
[0:19 pm, 26/08/2021] Aman: import pickle
def add():
while True:
ls=[]
with open("product.dat" ,"wb+") as f:
a=int(input('Enter prodcut code...'))
b=input("Enter product price..")
l=[a,b]
ls.append(l)
print()
print()
n=input("Enter more?")
if n=="n":
pickle.dump(ls,f)
break
add()
pc=int(input("Enter product code to be searched.."))
def searchprod(pc):
with open("product.dat","rb") as f:
s=pickle.load(f)
for i in s:
if i[0]==pc:
print("Product Found!!")
print()
print()
print(i)
else:
print("No product found")
searchprod(pc)
[0:19 pm, 26/08/2021] Aman: import pickle
def add():
while True:
ls=[]
with open("route.dat" ,"wb+") as f:
a=int(input('Enter route number...'))
b=input("Enter route destination..")
c=input("Enter route name")
l=[a,b,c]
ls.append(l)
print()
print()
n=input("Enter more?")
if n=="n":
pickle.dump(ls,f)
break
add()
rc=int(input("Enter route number to be modified.."))
def routechange(rc):
with open("route.dat","wb+") as f:
s=pickle.load(f)
for i in s:
if i[0]==rc:
i[0]=j
print("Route Found!!")
print(i)
print(s[j])
routechange(rc)
[0:20 pm, 26/08/2021] Aman: import pickle
def add():
while True:
ls=[]
with open("class.dat" ,"ab+") as f:
a=int(input('Enter roll number...'))
b=input("Enter name..")
c=input("Enter percentage")
l=[a,b,c]
ls.append(l)
print()
print()
n=input("Enter more?")
if n=="n":
pickle.dump(ls,f)
break
add()
def remcount():
count=0
with open('class.dat',"rb") as f:
s=pickle.load(f)
print(s)
for i in s:
if int(i[2])<40:
count+=1
print(count,"students need remedials")
remcount()
1. Write a function in python to search and display details, whose destination is “Cochin” from
binary file “Bus.Dat”. Assuming the binary file is containing the following elements in the list:
2. 1. Bus Number
3. 2. Bus Starting Point
3. Bus Destina
Write a function addrec() in Python to add more new records at the bottom of a binary file
“STUDENT.dat”, assuming the binary file is containing the following structure :
[Roll Number, Student Name]
Write a function searchprod( pc) in python to display the record of a particular product from a
file product.dat whose code is passed as an argument. Structure of product contains the
following elements [product code , product price]
Write a function routechange(route number) which takes the Route number as parameter and
modify the route name(Accept it from the user) of passed route number in a binary file
“route.dat”. Which contains routeno destination route name as records
Amit is a monitor of class XII-A and he stored the record of all the students of his class in a
file named “class.dat”. Structure of record is [roll number, name, percentage]. His computer
teacher has assigned the following duty to Amit
Write a function remcount( ) to count the number of students who need remedial class
(student who scored less than 40 percent)
4. View assignment
5. Material