0% found this document useful (0 votes)
25 views

File Handling Notes

Ip project for computer and other subjects
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

File Handling Notes

Ip project for computer and other subjects
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

UNIT-I

FILE HANDLING IN PYTHON

ABOUT FILE:
A file is a sequence of bytes contain data and store it on some storage device. File handling is an
important part of any computer application.

NEED FOR FILE HANDLING

➔ To store data for future reference

➔ Sometime the output generated by a program is large thus file help to store that huge data.

TYPES OF FILES

Text Files:

A file whose contents can be viewed using a text editor is called a text file. A text file is simply a
sequence of ASCII or Unicode characters. In text file, translation will take place after the EOL or delimiter
is encountered. Thus, it is slower than binary file.

Binary Files:

A binary file stores the data in the same way as stored in the memory. In Binary file there is no
delimiter for a line. The file contents returned by a binary file is raw i.e. with no translation, thus Binary
files are faster than text files.

CSV Files:

CSV stands for Comma Separated Values. CSV is just like a text file, in a human readable format
which is extensively used to store tabular data, in a spreadsheet or database. The separator character of
CSV files is called a delimiter. Default delimiter is comma (,). Other delimiters are tab (\t), colon (:), pipe (|),
semicolon (;) characters.

TEXT FILE Vs. BINARY FILES

Text Files Binary Files


Stores data in ASCII or Unicode format so Contains raw data so that not human
that text files are human readable. readable.
Each line is delimited by EOL or end of Line No delimiter for a line
(like \n).
Slower than Binary File Faster than text files

In Python, any type of File Handling consists the following three steps:

1. Opening of a file with specific mode. Using open () method / with statement of python.

2. Process file i.e perform read or write operation.

3. Close the file, if file has opened using open () method

STEPS TO PROCESS A FILE

1. Open the file by specifying file-path and file-mode(reading/writing)


i. Reading purpose: If the data is to be brought in from a file to memory.
ii. Writing purpose: If the data is to be sent from memory to file.

2. Assign its reference to a file object or file-handle.


3. Process the file as per requirement of program i.e perform the desired operation.
4. Close the file.

Opening a Text File

The first step of file handling is to open the file by specifying file-path and file-mode. If no file-mode is
specified by default file will be open in reading mode.

Python has two ways to open a file: -


1) Using built in function open()
2) Using Python ‘with’ statement

1) Using built-in function open() : -

Syntax: <file_object_name> = open(<file_path>,<mode>)

Example: f = open(“demo.txt”,”r”)
2) Using Python ‘with’ statement: -

Syntax: with open(<file_path>, <mode>) as <file_object>:

Example: with open(“demo.txt”) as f:

About File Object:


o It serves as a link to file residing in your computer.
o It is a reference to the file on the disk
o It helps to perform operations on the file.

Absolute Path:
• Absolute file paths are notated by a leading forward slash or drive label.
Example C:\\KVSROMUMBAI\\demo.txt
• An absolute path, which always begins with the root folder.

Relative Path:
A relative path, is relative to the program’s current working directory.

Example ‘.\test.txt’ or ‘test.txt’

File Modes (File Access Modes):

It indicates that for what purpose the file is being opened.

Text Binary File Description Purpose


File Mode
Mode
Opens a file for reading, Shows error if the file
“r” “rb” Read does not exist. It is the default file-mode.
Opens a file for writing, creates the file if it
“w” “wb” Write does not exist.
If the file exists, the previous contents will be
lost.
Opens a file for appending, Old data will retain
“a” “ab” Append if file already exist.
Creates the file if it does not exist.
File must exist otherwise error is raised.
“r+” “rb+” or Read and Both reading and writing operations can take
“r+b” Write place.
File is created if it does not exist.
“w+” “wb+” or Write and If the file exists previous data is truncated
“w+b” Read (removed).
Both reading and writing operations can take
place.
File is created if it does not exist.
“a+” “ab+” or Append and If the file exists previous data is retained
“a+b” Read and the new data will be added at the end of the
file.
Both reading and writing operations can take
place.

CLOSING A FILE: -
close() method is use to close the opened file. In python files are automatically close at the end of the
program but it is advisable to close a file after the end of the program.

S yntax: <fileObject>.close()

Example : f.close()

Moreover, if file has opened using ‘with’ statement of python then it will automatically close after
the nested block of code.

READING from a Text File: -


A Program can reads a text file from disk. The followings methods are used to read data from the
text file:
1. read()
2. readline()
3. readlines()

1. read() - The read() method returns the whole text in the form of string. You can also specify how
many characters you want to return by passing the size as argument.

Syntax: <file_object>.read([n]) where n is the No. of bytes that we want to read from the file.

a) <file_object>.read() – It will read the whole file.

b) <file_object>.read(n) – It will read exactly n bytes from the beginning of the file.

Example:
f = open("demo.txt", "r")

print(f.read(15)) # Returns the first 15 characters of the file"demo.txt".

2. readline() - readline() method will return a line, if n is specified reads n bytes.

Syntax: <file_object>.readline()

Example
f = open("demo.txt", "r")

print(f.readline()) # It will read the first line

print(f.readline()) # It will read the next line and so on

3. readlines() – This method will return a list of strings, each separated by \n. readlines() can be used to read
the entire content of the file.

Syntax: <file_object>.readlines()

It returns a list, which can then be used for manipulation.

Example: f = open("demo.txt", "r")


print(f.readlines())

Some Examples of Text File Handling: -

# Program to print the first and last line of the text file abcd.txt
myf=open("abcd.txt","r")
lst=myf.readlines()
print(lst[0]) # Used to print the first line
print(lst[-1]) # Used to print the last line
myf.close()

# Program to count No. of lines in a text file python.txt


myf=open("python.txt","r")
lst=myf.readlines()
print(len(lst))
myf.close()

# Program to count a specific word "python" in a text file text_demo.txt


myf=open("text_demo.txt","r")
st=myf.read()
c=0
for i in st.split():
if (i=="python" or i=="PYTHON" or i=="Python"):
c=c+1
print("The word python occurs ", c , “ times in the file.”)
myf.close()

# Program to print first n lines from the text file para.txt


myf=open("para.txt","r")
lst=myf.readlines()
n=int(input("Enter how many lines you wish to display"))
for i in range(0,n):
print(lst[i])
myf.close()

# Program to print n lines at the end from a text file abcd.txt

myf=open("abcd.txt","r")
lst=myf.readlines()
n =int(input('Enter how many lines you wish to print from last'))
for i in range(-1,-n-1,-1):
print(lst[i])
myf.close()

# Program to count total number of digits in a file myfile.txt

myf=open("myfile.txt","r")
x=myf.read()
c=0
for i in x:
if(i.isdigit()):
c=c+1
print('Total digits=',c)
myf.close()

Difference between read(), readline() and readlines():

The read() method reads data from a text file, and store its contents in a string type variable. It
can either read the whole content from the file if any parameter is not passed inside read method,
otherwise if n is passed inside read() the method will read n number of characters.
The readline() function reads data line by line from the text file. At a time it will return one line
of data in the form of string.
The readlines() function reads all lines and return them in a list.

Assume the content of text file, 'portfolio.txt' is:


Samrat Roy
12-11-1999
AB+
NAGPUR

Now, carefully watch the given code and then identify the data type of record1 and record2?

file = open("portfolio.txt")
record1 = file.readlines()
file.close()

Ans: - Data type of record1 will be list.

file = open("portfolio.txt")
record2 = file.read()
file.close()

Ans: - Data type of record2 will be string.

# Suppose the content of the file 'quotes.txt' is: -

All of us do not have equal talent.


But, all of us have an equal opportunity to develop our talents.
I am not handsome but I can give my hand to someone who needs help.
Because Beauty is required in the heart, not in face.

Now, tell what will be the output of the following code(s)?

FILE = open("quotes.txt")
LC = 0
DATA = FILE.readlines()
for L in DATA:
if L[0] == 'B':
LC += 1
print("No. of lines starts with B =",LC)
FILE.close()

Output:

No. of lines starts with B = 2

And, what will be the output of the following code: -

FILE = open("quotes.txt")
AC = 0
DATA = FILE.read()
for L in DATA:
if L=='B':
AC+=1
print("Total No. of alphabet B in the file =",AC)
FILE.close()

Output:

Total No. of alphabet B in the file = 3


WRITING into a text file: -

A Program can also write strings into a text file. Followings are the methods to write a data to the
file:

write ()
writelines()

Note: - To write into a text file, we must add parameter as ‘w’ or ‘a’ in the open() function which specifies the

file access mode :

"a" – for append the data at the end of the file

"w" – for overwrite any existing content and then write the new data

Difference between ‘a’ and ‘w’ modes i.e. append and write modes: -

If you open a file in “w” means write mode, Python overwrites an existing file or creates a new
file. For an existing file, the earlier data gets removed.

If you want to write into a file with retaining the previous data, the file must be opened in “a”
means append mode.

1) write() – This method takes a string ( as parameter ) and writes it in the file. For storing data with end of line
character, we will have to add \n character to the end of the string.

Example:

# Open the file "demo.txt" and add content in it

f = open("demo.txt", "w")

f.write("Hello students\nUse this study material\nYou will get good marks!")

f.close()

# open the file and read the content of it


f = open("demo.txt", "r")

print(f.read())

# Open the previous file "demo.txt" and add more content to the file:

f = open("demo.txt", "a")

f.write("\nAll the Best.")

f.close()

# Now again open the file in read mode and see the result …. (Check by yourself)

2) writelines() - Drawback of write() function is it will write a string in a text file at a time, and it can't be
used for writing a list, tuple etc. into a file. writelines() method will help to write a sequence of strings to the
file. The sequence can be any object producing strings, typically a list of strings. So, whenever we have to
write a sequence of string / data type, we will use writelines(), instead of write().

# Program showing use of writelines() method

f = open("demo.txt", "w")
f.writelines(["Python is just amazing!", "\nWe love python"]) # here we used list data
f.close()

#open and read the file after the appending

f = open("demo.txt", "r")
print(f.read())
flush() function: - The flush() function forces the writing of data on disc which was still pending in the
output buffer.

Syntax : <file_object>.flush()

# Program to demonstrate the use of flush() function

f = open("demo_flush.txt","w+")
f.write("India is my country.\n")

f.flush()

# After some statements x = "Jai Hind"

x=”Jai Hind”
f.write(x)

f.seek(0) # Here seek() will move the file pointer(handle) at the beginning of the file.
print(f.read())
f.close()

seek() function: – The seek() function changes the position of the file-pointer by placing the file-pointer at
the specified position in the open file.

Syntax: <file_object>.seek(offset, mode)

Offset will be 0 or 1 or 2

0 for beginning of the file (to move file pointer w.r.t. beginning of file) it is default mode.
1 for current position of file pointer (to move file pointer w.r.t current position of it.)
2 for end of file (to move file-pointer w.e.t. end of file)

Example:
f=open("demo_seek.txt","r")
print(f.read()) # after reading all the bytes file pointer reaches to the end of the file.

f.seek(6) # Now the file pointer will move to the 6th Byte from beginning.
print(f.read()) # It will print the remaining number of bytes after 6th Byte.

tell() function: – This function returns the current position of file pointer in the file.

Syntax: <file_object>.tell()

Example:
f=open("d:\\pooja\\demo_seek.txt","r")
print(f.read(5)) # This will read 5 bytes from the beginning
print(f.tell()) # This will show 5 as file pointer is at 5th character from beginning

BINARY FILES:

A Binary file stores the information in the form of a stream of bytes. A binary file stores the data
in the same way as stored in the memory. In Binary file there is no delimiter for a line. The file contents
returned by a binary file is raw i.e. with no translation, thus Binary files are faster than text files.

Python objects (list, dictionary etc) have a specific structure which must be maintained
while storing or accessing them. Python provides a special module called pickle module for this.

PICKLING refers to the process of converting the structure(list/dictionary) to a byte of stream before
writing it to a file. The process to converts any kind of python objects (list, dict etc.) into byte streams (0s
and 1s).

UNPICKLING is used to convert the byte stream back to the original structure while reading the
contents of the file.

pickle Module: -
Before reading or writing to a file, we have to import the pickle module.

import pickle

pickle module has two main methods: dump() and load()

pickle.dump() – This method is used to write the object in the file which is opened in ‘wb’ or ‘ab’
i.e. write binary or append binary access mode respectively.

Syntax : pickle.dump(<structure>,<FileObject>)

Here, Structure can be list or dictionary.


FileObject is the file handle of file in which we have to write.

# Simple program to write a list data into a binary file

import pickle
fo = open("binary_file1.dat","wb")
Laptop = ["Dell","HP","ACER"]
pickle.dump(Laptop,fo)
fo.close()

pickle.load() – This method is used to read data from a file and return back into the structure (list/dictionary).

Syntax : <structure> = pickle.load(<FileObject>)

Structure can be any sequence in Python such as list, dictionary etc. FileObject is the file handle of file in
which we have to write.

# Program to read data from a binary file

fbin = open("binary_file1.dat","rb")
x=pickle.load(fbin)
print(x)
fbin.close()
# Simple program to write a dictionary data into a binary file

import pickle
f=open("my_bin1.bin","wb")
D1={3:'Maruti',2:'Honda',4:'Hundai',1:'BMW'}
pickle.dump(D1,f)
f.close()

f1=open("my_bin1.bin","rb")
D2=pickle.load(f1)
print(D2)
f.close()

# Write a User defined function bdict() to store customer data into a binary file customer.dat using a
dictionary and print them on screen after reading them. The customer data contains
customer_ID(c1,c2,c3) as key, and name, city as values.

import pickle
def bdict():
f = open("customer.dat","wb")
d = {'C1':['Siman Raheja','Haryana'],
'C2':['Praharsh Kumar','Pune'],
'C3':['Vinita Minj','Indore']}
pickle.dump(d,f)
f.close()

f = open("customer.dat","rb")
d = pickle.load(f)
print(d)
f.close()

# Sample program to insert any number of records (as per user’s choice) of employee (employee number,
name, salary and allowance) and then display all the records.

import pickle
bfile=open("empfile.dat","ab")
recno=1
while True:
print("RECORD No.", recno)
eno=int(input("\tEmployee number : "))
ename=input("\tEmployee Name : ")
ebasic=int(input("\tBasic Salary : "))
allow=int(input("\tAllowances : "))
totsal=ebasic+allow
print("\tTOTAL SALARY : ", totsal)

edata=[eno,ename,ebasic,allow,totsal]
pickle.dump(edata,bfile)

ans=input("Do you wish to enter more records (y/n)? ")


recno=recno+1
if ans.lower()=='n':
print("Record entry OVER ")
break
bfile.close()
rbfile=open("empfile.dat","rb")
readrec=1
try:
while True:
edata=pickle.load(rbfile)
print("Record Number : ",readrec)
print(edata)
readrec=readrec+1
except EOFError:
print('Sorry No more records to show now.')
pass
rbfile.close()

# Write a program that have a binary file “Book.dat”. The file has structure [BookNo, Book_Name, Author,
Price]. Now do as directed: -

1) Write a user defined function CreateFile() to input data for a record and add to Book.dat.
2) Write the definition of show_all_books() UDF to display the details of all the books.
3) Write a User Defined function CountRec(Author) in Python which accepts the Author name as
parameter, display the details of the book of the same author also return the number of books of the
given Author are stored in the binary file “Book.dat"

import pickle

def createfile():
fobj=open("Book.dat","ab")

BookNo=int(input("Enter Book Number : "))


Book_name=input("Enter book Name :")
Author = input("Enter Author name: ")
Price = int(input("Price of book : "))
rec=[BookNo, Book_name ,Author, Price]
pickle.dump(rec, fobj)
print("Record saved")
fobj.close()

def show_all_books():
fobj=open("Book.dat","rb")
L1=[]

try:
while True:
L1=pickle.load(fobj)
print(L1)

except EOFError:
print('All Record displayed')
pass
fobj.close()

def countrec(Author):
fobj=open("Book.dat", "rb")
cnt = 0
print("Book No Book Name Author Price")

try:
while True:
r=pickle.load(fobj)
if Author==r[2]:
cnt = cnt + 1
print(r[0],"\t",r[1],"\t",r[2],'\t',r[3])
except:
print()

fobj.close()
return cnt

# Write a program that uses a binary file “STUDENT.DAT” has structure [roll_number, Name, Percentage].
Write a User defined function countrec() that would read contents of the file
“STUDENT.DAT” and display the details of those students whose percentage is above 75%.

import pickle

def createfile():
fobj=open("student.dat","ab")

rno=int(input("Enter Roll No : "))


nm=input("Enter Name :")
p = int(input("Enter percentage: "))

rec=[rno, nm , p]
pickle.dump(rec, fobj)
print("Record saved")
fobj.close()

def countrec():
fobj=open("student.dat","rb")
num = 0
try:
while True:
rec=pickle.load(fobj)

if rec[2]>375:
num = num + 1
print(rec[0],rec[1],rec[2])
except:
fobj.close()
return num

createfile()
countrec()

CSV FILES: -

CSV stands for Comma Separated Values. It is a type of plain text file that uses specific structure to
arrange tabular data i.e. data stored in rows and columns such as a spreadsheet or database.

CSV is like a text file, It is in human readable format and extensively used to store tabular data, in a
spreadsheet or database.

Each line of the csv file is a data record. Each record consists of one or more fields, separated by
commas. The separator character of CSV files is called a delimiter.

Default delimiter is (,). Other delimiters are tab(\t), colon (:), pipe(|), semicolon (;) characters.

READING FROM A CSV FILE

To read data from csv files, reader() method of csv module is used.
csv.reader() returns a reader object.
STEPS TO READ

1) import csv module

import csv

2) Open csv file in read mode.


f = open(“csv_demo.csv”,”r”)

3) Create the reader object.


demo_reader = csv.reader(f)

4) Fetch data through for loop, row by row.


for x in demo_reader:
print(x)

5) Close the file


f.close()

# Program to read from a csv file import csv

f = open("emp.csv","r")
emp_reader=csv.reader(f)
for row in emp_reader:
print(row[0],row[1],row[2]) # print(row) : As a list
f.close()

WRITING IN TO CSV FILES:

To write data into csv files, writer() function of csv module is used.

csv.writer(): This function returns a writer object which writes data into writer object.
Significance of writer object

The csv.writer() returns a writer object that converts the data into a delimited string.The string
can be later converted into csv files using the writerow() or writerows() method.
Syntax:

<writer_object>.writerow() :

Writes one row of data in to the writer object.

<writer_object>.writerows()

Writes multiple rows into the writer object.

# Program to write data into a CSV File and to read data stored in csv file

import csv
f=open("mycsv.csv","w",newline='')
w=csv.writer(f)
lst=["RNO","NAME","MARKS"]
w.writerow(lst)

n=int(input('Enter how many students record you want to add'))


for x in range(n):
r=int(input('Enter rno'))
n=input('Enter name')
m=int(input('Enter marks'))
lst2=[r,n,m]
w.writerow(lst2)
f.close()

f=open("mycsv.csv","r")
rec=csv.reader(f)
for i in rec:
print(i)
f.close()

# Program to write Employee Name, EmpID and Dept for some employees in a csv file then display
records of all the employees.

import csv

f=open("emp.csv","w",newline='')
emp_writer = csv.writer(f)

emp_writer.writerow(["EmpName","EmpID","Dept"])

emp_rec = []

while True:
print("Enter Employee details: ")
empname = input("EmpName : ")
eid = int(input("EmpID : "))
dept = input("Department : ")

emp_rec.append([empname,eid,dept])

ch = input("Do you want to continue ?? (Y?N)")

if ch == "N" or ch =="n":
break

emp_writer.writerows(emp_rec)

f.close()

f=open("emp.csv","r")

rec=csv.reader(f)

for i in rec:
print(i)

f.close()

Question based on CSV file Handling: -

Madhwan, is doing internship in “SQUARE Solutions Pvt. Ltd.”. He wrote the following python code to store
student’s data in csv file (Student.csv) handling. Unfortunately, he forgot some steps of the python code.
Please help him to create a CSV File 'Student.csv' by completing the code.

CSV File
1,SAKSHAM,XII,A
2,ARNAV,XI,A
3,SHREEVALI,XII,A
4,BHOOMI,XI,A
5,SWARIT,XII,A

The Incomplete Code written by Madhwan is as follows: -

import_____ #Statement-1

csvfh = open(_____, _____, newline='') #Statement-2

stuwriter = csv._____ #Statement-3

data = []

header = ['ROLL_NO', 'NAME', 'CLASS', 'SECTION']

data.append(header)

for i in range(5):
roll_no = int(input("Enter Roll Number : "))
name = input("Enter Name : ")
Class = input("Enter Class : ")
section = input("Enter Section : ")
rec = [_____] #Statement-4
data.append(rec)
stuwriter. _____ (data) #Statement-5

csvfh.close()

a) Identify the suitable code for Statement-1.

a) csv file
b) CSV
c) csv
d) Csv

Correct Answer : c) csv

b) Identify the correct parameters for Statement-2?

a) "School.csv","w"
b) "Student.csv","w"
c) "Student.csv","r"
d) "School.csv","r"

Correct Answer: b) "Student.csv","w"

c) Choose the function name (with argument) to complete Statement-3

a) reader(csvfh)
b) reader(MyFile)
c) writer(csvfh)
d) writer(MyFile)

Correct Answer : c) writer(csvfh)

d) Identify the suitable code for Statement-4.

a) 'ROLL_NO', 'NAME', 'CLASS', 'SECTION'


b) ROLL_NO, NAME, CLASS, SECTION
c) 'roll_no','name','Class','section'
d) roll_no,name,Class,section

Correct Answer : d) roll_no,name,Class,section

e) Choose the function name that should be used for Statement-5 to create the desired CSV File.

a) dump()
b) load()
c) writerows()
d) writerow()

Correct Answer : c) writerows()

You might also like