CH 3
CH 3
CH 3
A text file stores information in the form of a stream of ASCII or Unicode characters.
In text files, each line of text is terminated, (delimited) with a special character known
as EOL (End of Line) character. In Python, by default, this EOL character is the
newline character ('\n') or carriage-return, newline combination ('\r\n').
The text files can be of following types:
1. Regular Text files: These are the text files which store the text in the same
form as typed. Here the newline character ends a line and the text translations
take place. These files have a file extension as .txt.
2. Delimited Text files: In these text files, a specific character is stored to
separate the values, i.e., after each value, e.g., a tab or a comma after every
value.
a. When the comma is used to separate the values stored, these are called
CSV files (Comma Separated Values files). These files take the extension
as .csv.
Files – Binary Files
A binary file stores the information in the form of a stream of bytes. A binary file
contains information in the same format in which the information is held in memory,
i.e., the file content that is returned to you is raw (with no translation or no specific
encoding). As a result, binary files are faster and easier for a program to read and
write than are text files. As long as the file doesn't need to be read by people or need
to be ported to a different type of system, binary files are the best way to store
program information.
File Object: A file object is a reference to a file on disk. It opens the file and makes
it available for a number of different tasks.
Absolute Vs Relative path:
An absolute path is defined as specifying the location of a file or directory from the
root directory. In other words, we can say that an absolute path is a complete path
from start of actual file system from / directory.
Relative path is defined as the path related to the present working directly.
r: Opens a file for reading only. The file pointer is placed at the beginning
1
of the file. This is the default mode.
r+: Opens a file for both reading and writing. The file pointer placed at
2
the beginning of the file.
w: Opens a file for writing only. Overwrites the file if the file exists. If the
3
file does not exist, creates a new file for writing.
w+: Opens a file for both writing and reading. Overwrites the existing file
4 if the file exists. If the file does not exist, creates a new file for reading
and writing.
a: Opens a file for appending. The file pointer is at the end of the file if
5 the file exists. That is, the file is in the append mode. If the file does not
exist, it creates a new file for writing.
59
a+: Opens a file for both appending and reading. The file pointer is at the
6 end of the file if the file exists. The file opens in the append mode. If the
file does not exist, it creates a new file for reading and writing.
with statement: with statement will automatically close the file after the nested block
of code. It is guaranteed to close the file no matter how the nested block exits. Even
if an exception occurs with statement will handle it and close the file. It is used in the
following manner:
with open(<filename>,<filemode>) as <filehandle>:
<file manipulation statements>
Closing a text file: A close() function breaks the link of file-object and the file on
the disk. No tasks cab be performed after the file is closed. It is used in the following
way:
<file-handle>.close()
The tell() Function: The tell() function returns the current position of the file pointer
in a file. It is used as per the following syntax:
60
<file-object>.tell()
The seek() Function: the seek() function changes the position of the file pointer by
placing the file pointer at the specified position in the opened file. Its is used as per
the following syntax:
<file-object>.seek(offset[, mode])
offset: is a number specifying number of bytes.
mode: 0 – for beginning of the file – by default 0
1 – for current position of file pointer
2 – for end of the file
Q2. To read the next line of the file from the file object infi, we use
a. infi.read(all)
b. infi.read()
c. infi.readline()
d. infi.readlines()
Q3. Which function is used to ensure that the data in written in the file
immediately?
a. <filehandle>.write()
b. <filehandle>.writelines()
c. flush()
d. <filehandle>.close()
Q4. What is the datatype of the value returned by readlines() function?
a. Integer
b. String
c. List of strings
d. None of the above
Q5. What is the position of the cursor when the file is opened in append mode?
a. Start of file
b. End of file
c. At the 11th byte
d. Unknown
Q6. How to open a file such that close function is not needed to be called in order
to close the connection between file and python program?
a. Using open() method
b. Using read() method
c. Using with keyword
d. Using open with method
61
Q7. What is the full form of CSV?
a. Common segregated values
b. Comma separated values
c. Common separated values
d. None of the above
Q9. If the cursor in the file is at the end of the file, then what will the read()
function return?
a. None
b. False
c. Exception
d. Empty string
Q10. Which is the value of mode parameter to set the offset of the cursor from the
end of the file?
a. 0
b. 1
c. 2
d. None of the above
Q6. Given a file ‘data.txt’ write a function atoesidp() to display the file after
replacing ‘a’ with ‘e’.
Q7. What is the advantage of saving a file in text form and binary form?
Q10. If a file ‘f.txt’ contains data ‘Good’ and then what is the content of the file
f.txt then following program in run?
f = open(‘f.txt’, ‘w’)
f.write(‘Bye’)
Questions – 3 Marks – Long Answer
Q1. Differentiate between ‘r’ and ‘w’ file open modes and ‘w’ and ‘a’ file open mode.
Q2. Write a function stats() that accepts a filename and reports the files longest line.
Q3. A text file contains alphanumeric text (say an.txt). Write a program that reads
this text file and prints only the numbers or digits from the file.
Q4. Write a program to read a text file and display the count of lowercase and
uppercase letters in the file.
Q5. Write a method in python to read lines from a text file INDIA. TXT, to find and
display the Occurrence of the word "India".
Q6. Write a program that copies a text file "source.txt” onto "target.txt" barring the
starting with a "@" sign.
Q7. Take a sample text file and find the most commonly occurring word. Also, list
the frequencies of words in the text file.
Q8. Write a program to read a text file line by line and display each word separated
by a #.
Q9. Write a function remove_lowercase() that accepts two filenames, and copies all
the lines that do not start with a lowercase letter from the first file into the second.
Q10. Write a program to display all the records in a file along with line/record
number.
Questions – 5 Marks – Case Based Questions
Q1. Dhanush's teacher has given him the following text file (HigherEd. txt).
Q2. SHRUTHI has to complete her file-based assignment by tonight. She has been
given the following text file (Education.txt):
Binary File Modes: File mode governs the type of operations read/write/append
possible in the opened file. It refers to how the file will be used once its opened.
File Mode Description
rb Read Only: Opens existing file for read operation
wb Write Only: Opens file for write operation. If file does not exist, file is
created. If file exists, it overwrites data.
ab Append: Opens file in write mode. If file exist, data will be appended
at the end.
rb+ Read and Write: File should exist, Both read and write operations can
be performed.
wb+ Write and Read: File created if not exist, If file exist, file is truncated.
ab+ Write and Read: File created if does not exist, If file exist data is
truncated.
Pickle is a special python package that is used to generate data in binary format.
Pickle comes with few methods like dump( ) to write data in binary format.
Example:
import pickle
list =[ ] # empty list
while True:
roll = input("Enter student Roll No:")
sname = input("Enter student Name :")
student = {"roll":roll,"name":sname} # create a dictionary
list.append(student) # add the dictionary as an element
68
in the list
choice= input("Want to add more record(y/n) :")
if(choice=='n'):
break
file = open("student.dat","wb") # open file in binary and write
mode pickle.dump(list, file)
file.close( )
OUTPUT:
Enter student Roll No:1201
Enter student Name :Anil
Want to add more record(y/n) :y
Enter student Roll No:1202
Enter student Name :Sunil
Want to add more record(y/n) :n
To read the data from a binary file, we have to use load( ) function of pickle
module.
Example:
import pickle file = open("student.dat", "rb")
list = pickle.load(file) print(list)
file.close( )
OUTPUT:
[{'roll': '1201', 'name': 'Anil'}, {'roll': '1202', 'name': 'Sunil'}]
Output:
15
Output:
5
Pickle Module: Python Pickle is used to serialize and deserialize a python object
structure. Any object on python can be pickled so that it can be saved on disk.
Pickling: Pickling is the process whereby a Python object hierarchy is converted into
a byte stream.
Example:
import pickle
In this module, we shall discuss to functions of pickle module, which are:
i. dump( ) : To store/write the object data to the file.
ii. load( ) : To read the object data from a file and returns the object data.
Syntax:
Write the object to the file:
pickle.dump(List_name, file-object )
Read the object from a file:
pickle.load(file-object)
1 Mark Questions
1. The process of converting byte stream back to the original structure is known as
a. Picklingb. b. Unpickling c. Packing d. Zipping
5. Which of the following file mode opens a file for append or read a binary file and
moves the files pointer at the end of the file if the file already exist otherwise create
a new file?
a. a b. ab c. ab+ d. a+
71
6. Which of the following file mode opens a file for reading and writing both as well
as overwrite the existing file if the file exists otherwise creates a new file?
a. w b. wb+ c. wb d. rwb
7. Mr Sharma is working on a binary file and wants to write data from a list to a
binary file.
Consider list object as l1, binary file sharma_list.dat, and file object as f.
Which of the following can be the correct statement for him?
a. f = open(‘sum_list’,’wb’); pickle.dump(l1,f)
b. f = open(‘sum_list’,’rb’); l1=pickle.dump(f)
c. f = open(‘sum_list’,’wb’); pickle.load(l1,f)
d. f = open(‘sum_list’,’rb’); l1=pickle.load(f)
8. Every file has its own identity associated with it. Which is known as
a. icon b. extension c. format d. file type
10. Which of the following file types allows to store large data files in the computer
memory?
a. Binary Files b. Text Files c. CSV Files d. None of these
2 Mark Questions
1. Write a program in python to write and read structure, dictionary to the binary
file.
2. BINARY file is unreadable and open and close through a function only so what are
the advantages of using binary file
3. Write a statement to open a binary file name sample.dat in read mode and the file
sample.dat is placed in a folder ( name school) existing in c drive
4. When do you think text files should be preferred over binary files?
5. Consider a binary file employee.dat containing details such as
empno:ename:salary (seperator ':') write a python function to display details of
those employees who are earning between 20000 and 30000(both values
inclusive)
6. Differentiate between pickle.load() and pickle.dump() methods with suitable
example.
7. A binary file “Book.dat” has structure [BookNo, Book_Name, Author, Price].Write
a user defined function CreateFile() to input data for a record and add to Book.dat
8. A binary file “STUDENT.DAT” has structure (admission_number, Name,
Percentage). Write a function countrec() in Python that would read contents of the
file “STUDENT.DAT” and display the details of those students whose percentage is
above 75.
9. A binary file “Store.dat” has structure [ItemNo, Item_Name, Company, Price].
Write a function CountRec(Company) in Python which accepts the Company name
as parameter and count and return number of Items by the given Company are
stored in the binary file “Store.dat”.
72
10. A binary file “Store.dat” has structure [ItemNo, Item_Name, Company,
Price].
Write a function AddRecord(<List>) which accepts a List of the record [ItemNo,
Item_Name, Company, Price] and appends in the binary file “Store.Dat”
3 mark Questions
1. A binary file “Book.dat” has structure [BookNo, Book_Name, Author, Price].
i. Write a user defined function CreateFile() to input data for a record and add
to
“Book.dat” .
ii. Write a function CountRec(Author) in Python which accepts the Author name
as parameter and count and return number of books by the given Author are stored
in the binary file “Book.dat”
7. A binary file named “EMP.dat” has some records of the structure [EmpNo,
EName, Post, Salary], Write a user-defined function named NewEmp() to input the
details of a new employee from the user and store it in EMP.dat.
73
9. A binary file named “TEST.dat” has some records of the structure [TestId,
Subject, MaxMarks, ScoredMarks] Write a function in Python named
DisplayAvgMarks(Sub) that will accept a subject as an argument and read the
contents of TEST.dat.
10. Write a python program to search and display the record of the student from
a binary file “Student.dat” containing students records (Rollno, Name and Marks).
Roll number of the student to be searched will be entered by the user.
5 Mark Questions
2. Write a python program to create binary file dvd.dat and write 10 records in
it Dvd id,dvd name,qty,price Display those dvd details whose dvd price more than
25.
Working with CSV Files: CSV (Comma Separated Values)
A csv file is a type of plain text file that uses specific structuring to arrange tabular
data. csv is a common format for data interchange as it is compact, simple and
general.
Each line of the file is one line of the table.
csv files have .csv as file extension.
As you can see each row is a new line, and each column is separated with a comma.
This is an example of how a CSV file looks like.
To work with csv files, we have to import the csv module in our program.
OUTPUT:
['Roll No.', 'Name of student', 'stream', 'Marks']
['1', 'Anil', 'Arts', '426']
['2', 'Sujata', 'Science', '412']
CODE:
import csv
with open('C:\\data.csv', mode='a', newline='') as file:
writer = csv.writer(file, delimiter=',', quotechar='"' ) #write new record in file
writer.writerow(['3', 'Shivani', 'Commerce', '448'])
writer.writerow(['4', 'Devansh', 'Arts', '404'])
OUTPUT:
['Roll No.', 'Name of student', 'stream', 'Marks']
['1', 'Anil', 'Arts', '426']
['2', 'Sujata', 'Science', '412']
['3', 'Shivani', 'Commerce', '448']
['4', 'Devansh', 'Arts', '404']
When we shall open the file in notepad (Flat file) then the contents of the file will look
like this:
Roll No.,Name of student,stream,Marks
1,Anil,Arts,426
2,Sujata,Science,412
3,Shivani,Commerce,448
82
4,Devansh,Arts,404
CODE:
import csv
def readcsv():
with open('C:\\data.csv','rt')as f:
data = csv.reader(f) #reader function to generate a reader object
for row in data:
print(row)
def writecsv( ):
with open('C:\\data.csv', mode='a', newline='') as file:
writer = csv.writer(file, delimiter=',', quotechar='"') #write new record in file
writer.writerow(['4', 'Devansh', 'Arts', '404'])
OUTPUT:
Program-2:- CreateaCSVfilebyenteringuser-
idandpassword,readandsearchthepasswordforgivenuser-id.
CODE:
83
import csv
with open("user_info.csv", "w") as obj:
fileobj = csv.writer(obj)
fileobj.writerow(["User Id", "password"])
while(True):
user_id = input("enter id: ")
password = input("enter password: ")
record = [user_id, password]
fileobj.writerow(record)
x = input("press Y/y to continue and N/n to terminate the program\n")
if x in "Nn":
break
elif x in "Yy":
continue
with open("user_info.csv", "r") as obj2:
fileobj2 = csv.reader(obj2)
given = input("enter the user id to be searched\n")
for i in fileobj2:
next(fileobj2)
# print(i,given)
if i[0] == given:
print(i[1])
break
OUTPUT:
enter id: cbse
enter password: 123
press Y/y to continue and N/n to terminate the program
y
enter id: computer_science
enter password: python
press Y/y to continue and N/n to terminate the program
n
enter the user id to be searched
cbse
123
>>>
Sample Questions & Answers
84
1. What does the acronym CSV stand for in its full form?
a. Common Separated Value
b. Comma System Value
c. Comma Separated Value
d. Common System Vault
3. In regards to separated value files such as .csv and .tsv, what is the delimiter?
a. Any character such as the comma (,) or tab (\t) that is used to separate
the row data
b. Anywhere the comma (,) character is used in the file
c. Delimiters are not used in separated value files
d. Any character such as the comma (,) or tab (\t) that is used to separate
the column data.
4. In separated value files such as .csv and .tsv, what does the first row in the file
typically contain?
a. The source of the data
b. The author of the table data
c. Notes about the table data
d. The column names of the data
5. Assume you have a file object my_data which has properly opened a separated
value file that uses the tab character (\t) as the delimiter.
What is the proper way to open the file using the Python csv module and assign it
to the variable csv_reader?
Assume that csv has already been imported.
a. csv_reader = csv.tab_reader(my_data)
b. csv_reader = csv.reader(my_data)
c. csv_reader = csv.reader(my_data, tab_delimited=True)
d. csv_reader = csv.reader(my_data, delimiter='\t')
6. When iterating over an object returned from csv.reader(), what is returned with
each iteration?
For example, given the following code block that assumes csv_reader is an
object
returned from csv.reader(), what would be printed to the console with each
iteration?
85
for item in csv_reader:
print(item)
Neha is making software on “Items & their prices” in which various records are to be
stored/retrieved in STORE.CSV data file. It consists some records (Item & Price).
She has written the following code in python. As a programmer, you have to help her
to successfully execute the program.
def ShowRecord():
with open(“STORE.CSV”,”r”) as NI:
NewItem=csv._______ (NI) # Statement-5
for rec in NewItem:
print(rec[0], “#”, rec[1])
#main-code
AddItem(“Sugar”, 38.00)
AddItem(“Rice”, 48.50)
ShowRecord() # Statement-6
Q4. Which function to be used in Statement-5 to read the data from a csv
86
file.
A. read() B. readline() C. readlines() D. reader()