0% found this document useful (0 votes)
100 views4 pages

Data File Handling in Python W.S (C.S)

This document provides information about handling data files in Python. It discusses the differences between text and binary files, as well as various Python file handling methods such as open(), read(), write(), readline(), readlines(), seek() and close(). It also provides examples of opening and reading/writing to text and binary files, pickling/serializing Python objects to files, and a program to write and read records from a binary file.

Uploaded by

Shanawaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views4 pages

Data File Handling in Python W.S (C.S)

This document provides information about handling data files in Python. It discusses the differences between text and binary files, as well as various Python file handling methods such as open(), read(), write(), readline(), readlines(), seek() and close(). It also provides examples of opening and reading/writing to text and binary files, pickling/serializing Python objects to files, and a program to write and read records from a binary file.

Uploaded by

Shanawaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

2.

DATA FILE HANDLING IN PYTHON


ANSWER THE FOLLOWING QUESTIONS:
1. Differentiate between:
a) text file and binary file
Answer – Data files primarily come in two flavours: text files and binary files. Any text
editor can open a text file since it is made up of characters that are human readable. Binary
files, on the other hand, contain non-human readable characters and symbols and need special
software to retrieve its information.
b) readline() and readlines()
Answer – When called, the readline() function in Python returns a line from the file. The
readlines() method will return every line in a file as a list with each item representing a line in
the file.
c) write() and writelines()
Answer – A string is passed as an argument to the write() method, which writes it to a text
file. To write several strings to a file, use the writelines() method. To use the writelines()
method, we must supply an iterable object (such as a list, tuple, etc.) that contains strings.
2. Write the use and syntax for the following methods:
a) open()
Answer – open(file_name, mode)
b) read()
Answer – file_object.read()
c) seek()
Answer – dump(data_object, file_object)
3. Write the file mode that will be used for opening the following files. Also, write the
Python statements to open the following files:
a) a text file “example.txt” in both read and write mode
b) a binary file “bfile.dat” in write mode
c) a text file “try.txt” in append and read mode
d) a binary file “btry.dat” in read only mode.
Answer –
a) file = open( “example.txt”, “w+” )
b)file = open(“bfile.dat” , “wb” )
c) file = open ( “try.txt” , “a+” )
d) file = open( “btry.dat” , “rb” )
4. Why is it advised to close a file after we are done with the read and write operations?
What will happen if we do not close it? Will some error message be flashed?
Answer – As Python ensures that any unwritten or unsaved data is flushed off (written to the
file) before the file is closed, the main reason to close a file after read and write operations is
to prevent the application from slowing down. So, once we’re done with it, it’s always a good
idea to close the file.

5. What is the difference between the following set of statements (a) and (b):
a) P = open(“practice.txt”,”r”)
P.read(10)
b) with open(“practice.txt”, “r”) as P:
x = P.read()
Answer – File P will read 10 characters in part “a” but won’t print anything; in part “b,”
however, it will read every character in the practise file and save it in the variable “x.”
6. Write a command(s) to write the following lines to the text file named hello.txt.
Assume that the file is opened in append mode.
“ Welcome my class”
“It is a fun place”
“You will learn and play”
Answer –
f = open(“hello.txt”,”a”)
f.write(“Welcome my class”)
f.write(“It is a fun place”)
f.write(“You will learn and play”)
f.close()
7. Write a Python program to open the file hello.txt used in question no 6 in read mode
to display its contents. What will be the difference if the file was opened in write mode
instead of append mode?
Answer –
f = open(“hello.txt”,”r”)
x = f.read()
print(x)
8. Write a program to accept string/sentences from the user till the user enters “END”
to. Save the data in a text file and then display only those sentences which begin with an
uppercase alphabet.
Answer –
f = open(“example.txt”,”w”)
while True :
sen = input(“Enter something ( Enter END for quit ) :-“)
if sen == “END” :
break
else :
f.write(sen + “\n”)
f.close()
print()
print(“Lines started with Capital letters :-“)
f = open(“example.txt”,”r”)
print()
data = f.readlines()
for i in data :
if i[0].isupper() :
print(i)
f.close()
9. Define pickling in Python. Explain serialization and deserialization of Python object.
Answer – Pickling is a process also called serialization where an object is converted into stream of
bytes.
Serialization is the process of transforming data or an object in memory (RAM) to a stream of bytes
called byte streams. These byte streams in a binary file can then be stored in a disk or in a database or
sent through a network.
De-serialization or unpickling is the inverse of pickling process where a byte stream is converted back
to Python object.

10. Write a program to enter the following records in a binary file:


Item No integer
Item_Name string
Qty integer
Price float
Number of records to be entered should be accepted from the user. Read the file to
display the records in the following format:
Item No:
Item Name :
Quantity:
Price per item:
Amount: ( to be calculated as Price * Qty

Answer –
import pickle
file = open(“Pathwalla.dat”,”wb”)

while True :
dic = { }
Item_No = int(input(“Enter Item no.”))
Item_Name = input(“Enter Item name :-“)
Qty = int(input(“Enter Quantity :- “))
Price = float(input(“Enter price :-“))

dic[ “Item_No” ] = Item_No


dic[ “Item_Name” ] = Item_Name
dic[ “Qty” ] = Qty
dic[ “Price” ] = Price
pickle.dump( dic , file )
print()
ans = input(“Do you want to quit Enter [ Y/N ] :-“)
if ans == “y” or ans == “Y” :
break
print()

file.close()
print()
file = open(“Pathwalla.dat”,”rb”)
try :
while True :
dic = pickle.load(file)
print( “Item No. :”,”\t”, dic[ “Item_No” ] )
print( “Item Name :”,”\t”, dic[ “Item_Name” ] )
print( “Quantity :”,”\t”, dic[ “Qty” ] )
print( “Price of one :”,”\t”,dic[ “Price” ] )
print( “Amount :”, “\t” , dic[ “Qty” ] * dic[ “Price” ] )
print()
except :
file.close()

You might also like