Binary and CSV File Assigment
Binary and CSV File Assigment
10 Consider a binary file which stores Name of employee, where each name occupies 20 bytes (length of
each name) in file irrespective of actual characters. Now you have to write code to access the first name, 5th
name and last name.
f = open("Emp.txt","rb")
s= #code to get first record print(s.decode())
# code to position at 5th record s = f.read(size)
print(s.decode())
# code to position at last record s = f.read(20)
print(s.decode()) f.close()
11 Write a Python statement to reposition the read pointer to 20 bytes back from the current
position.
f = open("Emp.txt","rb") f.read(20)
f.read(20) f.read(20)
f. # reposition read pointer to previous record
f.close()
12 Write a function RECCOUNT() to read the content of binary file „NAMES.DAT‟ and
display number of records ( each name occupies 20 bytes in file ) in it.
For. e.g. if the content of file is: SACHIN
AMIT AMAN SUSHIL DEEPAK
HARI SHANKER
Function should display
Total Records are 6
13 Write a function SCOUNT() to read the content of binary file „NAMES.DAT‟ and display
number of records (each name occupies 20 bytes in file ) where name begins from „S‟ in it.
31 Write a python function CSVCOPY() to take sourcefile, targetfile as parameter and create
a targetfile and copy the contents of sourcefile to targetfile