0% found this document useful (0 votes)
26 views81 pages

File Handling

This document covers file handling in programming, specifically focusing on text and binary files in Python. It details the types of files, methods for opening, reading, writing, and closing files, as well as the differences between text and binary file handling. Additionally, it discusses CSV files, file modes, and the importance of data persistence beyond program execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views81 pages

File Handling

This document covers file handling in programming, specifically focusing on text and binary files in Python. It details the types of files, methods for opening, reading, writing, and closing files, as well as the differences between text and binary file handling. Additionally, it discusses CSV files, file modes, and the importance of data persistence beyond program execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 81

FILE HANDLING

1
LEARNING OBJECTIVES
At the end of this chapter you will be able to learn :

❏ Understanding Files
❏ Types of Files
❏ Understanding Text Files
❏ Opening and closing Text files
❏ Reading and writing in Files
❏ Understanding Binary Files
❏ Pickling and Unpickling
❏ Opening and closing Binary files
❏ Reading and writing in Binary Files
❏ CSV (Comma separated values) files. 2
NEED FOR DATA FILE HANDLING

● Mostly, in programming languages, all the values or data are stored in some
variables which are volatile in nature.
● Because data will be stored into those variables during run-time only and will be
lost once the program execution is completed. Hence it is better to save these data
permanently using files.
● Python offers provision to create files through python programs.

3
INTRODUCTION
● Data Files – The data files are the files that store data pertaining to a specific
application, for later use. The data files can be stored in two ways :
● Text files

● Binary files

4
TEXT FILE
● Stores information in the form of ASCII or Unicode characters.
● Each line of text is terminated (delimited)by a special character known as End Of
Line(EOL) character.
● In text files, some internal translations take place when this EOL character is
encountered
● By default the EOL character is the newline character (‘\n’) or carriage return,
newline combination(‘\r\n’)

5
TEXT FILE
● Text files can be following types
● Regular Text files – store the text in the same form as it is typed and have
extension .txt
● Delimited Text files – In these specific character is stored to separate the values
i.e. a tab or a comma after every value.
● When a tab character is used to separate the values they are called TSV files
(Tab Separated files). They have extension .txt or .tsv
● When a comma is used to separate the values they are called CSV files
(Comma Separated Values). They have extension .csv

Regular Text file content -------- I am simple text.

TSV file content --------- I → am → simple → text.

CSV file content --------- I, am, simple, text. 6


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(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.

7
8
BINARY FILE
● A file that contains information in the same format in which information
is held in memory.
● Binary file contains arbitrary binary data.
● i.e the file content that is returned to you is raw (with no translation)
● Python provides special module(s) for encoding and decoding of data
for binary file.
● In binary file there is no delimiter for a line.

9
STEPS TO PROCESS A FILE
1. Determine the type of file usage.
a. Reading purpose : If the data is to be brought in from a file to memory
b. Writing purpose : If the data is to be sent from memory to file.
2. Open the file and assign its reference to a file object or file-handle.
3. Process the file as required : Perform the desired operation from the file.
4. Close the file.

10
OPENING A TEXT FILE
● The key function for working with files in Python is the open() function.
● It accepts two parameters : filename, and mode.

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

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

● Can specify if the file should be opened in read or write mode :


○ “r - read - Default mode
○ “w" - write
○ “a”- append 11
OPENING A TEXT FILE
Example: f = open(“e:\\main\\demo.txt”,”r”)

f = open(r “e:\main\demo.txt”,”r”)

( r indicates raw string – there is no special meaning


attached to any character)

Note : the default file mode is read mode

12
● File Object / File-Handle:
○ It serves as a link to file residing in your computer.
○ It is a reference to the file on the disk. It opens and makes it available for
a number of different task.
● File Access modes / file-mode:
○ It governs the type of operations(such as read or write or append)
possible in the opened file. Or it refers to how the file will be used once it
is opened.

13
Text Binary Description ● Various Modes for opening a text file are:
Mode Mode

“r” “rb” Read Default mode. Opens a file for reading, error if the file does not exist.

“w” “wb” Write Opens a file for writing, creates the file if it does not exist.
If exist truncate and over-write in the file.

“a” “ab” Append Retains the data in the file and appends the new data, creates the file
if it does not exist.( write only mode)

“r+” “r+b” or Read and File must exist otherwise error is raised.Both reading and writing
“rb+” Write operations can take place.

“w+” “w+b” or Write and File is created if it does not exist.If the file exists past data is lost
“wb+” Read (truncated).Both reading and writing operations can take place.

“a+” “a+b” or Append and File is created if it does not exist.If the file exists past data is not lost
“ab+” Read .Both reading and writing(appending) operations can take place.
14
CLOSING A FILE
● close()- function breaks the link of file-object and the file on the disk. After
close() no task can be performed on that file through the file-object.

Syntax:

<fileObject>.close()

Example : f.close()

● It is important to close your files, if the program exits unexpectedly there are
chances that the data may be lost.

15
READING FROM A FILE
➔ A Program reads a text/binary file from hard disk. Here File acts like an input to the
program.
➔ Followings are the methods to read a data from the file:
◆ read() METHOD
◆ <filehandle>.read([n]) --- returns the read bytes in the form of string
◆ readline() METHOD
◆ <filehandle>.readline([n]) --- returns the read bytes in the form of string
◆ readlines() METHOD
◆ <filehandle>.readlines() --- returns the read bytes in the form of list.

16
read() METHOD
● By default the read() method returns the whole text, but you can also specify
how many characters/bytes you want to return by passing the size as argument.

Syntax: <file_object>.read([n])

where n is the size

● To read entire file : <file_object>.read()


● To reads only a part of the File : <file_object>.read(size)

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

print(f.read(15))

Returns the 15 first characters of the file "demo.txt". 17


18
19
readline() METHOD

● readline() will return a line read, as a string from the file.

Syntax:

<file_object>.readline([n])

● Example

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

print(f.readline())

● This example program will return the first line in the file “demo.txt”
20
irrespective of number of lines in the text file.
21
22
● Another way of printing file line by line

myfile=open(“poem.txt”,”r”)
for line in myfile:
print(line)

23
readlines() METHOD
● readlines() method reads all the lines and returns them in a list
● 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("demofile.txt", "r")

print(f.readlines())

f.close() 24
Read a file using Readlines()

25
# To find the total no of characters
myfile=open(“aa.txt”, “r”)
a=myfile.read()
print(len(a))
myfile.close()

#To find no of characters in a line


myfile=open(“aa.txt”, “r”) Including EOF(End
a=myfile.readline() of File character)
print(len(a))
myfile.close()

#To find total no of lines


myfile=open(“aa.txt”, “r”)
a=myfile.readlines()
print(len(a))
myfile.close()

26
#To find no of characters
myfile=open("aa.txt", "r")
s1=" "
with_eof=0
without_eof=0
while s1:
without EOF(End of
s1=myfile.readline()
File character)
with_eof=with_eof+len(s1)
without_eof=without_eof+len(s1.strip())
print("total characters with eof",with_eof)
print("total characters without eof",without_eof)
myfile.close()

strip() removes the given character from both ends


rstrip() removes the given character from trailing end i.e right end
lstrip() removes the given character from leading end i.e left end
27
WRITING TO A TEXT FILE
● A Program writes into a text/binary file in hard disk.
● Followings are the methods to write a data to the file.
○ write () METHOD
○ <filehandle>.write(str) –Writes a string to the file
○ writelines() METHOD
○ <filehandle>.write(L) -- Writes all strings in the list to file
● To write to an existing file, you must add a parameter to the open()

Function which specifies the mode :

○ "a" - Append - will append to the end of the file


○ "w" - Write - will overwrite any existing content
28
write() Method
● <filehandle>.write(strl)
● write() method takes a string ( as parameter ) and writes it in the file.
● For storing data with end of line character, you will have to add \n character to end
of the string
● Example:

Open the file "demo_append.txt" and append content to the file:

f = open("demo_write.txt", "a”)

f.write("Hello students \n We are learning data file handling…..!")

f.close()

f = open("demo_write.txt", "r") #open and read the file after the appending
29
print(f.read())
30
s=open("newwrite.txt","w")
for r in range(5):
new1=input("enter name")
s.write(new1+"\n")
s.close()

31
This program will both read and write

s=open("aa.txt","a+")
s.write("hello")
s.seek(0) ---places the file-pointer to beginning of file
print(s.read())
s.close()

32
writelines() METHOD
● For writing a string at a time, we use write() method, it can't be used

for writing a list, tuple etc. into a file.

● Python file method writelines() writes a sequence of strings to the file.


The sequence can be any iterable object producing strings, typically a
list of strings.
● So, whenever we have to write a sequence of string, we will use
writelines(), instead of write().
● writelines() method writes all strings in a list

33
34
35
With r+, the position is initially at the beginning, but reading it once will push it towards the end, allowing you to append.
With a+, the position is initially at the end.

with open("filename", "r+") as f:


# here, position is initially at the beginning
text = f.read()
# after reading, the position is pushed toward the end
f.write("stuff to append")

with open("filename", "a+") as f:


# here, position is already at the end
f.write("stuff to append")

If you ever need to do an entire reread, you could return to the starting position by doing f.seek(0).

s=open("aa.txt","a+")
s.write("hello")
s.seek(0) -----places the file-pointer to beginning of file
print(s.read())
s.close()

37
The flush()
The flush function forces the writing of data on disc still pending on the
output buffer.

Syntax : <file_object>.flush()

38
39
Standard input, output and error streams
The standard input and output devices are implemented as files in python
called standard streams.
These standard streams i.e
stdin --- to read from keyboard
stdout – to print to the display
stderr – same as stdout but used for errors.
can be used by importing sys module

40
Standard input, output and error streams

41
with statement
The with statement will automatically close the file after the
nested block of code.

with open(“aa.txt”, “r”) as f:


f.write (“Hi”)

Even if an exception (a runtime error) occurs before the end of


the block, the with statement will handle it and close the file.

42
Relative path and absolute path

The absolute path are from the topmost level of the directory structure.
Ex. E:\\accounts\\history\\cash.act

The relative paths are relative to the current working directory


denoted as a dot(.) while its parent directory is denoted with two dots(..)
1) ..\\history\\cash.act
2) .\\cash.act

43
DATA FILE HANDLING IN BINARY
FILES
● Files that store objects as some byte stream are called binary files.
● That is, all the binary files are encoded in binary format , as a sequence of
bytes, which is understood by a computer or machine.
● In binary files, there is no delimiter to end the line.
● Since they are directly in the form of binary, there is no need to translate
them hence faster and easier to access.
● But they are not in human readable form and hence difficult to

understand.

44
Opening and closing binary files
Opening a binary file:

Similar to text file except that it should be opened in binary


mode.Adding a ‘b’ to the text file mode makes it binary - file mode.

EXAMPLE :

f = open(“demo.dat”,”rb”)

Closing a binary file

f.close()

45
Text Binary Description ● Various Modes for opening a text file are:
Mode Mode

“r” “rb” Read Default mode. Opens a file for reading, error if the file does not exist.

“w” “wb” Write Opens a file for writing, creates the file if it does not exist.
If exist truncate and over-write in the file.

“a” “ab” Append Retains the data in the file and appends the new data, creates the file
if it does not exist.( write only mode)

“r+” “r+b” or Read and File must exist otherwise error is raised.Both reading and writing
“rb+” Write operations can take place.

“w+” “w+b” or Write and File is created if it does not exist.If the file exists past data is lost
“wb+” Read (truncated).Both reading and writing operations can take place.

“a+” “a+b” or Append and File is created if it does not exist.If the file exists past data is not lost
“ab+” Read .Both reading and writing(appending) operations can take place.
46
https://www.youtube.com/watch?v=e5jbgyHf4-M

● Text files cannot deal with other data objects except strings
● Sometimes we need to read and write non-simple objects like List,
tuples and dictionaries on to the files
● These Objects have a specific structure to be maintained while
storing or accessing them.
● Thus objects are often serialized and then stored in binary files.

47
● Serialization / PICKLING refers to the process of converting the Python
object to a byte Stream(machine readable format) before writing to a file.

● UNPICKLING is used to convert the byte stream back to the original

structure while reading the contents of the file.

48
● Thus Python provides a special module called pickle module for to
read and write objects likes list, tuples and dictionaries into
a file.

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

import pickle

● It provides two main functions :


○ dump()
○ load()

49
Pickling and Unpickling

51
pickle.dump() Method
● pickle.dump() method is used to write the object in file which is
opened in binary access mode.

Syntax :

pickle.dump(<object-to-be-written>,<FileObject>)

● <object-to-be-written> can be any sequence in Python such as list,


dictionary etc.
● <FileObject> is the file handle of file in which we have to write.

52
binary_file.dat file
after execution of
the program.

53
pickle.load() Method
● pickle.load() method is used to read data from a file

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.

54
(reads one record at a time)
so should be inside while loop

55
Random Access in Files : tell() and seek()
❏ tell() function is used to obtain the current position of the file pointer

Syntax : f.tell()

❏ File pointer is like a cursor, which determines from where data has to be
read or written in the file.

58
Random Access in Files : tell()

59
Random Access in Files : tell() and seek()
❏ seek () function is used to change the position of the file pointer to a given
position.

Syntax : f.seek(offset,reference_point)

Where f is the file object

60
61
62
CSV files
● CSV stands for Comma Separated Values.
● CSV files are delimited files that store tabular data (in spreadsheet or
database) where comma delimits every value.
● Each line of the 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.

63
Python CSV Module
● CSV module provides two types of objects :
○ reader : to read from cvs files
○ writer : to write into csv files.
● To import csv module in our program , we use the following statement:

import csv

64
Opening / Closing a CSV File
● Open a CSV File :

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

OR

f = open(“demo_csv.csv”,”w”)

● Close a CSV File:

f.close()

65
Role of argument newline in opening
of csv files :
● newline argument specifies how would python handle new line characters
while working with csv files on different Operating System.
● Additional optional argument as newline = “”(null string no space in
between) with the file open() will ensure that no translation of EOL character
takes place.

● F=open(“stu.csv”,”w”,newline=“”)
While writing if newline is used it displays output without
a newline(or space in between two lines)
66
Writing in CSV Files
● csv.writer() :

Returns a writer object which writes data into csv files.

● writerow() :

Writes one row of data onto the writer object.

Syntax : <writer_object>.writerow()

● writerows() :

Writes multiple rows into the writer object

Syntax : <writer_object>.writerow() 67
Steps to write data to a csv file
1. Import csv module

import csv

2. Open csv file in write mode.

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

3. Create the writer object.

demo_writer = csv.writer(f) (by default delimiter is comma(,))

OR(for using other delimiter) - demo_writer = csv.writer(f, delimiter=“|”)

4. Write data using the methods writerow() or writerows()


demo_writer.writerow(<iterable_object>)
5. Close the file
f.close() 68
Writing in CSV Files
● To write data into csv files, writer() function of csv module is used.
● csv.writer():
○ Returns a writer object which writes data into csv files.

● Significance of writer object


○ The csv.writer() returns a writer object that converts the data into
csv writable form i.e. a delimited string form.

69
● <writer_object>.writerow() :

Writes one row of data in to the writer object.

● <writer_object>.writerows()

Writes multiple rows into the writer object.

71
Write a program to create a CSV file to store student data. Write five records into the file.

import csv
fh=open("student.csv","w“,newline=“”)
swriter=csv.writer(fh)
swriter.writerow(['rollno','name','marks'])
for i in range(5):
print("student record:",i+1)
rollno=int(input("enter rollno"))
name=input("enter name")
marks=float(input("enter marks"))
sturec=[rollno,name,marks]
swriter.writerow(sturec)
fh.close()

While writing if newline is used it displays output without a newline(or space in between two lines)

72
The writerows() function

Create a nested sequence out of the data and then write using the writerows() function

Write a program to create a CSV file.

import csv
fh = open("compresult.csv", "w")
cwriter = csv.writer(fh)
compdata = [ ['Name', 'Points', 'Rank’], ['Shradha', 4500, 23],
['Nishchay', 4800, 31], ['Ali', 4500, 25], ['Adi', 5100, 14] ]

cwriter.writerows (compdata)
fh.close()

73
Reading from CSV Module
● To read data from csv files, reader function of csv module is used.

● csv.reader() :

Returns a reader object.

It loads data from a csv file into an iterable after parsing delimited data.

74
Steps to read data from a csv file
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 rec in demo_reader:
print(rec)

5. Close the file


f.close()
75
import csv Using with statement
fh=open("student.csv","r") import csv
sreader=csv.reader(fh) with open("student.csv","r") as fh
for tr in sreader: sreader=csv.reader(fh)
print(tr) for tr in sreader:
print(tr)
fh.close()

Using newline
import csv
fh=open("student.csv","r“, newline=“\r\n”)
sreader=csv.reader(fh) While reading if newline is used it displays
for tr in sreader: output without a newline(or space in
print(tr) between two lines)

fh.close()
77
QUESTIONS FOR
PRACTICE
Write a method in python to read the content from a file “Poem.txt”
and display the same on the screen.

Write a method in python to read the content from a file “Poem.txt”


line by line and display the same on the screen.

Write a method in python to count the number of lines from a text


file which are starting with an alphabet T.

Write a method in Python to count the total number of words in a file

78
Write a method in python to read from a text file “Poem.text” to
find and display the occurrence of the word “Twinkle”

Write a method Bigline() in Python to read lines from a text file


“Poem.txt” and display those lines which are bigger than 50
characters.

Write a method to read data from a file. All the lowercase letter
should be stored in the file “lower.txt” , all upper case characters
get stored inside the file “upper.txt” and other characters stored
inside the file “others.txt”

79
● Text file ● Binary File
Methods Import pickle
1. <filehandle>.read() -- returns string Functions :
2. <filehandle>.readline() ---returns string 1. pickle.dump(<obj_to_be_written>,<filehandle>)
3. <filehandle>.readlines() ---returns list
2. pickle.load(<filehandle>) (reads one record at a time) so
4. <filehandle>.write(<str>)
should be inside while loop
5. <filehandle>.writelines(<list>)
▪ while reading should use try
and except block
for loop is required for reading all
● CSV File
Import csv
Functions :
1. <nameofwriterobject>= csv.writer(<filehandle>)
2. <nameofwriterobject>.writerow(<list>)
3. <nameofwriterobject>.writerows(<nested list>)
4. <nameofreaderobject>= csv.reader(<filehandle>) - returns list, for loop is required for reading all records

80
THANK YOU

81

You might also like