10._ File Handling in Python.docx
10._ File Handling in Python.docx
1
Website: https://pythonlife.in/
in
we are done, it needs to be closed so that the resources that are tied with
the file are freed.
Hence, in Python, a file operation takes place in the following order:
e.
– Open a file
– Read or write (perform operation)
– Close the file
lif
Types Of File in Python
There are two types of files in Python and each of them are explained
below in detail with examples for your easy understanding.
on
They are:
• Binary file
• Text file
th
Py
11
2
Website: https://pythonlife.in/
Binary files in Python
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.
For handling such binary files we need a specific type of software to open
it.
For Example, You need Microsoft word software to open .doc binary files.
in
Likewise, you need a pdf reader software to open .pdf binary files and you
need a photo editor software to read the image files and so on.
e.
Most of the files that we see in our computer system are called binary files.
Example:
• Document files: .pdf, .doc, .xls etc.
lif
• Image files: .png, .jpg, .gif, .bmp etc.
• Video files: .mp4, .3gp, .mkv, .avi etc.
• Audio files: .mp3, .wav, .mka, .aac etc.
on
• Database files: .mdb, .accde, .frm, .sqlite etc.
• Archive files: .zip, .rar, .iso, .7z etc.
• Executable files: .exe, .dll, .class etc.
th
3
Website: https://pythonlife.in/
• Web standards: html, XML, CSS, JSON etc.
• Source code: c, app, js, py, java etc.
in
Python
e.
The method open() is used to open an existing file or creating a new file. If
the complete directory is not given then the file will be created in the
lif
directory in which the python file is stored. The syntax for using open()
•The open method returns file object which can be stored in the name
file_object (file-handle).
File name is a unique name in a directory. The open() function will create
Py
the file with the specified name if it is not already exists otherwise it will
4
Website: https://pythonlife.in/
in
be opened for operations. There are three different access modes are
available in python.
e.
•Reading: Reading mode is crated only for reading the file. The pointer will
be at the beginning of the file.
•Writing: Writing mode is used for overwriting the information on existing
file.
lif
• Append: Append mode is same as the writing mode. Instead of over
on
writing the information this mode append the information at the end.
• ‘w' – Write Mode: This mode is used when you want to write data into the
file or modify it. Remember write mode overwrites the data present in the
file.
•‘a' – Append Mode: Append mode is used to append data to the file.
Remember data will be appended at the end of the file pointer.
• ‘r+' – Read or Write Mode: This mode is used when we want to write or
read the data from the same file.
11
5
Website: https://pythonlife.in/
• ‘a+' – Append or Read Mode: This mode is used when we want to read
data from the file or append the data into the same file.
in
• f = open(r“D:\temp\data.txt“, “r”)
– –raw string
e.
• f = open(“D:\\temp\\data.txt“, “r”)
– -absolute path
•Syntax: file_object.close()
string = "This is a String in Python“
Py
my_file = open(my_file_name.txt,"w+",1)
my_file.write(string)
my_file.close()
print(my_file.closed)
6
Website: https://pythonlife.in/
•In order to read a file in python, we must open the file in read
mode.
•There are three ways in which we can read the files in python.
– read([n])
– readline([n])
– readlines()
in
– all lines returned to a list
Here, n is the number of bytes to be read
e.
Example 1:
my_file = open(“C:/Documents/Python/test.txt”, “r”) print(my_file.read(5))
lif
on
Output:
Hello
Here we are opening the file test.txt in a read-only mode and are reading
th
only the first 5 characters of the file using the my_file.read(5) method.
Example 2:
Py
Output:
Hello World
Hello Python
Good Morning
11
7
Website: https://pythonlife.in/
Here we have not provided any argument inside the read() function.Hence
it will read all the content present inside the file.
Example 3: my_file =
open(“C:/Documents/Python/test.txt”, “r”)
print(my_file.readline(2))
in
Output:
He
e.
This function returns the first 2 characters of the next line.
Example 4:
lif
my_file = open(“C:/Documents/Python/test.txt”, “r”)
print(my_file.readline())
on
Output:
Hello World
Using this function we can read the content of the file on a line by line basis .
th
Example 5:
my_file = open(“C:/Documents/Python/test.txt”, “r”)
Py
print(my_file.readlines())
Output:
[‘Hello World\n’, ‘Hello Python\n’, ‘Good Morning’]
Here we are reading all the lines present inside the text file including the newline
characters .
11
8
Website: https://pythonlife.in/
Reading a 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):
in
print(line)
break
e.
currentline = currentline +1
lif
on
Output:
How are You
In the above example, we are trying to read only the 4 th line from the ‘test.txt’ file
using a “for loop”.
th
filename = “C:/Documents/Python/test.txt”
filehandle = open(filename, ‘r’)
filedata = filehandle.read()
print(filedata)
Output:
11
9
Website: https://pythonlife.in/
Hello World
Hello Python
Good Morning
How are You
in
e.
lif
on
Write to a Python File
In order to write data into a file, we must open the file in write mode.
• We need to be very careful while writing data into the file as it
th
overwrites the content present inside the file that you are writing, and
all the previous data will be erased.
Py
• We have two methods for writing data into a file as shown below.
– write(string)
– writelines(list)
• Example 1:
my_file = open(“C:/Documents/Python/test.txt”, “w”)
my_file.write(“Hello World”)
The above code writes the String ‘Hello World’ into the ‘test.txt’ file.
11
10
Website: https://pythonlife.in/
Example 2:
my_file = open(“C:/Documents/Python/test.txt”, “w”)
my_file.write(“Hello World\n”)
my_file.write(“Hello Python”)
•The first line will be ‘Hello World’ and as we have mentioned \n character,the
cursor will move to the next line of the file and then write ‘Hello Python’.
in
•Remember if we don’t mention \n character, then the data will be written
continuously in the text file like ‘Hello WorldHelloPython’
Example 3:
e.
fruits = [“Apple\n”, “Orange\n”, “Grapes\n”, “Watermelon”]
my_file = open(“C:/Documents/Python/test.txt”, “w”)
my_file.writelines(fruits)
lif
The above code writes a list of data into the ‘test.txt’ file simultaneously
on
Append in a Python 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.
th
Example 1:
Py
Example 2:
my_file = open(“C:/Documents/Python/test.txt”, “a+”)
my_file.write (“\nGuava”)
11
11
Website: https://pythonlife.in/
The above code appends the string ‘Apple’ at the end of the ‘test.txt’ file in
a new line.
flush()function
•When we write any data to file, python hold everything in buffer (temporary
in
memory) and pushes it onto actual file later. If you want to force Python to
write the content of buffer onto storage, you can useflush() function.
•Python automatically flushes the files when closing them i.e. it will be
e.
implicitly called by the close(), BUT if you want to flush before closing any
file you can useflush()
lif
on
th
12
Website: https://pythonlife.in/
– lstrip(): removes given character from left end
– rstrip(): removes given character from right end
FilePointer
•Every file maintains a file pointer which tells the current position in the file
where reading and writing operation will take.
in
•When we perform any read/write operation two things happens:
– The operation at the current position of file pointer
– File pointer advances by the specified number of bytes
e.
lif
on
Binary file Operations
th
•If we want to write a structure such as list or dictionary to a file and read it
Py
13
Website: https://pythonlife.in/
O dump() : to write the object in file which is loaded in binary mode
Syntax : dump(object_to_write, filehandle)
o – load() : dumped data can be read from file using load() i.e. it is used to
read object from pickle file.
in
The four major operations performed using a binary file
are—
e.
1.Inserting/Appending a record in a binary file
lif
2.Reading records from a binary file
3.Searching a record in a binary file
on
4.Updating a record in a binary file
th
14
Website: https://pythonlife.in/
•deals with reading the contents from binary file student using load()
method of pickle module. It is used to read the object from the opened file.
The syntax for this is given by the statement—
– object = pickle.load(file)
in
3.Searching a record in a binary file
Searching the binary file ( "student" )is carried out on the basis of the roll
e.
number entered by the user. The file is opened in the read-binary mode
and gets stored in the file object, f. load() method is used to read the object
lif
from the opened file. A variable ‘found’ is used which will tell the status of
the search operation being successful or unsuccessful. Each record from
on
the file is read and the content of the field, roll no, is compared with the roll
number to be searched. Upon the search being successful, appropriate
message is displayed to the user.
th
Py
15
Website: https://pythonlife.in/
written to the file and the record is updated. seek() method is used for
random access to the file. (more about seek() in later sessions)
in
RANDOM ACCESS IN FILES USING TELL() AND
e.
SEEK()
lif
•Till now, in all our programs we laid stress on the sequential
processing of data in a text and binary file.
on
•But files in Python allow random access of the data as well using
built-in methods seek() and tell().
th
seek()—seek() function is used to change the position of the file handle (file
pointer) to a given specific position. File pointer is like a cursor, which defines
Py
16
Website: https://pythonlife.in/
• 0: sets the reference point at the beginning of the file, which is by default.
• 1: sets the reference point at the current file position.
• 2: sets the reference point at the end of the file.
● seek() can be done in two ways:
– Absolute Positioning
– Relative Positioning
•Absolute referencing using seek() gives the file number on which the file
in
pointer has to position itself. The syntax for seek() is—
– f.seek(file_location) #where f is the file pointer
e.
•For example, f.seek(20) will give the position or file number where the file
pointer has been placed. This statement shall move the file pointer to 20th
byte in the file no matter where you are.
lif
Relative referencing/positioning has two arguments, offset and the position
on
from which it has to traverse. The syntax for relative referencing is:
– f.seek(offset, from_what) #where f is file pointer, For example,
– f.seek(–10,1) from current position, move 10 bytes backward
th
tell()—tell() returns the current position of the file read/write pointer within
the file. Its syntax is:
• f.tell() #where f is file pointer
• When we open a file in reading/writing mode, the file pointer rests at 0th
byte.
11
17
Website: https://pythonlife.in/
• When we open a file in append mode, the file pointer rests at the last
byte.
• This is illustrated in the practical implementation that follows:
in
text file that uses specific structuring to arrange tabular data.
Because it’s a plain text file, it can contain only actual text
e.
data—in other words, printable ASCII or Unicode characters .
• The structure of a CSV file is given away by its name.
lif
Normally, CSV files use a comma to separate each specific
data value.
on
th
Py
11
18
Website: https://pythonlife.in/
in
e.
lif
on
th
Py
Normally, the first line identifies each piece of data—in other words, the
name of a data column. Every subsequent line after that is actual data and
is limited only by file size constraints.
• In general, the separator character is called a delimiter, and the comma is
not the only one used. Other popular delimiters include the tab (\t), colon (:)
and semi-colon (;) characters. Properly parsing a CSV file requires us to
know which delimiter is being used.
11
19
Website: https://pythonlife.in/
• CSV is a simple flat file in a human readable format which is extensively
used to store tabular data, in a spreadsheet or database. A CSV file stores
tabular data (numbers and text) in plain text.
Files in the CSV format can be imported to and exported from programs
that store data in tables, such as Microsoft Excel or OpenOffice Calc.
• CSV stands for “comma separated values”. Thus, we can say that a
in
comma separated file is a delimited text file that uses a comma to separate
values.
e.
Each line in a file is known as data/record. Each record consists of one or
more fields, separated by commas (also known as delimiters), i.e., each of
lif
the records is also a part of this file. Tabular data is stored as text in a CSV
file. The use of comma as a field separator is the source of the name for
this file format. It stores our data into a spreadsheet or a database.
on
WHY USE CSV?
th
•The extensive use of social networking sites and their various associated applications
requires the handling of huge data. But the problem arises as to how to handle and
organize this large unstructured data?
Py
•The solution to the above problem is CSV. Thus, CSV organizes data into a structured
form and, hence, the proper and systematic organization of this large amount of data is
done by CSV. Since CSV file formats are of plain text format, it makes it very easy for
website developers to create applications that implement CSV.
•the several advantages that are offered by CSV files are as follows:
– CSV is faster to handle.
– CSV is smaller in size.
– CSV is easy to generate and import onto a spreadsheet or database.
– CSV is human readable and easy to edit manually.
11
20
Website: https://pythonlife.in/
– CSV is simple to implement and parse.
– CSV is processed by almost all existing applications.
•For working with CSV files in Python, there is an inbuilt module called
CSV. It is used to read and write tabular data in CSV format.
•To perform read and write operations with CSV file, we must import CSV
module. CSV module can handle CSV files correctly regardless of the
in
operating system on which the files were created.
• Along with this module, open() function is used to open a CSV file and
e.
return file object. We load the module in the usual way using import:
– >>> import csv
Like other files (text and binary) in Python, there are two basic operations
lif
that can be carried out on a CSV file:
– 1. Reading from a CSV file
on
– 2. Writing to a CSV file
Python contains a module called csv for the handling of CSV files. The
reader class from the module is used for reading data from a CSV file. At
Py
first, the CSV file is opened using the open() method in ‘r’ mode(specifies
read mode while opening a file) which returns the file object then it is read
by using the reader() method of CSV module that returns the reader object
that iterates throughout the lines in the specified CSV document.
Syntax:
csv.reader(csvfile, dialect='excel', **fmtparams)
Example:
11
21
Website: https://pythonlife.in/
Consider the below CSV file –
in
e.
lif
on
Source Code:
import csv
th
csvFile = csv.reader(file)
# displaying the contents of the CSV file
for lines in csvFile:
print(lines)
Output:
[['Steve', 13, 'A'],
['John', 14, 'F'],
['Nancy', 14, 'C'],
11
22
Website: https://pythonlife.in/
['Ravi', 13, 'B']]
in
newline=” otherwise, newline characters inside the quoted fields will not be interpreted correctly.
Syntax:
csv.writer(csvfile, dialect='excel', **fmtparams)
csv.writer class provides two methods for writing to CSV. They are writerow() and writerows().
e.
writerow(): This method writes a single row at a time. Field row can be written using this method.
•
Syntax:
lif
writerow(fields)
writerows(): This method is used to write multiple rows at a time. This can be used to write rows list.
Syntax:
on
writerows(rows)
Example:
import csv
fields = ['Name', 'Branch', 'Year', 'CGPA']
th
23
Website: https://pythonlife.in/
csvwriter.writerow(fields)
# writing the data rows
csvwriter.writerows(rows)
Output:
in
e.
lif
on
th
We can also write dictionary to the CSV file. For this the CSV module provides the csv.DictWriter class.
Py
This class returns a writer object which maps dictionaries onto output rows.
Syntax:
csv.DictWriter(csvfile, fieldnames, restval=”, extrasaction=’raise’, dialect=’excel’, *args, **kwds)
csv.DictWriter provides two methods for writing to CSV. They are:
•writeheader(): writeheader() method simply writes the first row of your csv file using the pre-specified
fieldnames.
Syntax:
writeheader()
•writerows(): writerows method simply writes all the rows but in each row, it writes only the values(not
keys).
Syntax:
11
24
Website: https://pythonlife.in/
writerows(mydict)
Example:
import csv
mydict =[{'branch': 'COE', 'cgpa': '9.0', 'name': 'Nikhil', 'year': '2'},
{'branch': 'COE', 'cgpa': '9.1', 'name': 'Sanchit', 'year': '2'},
{'branch': 'IT', 'cgpa': '9.3', 'name': 'Aditya', 'year': '2'},
{'branch': 'SE', 'cgpa': '9.5', 'name': 'Sagar', 'year': '1'},
{'branch': 'MCE', 'cgpa': '7.8', 'name': 'Prateek', 'year': '3'},
in
{'branch': 'EP', 'cgpa': '9.1', 'name': 'Sahil', 'year': '2'}]
e.
filename = "university_records.csv"
with open(filename, 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames = fields)
writer.writeheader()
lif
writer.writerows(mydict)
on
Output:
th
Py
11
25
Website: https://pythonlife.in/
in
e.
lif
on
th
Py
11
26
Website: https://pythonlife.in/
in
e.
lif
on
th
Py