0% found this document useful (0 votes)
79 views34 pages

File Handling - XII - CS

The document discusses file handling in Python. It defines what a file is and explains the need for data files and different types of files like text files, binary files, and CSV files. It then covers topics like opening, closing, reading, writing, and manipulating files in Python.

Uploaded by

Tadi Vyshnavi
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)
79 views34 pages

File Handling - XII - CS

The document discusses file handling in Python. It defines what a file is and explains the need for data files and different types of files like text files, binary files, and CSV files. It then covers topics like opening, closing, reading, writing, and manipulating files in Python.

Uploaded by

Tadi Vyshnavi
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/ 34

KENDRIYA VIDYALAYA KARIMGANJ

FILE HANDLING IN PYTHON


Class XII (CS)

Prepared By: Siddharth Dhage


PGT CS
Contents
File Handling
Need for a data file
Types of Files
Text Files
Binary Files
CSV
FILE HANDLING

Definition: A file is some information or data which stays in the


computer storage devices.
A file is a contiguous set of bytes used to store data.
e.g. music files, video files, text files.
Need for a Data File

To Store data in organized manner


To store data permanently
 To access data faster
To Search data faster
To easily modify data later on
Types of Files

Text Files

Binary Files

Comma Separated Value Files


Text Files

Text files don’t have any specific encoding and it can


be opened in normal text editor itself.
Example:
Web standards: html, XML, CSS, JSON etc.
Source code: c, app, js, py, java etc.
Documents: txt, tex, RTF etc.
Configuration: ini, cfg, reg etc.
BASIC OPERATIONS ON A TEXT FILE

Open (filename – absolute or relative path, mode)

Close

Read/Write

Manipulation of Data

Appending Data into a text file


File Opening
• Python has an in-built function called open() to open a file.
• It takes a minimum two argument as mentioned in the
below syntax.
Example: fo = open(“C:/Documents/Python/test.txt”, “r+”)
Syntax

Here, file_name File_object=open(file_name,


is the name of the file ormode)
the location of the file
that you want to open, and file_name should have the file
extension included. e.g. in test.txt – the term test is the name of
the file and .txt is the extension of the file.
Modes are ‘r’, ‘w’ and ‘a’ .
File Opening Contd…
Absolute Path:
• Absolute file paths are notated by a leading forward slash or drive
label. For example C:/system32/cmd.exe
• An absolute path, which always begins with the root folder.

Relative Path:
A relative path, which is relative to the program’s current working
directory.
e.g. ‘.\test.txt’ or ‘test.txt’
File Opening Contd…
Modes: The mode in the open function syntax will tell Python
as what operation you want to do on a file.
Modes for opening Text Files are given in below table.
‘r’ Read Mode Read mode is used only to read data from the file
This mode is used when you want to write data into the file
‘w’ Write Mode file or modify it. Remember write mode overwrites the
data present in the file.
Append mode is used to append data to the file.
‘a’ Append Mode Remember data will be appended at the end of the file
pointer.
Read or Write This mode is used when we want to write or read the data
‘r+’
Mode from the same file.
Append or Read This mode is used when we want to read data from the file
‘a+’
Mode file or append the data into the same file.
File Opening Contd…
Modes for opening Binary Files are given in below table.

‘rb’ Read Mode Open a file for the read-only mode in the binary format

‘wb’ Write Mode Open a file for write only mode in the binary format

‘ab’ Append Mode Open a file for appending only mode in the binary format

Read or Write Open a file for read and write only mode in the binary
‘rb+’
Mode format

Append or Read Open a file for appending and read-only mode in the
‘ab+’
Mode binary format
Closing a File
In order to close a file, we must first open the file. In
python, we have an in-built method called close() to close
the file which is opened.
Whenever you open a file, it is important to close it,
especially, with write method. Because if we don’t call the
close function after the write method then whatever data
we have written to a file will not be saved into the file.
Example:
my_file = open(“C:/Documents/Python/test.txt”, “r”)
print(my_file.read())
my_file.close()
Reading/Writing a File in Python
There are three ways in which we can read the files in python.

read([n])
readline([n])
readlines()
Here, n is the number of bytes to be read.
First, let's create a sample text file as shown below.
Reading a File Contd..
Example 1:
my_file = open(“C:/Documents/Python/test.txt”, “r”)
print(my_file.read(5))
Output:
Hello
Example 2:
my_file = open(“C:/Documents/Python/test.txt”, “r”)
print(my_file.read())
Output:
Hello World
Hello Python
Good Morning
Reading a File Contd..

Example 3:
my_file = open(“C:/Documents/Python/test.txt”, “r”)
print(my_file.readline(2))
Output:
He
Example 4:
my_file = open(“C:/Documents/Python/test.txt”, “r”)
print(my_file.readline())
Output:
Hello World
Reading a File Contd..
Example 5:
my_file = open(“C:/Documents/Python/test.txt”, “r”)
print(my_file.readlines())
Output:
[‘Hello World\n’, ‘Hello Python\n’, ‘Good Morning’]

Example 6: Reading Specific Line From a File


line_number = 4
fo = open(“C:/Documents/Python/test.txt”, ’r’)
currentline = 1
for line in fo:
if(currentline == line_number):
print(line)
break
currentline = currentline +1
Output:
How are you
Reading a File Contd..
Example 7:
filename = “C:/Documents/Python/test.txt”
filehandle = open(filename, ‘r’)
filedata = filehandle.read()
print(filedata)
Output:
Hello World
Hello Python
Good Morning
How are You
Writing to a File
Write() method is used to write data into a file.
We have two methods write(string) writelines(list) for writing data.
Example 1:
my_file = open(“C:/Documents/Python/abc.txt”, “w”)
my_file.write(“Hello World\n”)
my_file.write(“Hello Python”)
Output:
Writing to a File

Example 2:
fruits = [“Apple\n”, “Orange\n”, “Grapes\n”, “Watermelon”]
my_file = open(“C:/Documents/Python/test.txt”, “w”)
my_file.writelines(fruits)

Output:
Manipulation of Data in a File

There are various functions to modify the content of


a file in python.
 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
Appending Data in a File
To append data into a file we must open the file in ‘a+’ mode
so that we will have access to both the append as well as
write modes.
Example 1:
my_file = open(“C:/Documents/Python/test.txt”, “a+”)
my_file.write (“Strawberry”)
Output:
Appending Data in a File Contd..
Example 2:
fruits = [“\nBanana”, “\nAvocado”, “\nFigs”, “\nMango”]
my_file = open(“C:/Documents/Python/test.txt”, “a+”)
my_file.writelines(fruits)
Output:
Appending Data in a File Contd..
Example 3:
text=["\nHello","\nHi","\nPython"]
my_file=open("C:/Documents/Python/test.txt",mode="a+")
my_file.writelines(text)
print("where the file cursor is:",my_file.tell())
my_file.seek(0)
for line in my_file:
print(line)

In the above code, we are appending the list of data into the ‘test.txt’ file. Here, you
can observe that we have used the tell() method which prints where the cursor is
currently at.

seek(offset): The offset takes three types of arguments namely 0,1 and 2.
When the offset is 0: Reference will be pointed at the beginning of the file.
When the offset is 1: Reference will be pointed at the current cursor position.
When the offset is 2: Reference will be pointed at the end of the file.
Python File Methods
Function Explanation
open() To open a file
close() Close an open file
fileno() Returns an integer number of the file
read(n) Reads ‘n’ characters from the file till end of the file

readable() Returns true if the file is readable


readline() Read and return one line from the file
readlines() Reads and returns all the lines from the file

seek(offset) Change the cursor position by bytes as specified by the offset


offset
seekable() Returns true if the file supports random access

tell() Returns the current file location


writable() Returns true if the file is writable
write() Writes a string of data to the file
writelines() Writes a list of data to the file
Standard Input, Output and error Streams

Standard Input stdin Reads from Keyboard


Device
Standard Output stdout Display output on monitor
Device
Standard Error stderr Same as stdout but normally only for errors.
Device

 These devices are implemented as files called standard streams.


 Standard stream files can be used by importing sys module.

Example:
import sys
fh=open(“D:\Test.txt”)
line=fh.readline()
sys.stdout.write(line)
sys.stderr.write(“No errors occurred…\n”)
Binary Files
Binary files can only be processed by an application that know or understand the
file’s structure. In other words, they must be applications that can read and interpret
binary.
Example:
Document files: .pdf, .doc, .xls etc.
Image files: .png, .jpg, .gif, .bmp etc.
Video files: .mp4, .3gp, .mkv, .avi etc.
Audio files: .mp3, .wav, .mka, .aac etc.
Database files: .mdb, .accde, .frm, .sqlite etc.
Archive files: .zip, .rar, .iso, .7z etc.
Executable files: .exe, .dll, .class etc.

All binary files follow a specific format. We can open some binary files in the normal
text editor but we can’t read the content present inside the file. That’s because all
the binary files will be encoded in the binary format, which can be understood only
by a computer or machine.
Basic Operations on Binary Files
Binary files store data in the binary format (0’s and 1’s) which is understandable by
the machine. So when we open the binary file in our machine, it decodes the data
and displays in a human-readable format.

Open():
open() method is used the open the file with two parmeters i.e. file name and mode.

e.g. my_file = open(“C:/Documents/Python/bfile.bin”, “wb+”)

Close():
Close() method is used to close the file.
e.g. my_file = open(“C:/Documents/Python/bfile.bin”, “wb+”)
my_file.close()
Pickle Module in Binary Files
• Pickle is used for serializing and de-serializing Python
object structures, also called marshalling or flattening.
• Data in the form of dictionaries, DataFrames, or any
other data type can be saved to a file, for this purpose
Pickle module is used.
• Pickle module serializes objects so they can be saved to
a file, and loaded in a program again later on.
• If you want to use data across different programming
languages, pickle is not recommended.
Pickle dump() and load() Methods
pickle.dump() function is used to store the object data to the file. It
takes 3 arguments.First argument is the object that we want to
store. The second argument is the file object we get by opening the
desired file in write-binary (wb) mode. And the third argument is the
key-value argument. This argument defines the protocol. There are two
type of protocol – pickle.HIGHEST_PROTOCOL and
pickle.DEFAULT_PROTOCOL.

Pickle.load() function is used to retrieve pickled data.The steps are quite


simple. We have to use pickle.load() function to do that. The primary
argument of pickle load function is the file object that you get by
opening the file in read-binary (rb) mode.
Read, Write and create operations in Binary Files
Example:
my_file = open(“C:/Documents/Python/bfile.bin”, “wb+”)
message = “Hello Python”
file_encode = message.encode(“ASCII”)
my_file.write(file_encode)
my_file.seek(0)
bdata = my_file.read()
print(“Binary Data:”, bdata)
ntext = bdata.decode(“ASCII”)
print(“Normal data:”, ntext)
Output:
Binary Data: b'Hello Python'
Normal data: Hello Python

In the above example, first we are creating a binary file ‘bfile.bin’ with the read
and write access and whatever data you want to enter into the file must be
encoded before you call the write method.
Also, we are printing the data without decoding it, so that we can observe how
the data exactly looks inside the file when it’s encoded and we are also
printing the same data by decoding it so that it can be readable by humans.
Search, Append and Update Operations in Binary Files
Approach:
Step 1: Searching for the word in the binary file.
Step 2: While searching in the file, the variable “pos” stores
the position of file pointer record then traverse(continue)
reading of the record.
Step 3: If the word to be searched exists then place the
write pointer (to ending of the previous record) i.e. at pos.
Step 4: Call write() function to take the new record.
Step 5: Write the new object at the position “pos” and
hence the record is updated and print “record successfully
updated”.
Step 6: If the word does not exists then print “record not
found”.
CSV Files
 CSV (Comma Separated Values) is a simple file
format used to store tabular data, such as a
spreadsheet or database.
 CSV file stores tabular data (numbers and text) in
plain text.
 Each line of the file is a data record. Each record
consists of one or more fields, separated by
commas. The use of the comma as a field
separator is the source of the name for this file
format.
 For working CSV files in python, there is an inbuilt
module called csv
csv.reader() & csv.writerow() method
Reading a CSV file
# importing csv module
import csv
filename = "aapl.csv“ # csv file name
# initializing the titles and rows list
fields = []
rows = []
# reading csv file
with open(filename, 'r') as csvfile:
csvreader = csv.reader(csvfile) # creating a csv reader object
fields = next(csvreader) # extracting field names through first row
# extracting each data row one by one
for row in csvreader:
rows.append(row)
print("Total no. of rows: %d"%(csvreader.line_num)) # get total number of rows

print('Field names are:' + ', '.join(field for field in fields)) # printing the field names
print('\nFirst 5 rows are:\n') # printing first 5 rows
for row in rows[:5]:
for col in row: # parsing each column of a row
print("%10s"%col),
print('\n')
Writing to a CSV file
# importing the csv module
import csv
# field names
fields = ['Name', 'Branch', 'Year', 'CGPA']
# data rows of csv file
rows = [ ['Nikhil', 'COE', '2', '9.0'],
['Sanchit', 'COE', '2', '9.1'],
['Aditya', 'IT', '2', '9.3'],
['Sagar', 'SE', '1', '9.5'],
['Prateek', 'MCE', '3', '7.8'],
['Sahil', 'EP', '2', '9.1']]
filename = "university_records.csv“ # name of csv file
# writing to csv file
with open(filename, 'w') as csvfile:
csvwriter = csv.writer(csvfile) # creating a csv writer object
csvwriter.writerow(fields) # writing the fields
csvwriter.writerows(rows) # writing the data rows

You might also like