KnowledgeBoat Logo

Computer Science

(i) How are text files different from binary files?

(ii) A Binary file, CINEMA.DAT has the following structure:

{MNO:[MNAME, MTYPE]}

Where

MNO – Movie Number
MNAME – Movie Name
MTYPE is Movie Type

Write a user defined function, findType(mtype), that accepts mtype as parameter and displays all the records from the binary file CINEMA.DAT, that have the value of Movie Type as mtype.

Python File Handling

5 Likes

Answer

(i)

Text filesBinary Files
Extension is .txtExtension is .dat
Data is stored in ASCII format that is human readable.Data is stored in binary form (0s and 1s), that is not human readable.

(ii)

def Searchtype(mtype):
    fobj = open("CINEMA.DAT", "rb")
    try:
        while True:
            data = pickle.load(fobj)
            if data[2] == mtype:
                print("Movie number:", data[0])
                print("Movie Name:", data[1])
                print("Movie Type:", data[2])

    except EOFError:
        fobj.close()

Answered By

2 Likes


Related Questions