Computer Science
(i) Differentiate between r+ and w+ file modes in Python.
(ii) Consider a file, SPORT.DAT, containing records of the following structure: [SportName, TeamName, No_Players]
Write a function, copyData(), that reads contents from the file SPORT.DAT and copies the records with Sport name as “Basket Ball” to the file named BASKET.DAT. The function should return the total number of records copied to the file BASKET.DAT.
Python File Handling
5 Likes
Answer
(i)
r+ mode | w+ mode |
---|---|
Primary function is reading. | Primary function is writing. |
If the file does not exist, it results in an error. | If the file does not exist, it creates a new file. |
(ii)
def copyData():
fobj = open("SPORT.DATA", "rb")
fobj1 = open("BASKET.DAT", "wb")
cnt = 0
try:
while True:
data = pickle.load(fobj)
print(data)
if data[0] == "Basket Ball":
pickle.dump(data, fobj1)
cnt +=1
except:
fobj.close()
fobj1.close()
return cnt
Answered By
4 Likes
Related Questions
Vedansh is a Python programmer working in a school. For the Annual Sports Event, he has created a csv file named Result.csv, to store the results of students in different sports events. The structure of Result.csv is :
[St_Id, St_Name, Game_Name, Result]
Where
St_Id is Student ID (integer)
ST_name is Student Name (string)
Game_Name is name of game in which student is participating(string)
Result is result of the game whose value can be either 'Won', 'Lost' or 'Tie'.For efficiently maintaining data of the event, Vedansh wants to write the following user defined functions:
Accept() – to accept a record from the user and add it to the file Result.csv. The column headings should also be added on top of the csv file.
wonCount() – to count the number of students who have won any event.
As a Python expert, help him complete the task.
Meticulous EduServe is an educational organization. It is planning to setup its India campus at Chennai with its head office at Delhi. The Chennai campus has 4 main buildings – ADMIN, ENGINEERING, BUSINESS and MEDIA.
Block to Block distances (in Mtrs.)
From To Distance ADMIN ENGINEERING 55 m ADMIN BUSINESS 90 m ADMIN MEDIA 50 m ENGINEERING BUSINESS 55 m ENGINEERING MEDIA 50 m BUSINESS MEDIA 45 m DELHI HEAD OFFICE CHENNAI CAMPUS 2175 km Number of computers in each of the blocks/Center is as follows:
block/center Number of computers ADMIN 110 ENGINEERING 75 BUSINESS 40 MEDIA 12 DELHI HEAD 20 (a) Suggest and draw the cable layout to efficiently connect various blocks of buildings within the CHENNAI campus for connecting the digital devices.
(b) Which network device will be used to connect computers in each block to form a local area network?
(c) Which block, in Chennai Campus should be made the server? Justify your answer.
(d) Which fast and very effective wireless transmission medium should preferably be used to connect the head office at DELHI with the campus in CHENNAI?
(e) Is there a requirement of a repeater in the given cable layout? Why/ Why not?
(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 TypeWrite 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.
(i) Define the term Domain with respect to RDBMS. Give one example to support your answer.
(ii) Kabir wants to write a program in Python to insert the following record in the table named Student in MYSQL database, SCHOOL:
- rno(Roll number )- integer
- name(Name) - string
- DOB (Date of birth) – Date
- Fee – float
Note the following to establish connectivity between Python and MySQL:
- Username - root
- Password - tiger
- Host - localhost
The values of fields rno, name, DOB and fee has to be accepted from the user. Help Kabir to write the program in Python.