File Handling - XII - CS
File Handling - XII - CS
Text Files
Binary Files
Close
Read/Write
Manipulation of Data
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 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
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
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.
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.
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